Chapter 7 Creating SEQUENCES and VIEWS in ORACLE
Chapter 7 Creating SEQUENCES and VIEWS in ORACLE
ORACLE
A sequence is used to generate numbers in sequence. You can use sequences to insert unique values
in Primary Key and Unique Key columns of tables. To create a sequence give the CREATE
SEQUENCE statement.
CREATING SEQUENCES
create sequence bills
start with 1
increment by 1
minvalue 1
maxvalue 100
cycle
cache 10';
The above statement creates a sequence bills it will start with 1 and increment by 1. It’s maxvalue is
100 i.e. after 100 numbers are generated it will stop if you say NOCYCLE, otherwise if you mention
CYCLE then again it will start with no. 1. You can also specify NOMAXVALUE in that case the
sequence will generate infinite numbers.
The CACHE option is used to cache sequence numbers in System Global Area (SGA). If you say
CACHE 10 then Oracle will cache next 10 numbers in SGA. If you access a sequence number then
oracle will first try to get the number from cache, if it is not found then it reads the next number from
disk. Since reading the disk is time consuming rather than reading from SGA it is always
recommended to cache sequence numbers in SGA. If you say NOCACHE then Oracle will not cache
any numbers in SGA and every time you access the sequence number it reads the number from disk.
BILLS
-----
1
NEXTVAL gives the next number in sequence. Whereas, CURRVAL returns the current number of
the sequence. This is very handy in situations where you have insert records in Master Detail tables.
For example to insert a record in SALES master table and SALES_DETAILS detail table.
insert into sales (billno,custname,amt)
values (bills.nextval,’Sami’,2300);
Sequences are usually used as DEFAULT Values for table columns to automatically insert unique
numbers.
For Example,
Now whenever you insert rows into invoices table ommiting invoice_no as follows
ALTERING SEQUENCES
To alter sequences use ALTER SEQUENCE statement. For example, to alter the bill sequence
MAXVALUE give the following command.
Except Starting Value, you can alter any other parameter of a sequence. To change START WITH
parameter you have to drop and recreate the sequence.
DROPPING SEQUENCES
To drop sequences use DROP SEQUENCE command. For example, to drop bills sequence give the
following statement
Views
Views are known as logical tables. They represent the data of one of more tables. A view derives its
data from the tables on which it is based. These tables are called base tables. Views can be based on
actual tables or another view also.
Whatever DML operations you performed on a view they actually affect the base table of the view.
You can treat views same as any other table. You can Query, Insert, Update and delete from views,
just as any other table.
Views are very powerful and handy since they can be treated just like any other table but do not
occupy the space of a table.
The following sections explain how to create, replace, and drop views using SQL commands.
Creating Views
Suppose we have EMP and DEPT table. To see the empno, ename, sal, deptno, department name and
location we have to give a join query like this.
select e.empno,e.ename,e.sal,e.deptno,d.dname,d.loc
From emp e, dept d where e.deptno=d.deptno;
So everytime we want to see emp details and department names where they are working we have to
give a long join query. Instead of giving this join query again and again, we can create a view on
these table by using a CREATE VIEW command given below
Now to see the employee details and department names we don’t have to give a join query, we can
just type the following simple query.
This will show same result as you have type the long join query. Now you can treat this EMP_DET
view same as any other table.
For example, suppose all the employee working in Department No. 10 belongs to accounts
department and most of the time you deal with these people. So every time you have to give a DML
or Select statement you have to give a WHERE condition like .....WHERE DEPTNO=10. To avoid
this, you can create a view as given below
Now to see the account people you don’t have to give a query with where condition you can just type
the following query.
The query that defines the ACCOUNTS_STAFF view references only rows in department 10.
Furthermore, WITH CHECK OPTION creates the view with the constraint that INSERT and
UPDATE statements issued against the view are not allowed to create or result in rows that the view
cannot select.
Considering the example above, the following INSERT statement successfully inserts a row into the
EMP table through the ACCOUNTS_STAFF view:
However, the following INSERT statement is rolled back and returns an error because it attempts to
insert a row for department number 30, which could not be selected using the ACCOUNTS_STAFF
view:
Dropping Views
Use the SQL command DROP VIEW to drop a view. For example:
If you are in doubt whether a view is modifiable, then you can SELECT from the view
USER_UPDATABLE_COLUMNS to see if it is. For example:
5 rows selected.