Professional Documents
Culture Documents
■types of synonyms
–private
–public
■private
–can be used by a user who has created it
■public
–accessible to all users
–can be created by only a user having dba privilege
■syntax:
–create [public] synonym synonym_name for
object_name;
■e.g.
–create synonym employee for emp;
deleting synonym
■drop command is used to delete a synonym
■syntax:
–drop [public] synonym synonym_name;
■e.g.
–drop synonym employee;
sequence
■is used to generate the series of unique integers
■multiple users can use same sequence
■syntax:
–create sequence sequence_name
[increment by n]
[start with n];
■e.g.
–create sequence deptnum
increment by 10
start with 50;
–create sequence s1
start with -10
increment by 1
maxvalue -1
minvalue –10
–create sequence s1
start with -10
increment by 1
maxvalue 1
minvalue -100
cache 50
cycle
■currval
–returns the current value in the sequence
■nextval
–increments the current value in the sequence and returns
incremented value
insert a new department ‘production’ located at
‘pune’ into dept table using sequence.
solution:
insert into dept values (deptnum.nextval,
‘production’, ‘pune’);
views
■objectives:
–explain the need for a view
–state the syntax for creating a view
–construct a view
–state advantages and disadvantages of views
–state rules & restriction for updating views
■why views?
–the table in a database defines the structure and
organisation of it’s data
–all the users of a database do not need to access all the
data in the database
–oracle enables you to present only relevant data to users
by using view
–view is a virtual table whose contents are taken from
other tables through the execution of a query
–a view is a database object that contains no data of its
own
–the tables from which a view derives its data are known
as base tables
–a base table might be a table or it might be another view
■characteristics of a view
–unlike a table, a view does not physically exists in a
database
–a user cannot distinguish between a table and a view
–a view is queried just like querying a table
–change in the base table’s data is automatically reflected
in the view
–most of the operations on a table can also be carried out
with views, with some restrictions
creating a view
■to create a view, you need to :
–identify the base table or tables from which the view is
desired
–specify the columns to be displayed in the view
–use the create view statement
■syntax :
create [ or replace ] [ force | noforce ]
view <view_name>
[(<column_list>)]
as
select statement
[ with check option ];
■create :
–creates a new view with the specified name
■or replace :
–replaces a view if it already exists
–used to change the definition of an existing view
■force :
–creates a view (with compilation errors) regardless of
whether the base table exists or user has the privileges
on them
–to view the view
■user must have privileges
■the base table(s) must exist
■noforce :
–default option
–creates the view only if
■the base table(s) exist
■the user has privileges on them
■you can create two types of views
–simple views
■simple views are based on a single table
■they do not contain functions or groups of data
■dml operation is possible
–complex views
■complex views are based on one or more tables
■can contain functions, groups of data and join
■dml operation may not be allowed
creating a view
■the view’s default column names are same as the
base table column names
■new column names, if required, can be specified
with create view column list
■new column names can be used with view only
■in a view definition you can use
–group by clause
–joins, sub-queries, nested with other tables or views
–a view to define other views
–group functions, expressions on columns
–order by clause (8i onwards)
■examples :
1. create a view for the employees belonging to
department 20.
delete restrictions
■you can not delete rows when the view contains
–group function
–distinct clause
–group by clause
–join condition
update restrictions
■you can not update a view when the view contains
–expressions such as sal+comm
–restrictions same as stated for delete
insert restrictions
■you can not insert rows using a view when
–view does not contain all the not null columns of the base
table
–restrictions same as stated for delete and update
views restrictions
■in the following examples(1,2 & 3) specify whether the
view is updatable or read-only and why?
1. create view empview as
select empno, count(*) total_increments
from incr group by empno;
2. create view empsal as
select empno, sal * 0.1 pct_sal from emp;
deleting a view
■in some situations you may need to delete a view
■drop view command is used
■syntax :
drop view <view_name>;
indexing
objectives :
–list methods for improving performance of data retrieval
–explain & construct an index on a table
–list guidelines for creating indexes
enhancing performance
■indexing is common way to enhance performance in
retrieving information from a table
■sql syntax does not change
■these database objects are transparent to the user
indexing
■oracle analyses each query to find the fastest path
to the data
■what is an index?
■indexes provide a fast access path to columns
that are indexed
■indexes can also be used to ensure that no
duplicate values are entered into a column
(using unique index)
■indexes are referred whenever the indexed columns
are referenced in the where clause
■indexes are used to reference records in all the sql
statements, not just the select query
managing indexes
■indexes do not have to be explicitly activated or
deactivated
■with every data manipulation the appropriate
index(s) are automatically updated
■indexes are stored separately from actual data
■oracle does not limit the number of indexes on a
table
creating an index
■create index command is used
■syntax :
create [unique] index <index_name>
on <table_name> (<column_list>);
examples
■create a unique index for emp table on employee
name
create unique index emp_idx
on emp(ename);
■create an index on deptno and dname for dept table
create index dept_idx
on dept(deptno, dname);
indexes : guidelines
■you may create any number of indexes on different
columns of a table
■index names should be unique
■indexes can be used to force unique values in the
columns
■create indexes on the columns which are used most
often in the where clause
■do not create too many indexes on table as it
results to
–more disk space
–affecting response time for data manipulation operations
■use index only on larger tables
–load some data into database first then create indexes
■if the data is static and heavily queried, more
indexes will help
■if you join two tables, create index on the joining
column in one or both tables
deleting an index
■use drop index command
■syntax :
drop index <index_name>;
■example :
drop index emp_idx;