You are on page 1of 11

synonym

■used to give simple alias for database object


■used to give meaningful alternative name for
database objects

■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

■sequence provides two attributes for using the values


generated using the sequence

■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’);

■drop command is used to delete a sequence


■syntax:
–drop sequence sequence_name;
■e.g.
drop sequence deptnum;

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.

create view dept20 as


select * from emp
where deptno = 20;

2. list all the employees from department 20.


normal query :
select * from emp
where deptno = 20;
using the view dept20 :
select * from dept20;
views : advantages
■security
–restricting access to a table or tables
–critical data in the base table(s) is safeguarded as access
to such data can be controlled using views
■query simplicity
–views draw data from multiple tables and present it to
the user as single table
–complicated queries are simplified as multi-table queries
change to single table queries
■structural simplicity
–personalized view of the data using virtual table
–you can show same data to different users in different
ways
■insulation from change :
–source tables may split, restructured or renamed
–view can still present consistent, unchanged image of the
structure of the database
■data integrity
–as views does not store any data, there is no integrity
problem
–changes in the base tables are automatically reflected in
the views and vice versa
■performance :
–performance gets affected as
■queries against views are to be translated to the queries on
base tables
■a view can be complex with a multi-join query
■update restrictions :
–more restrictions on manipulation of data using views
than writing queries directly on base tables
–complex views are typically read-only
updating data using views
■like tables insert, update and delete commands can
also be used with views
■indirect way of manipulating tables
■while manipulating tables through views, certain
rules are applicable
■the criteria that determines whether the operations
are possible through a view are
–the view must be based on a single table
–it must not have columns that are aggregate functions
–it must not have expressions in its definition
–it must not specify distinct in its definition
–it must not use group by or having clause it its definition
–it must not use subqueries

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;

3a. create view empdept30 as


select empno, ename, deptno from emp
where deptno = 30;
4. add an employee with empno (1234), ename (john),
deptno (20) using the view from ex3.

5. is it possible to view this record using view in ex3?

6. is it possible to restrict such update operations? if yes,


then how?

■some times, insert or update operations on a view


can result in data that the view can’t retrieve
■you might want to restrict the view so that the view
does not accept any data that it can’t display
■use the with check option with create view
statement

with check option


example :

create view empdept30 as


select empno, ename, deptno from emp
where deptno = 30
with check option;
alter view
■alter view command is used to recompile the views
■syntax :
alter view <view_name> compile;
■recompiling is useful
–for views created with compilation errors
–also when we alter base tables and want to see its
effects on derived views

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;