Sequences

Preview:

DESCRIPTION

 

Citation preview

A sequence is a user created data base object that can be shared by multiple users to generate unique integer.

A typical usage of sequence is to create a primary key value, which must be unique.

Sequence numbers are stored and generated independently of tables. Therefore the same sequence can be used for multiple tables.

Create sequence sequencename[increment by n start with n maxvalue n/ nomaxvalue minvalue n/ nominvalue cycle/ nocycle cache n/ nocache order/ noorder].

Sequences is used in create , Insert, select, alter ,drop statement.

The start value of sequence cannot be altered.

SEQUENCENAME:It is the name of the sequence.

INCREMENT BY N: specifies the interval between the sequences nos where n is an integer. The default vale is 1.

START WITH: specifies the first sequence number to be generated.

create sequence seq1 increment by 1 start with 1;

select seq1.nextval from dual; Output: NEXTVAL ----------

1

MAXVALUE: specifies the maximum sequence value.

MINVALUE: specifies the minimum sequence value.

EXAMPLE: alter sequence seq1 minvalue 1

maxvalue 2; select seq1.nextval from dual; 2

NOMAXVALUE: specifies a maximum value of 10^27 for an ascending sequence or -1 for descending sequence.

NOMINVALUE: specifies a maximum value of 1 for an ascending sequence or (-10)^26 for descending sequence.

alter sequence seq1 nomaxvalue nominvalue;

OUTPUT:select seq1.nextval from dual;123...

CYCLE: specifies that the sequence continues to generate repeat values after reaching either its maximum value.

NOCYCLE: specifies that the sequence cannot generate more values after reaching either its maximum value.

Create sequence seq2 increment by 1 start with1 maxvalue 21 cycle;

OUTPUT: Select seq2.nextval from dual; 1 2 . . 21 1

create sequence seq2 increment by 1 start with 1 maxvalue 2 nocycle;

OUTPUT:Select seq2.nextval from dual;12Error:exceed maximum value.

CACHE: specifies how many values of a sequence oracle pre-allocates and keeps in memory for faster access.

NOCACHE: specifies that values of a sequence are not pre-allocated.

Create sequence seq1 increment by 1 start with 1 cache;

OUTPUT Select seq1.nextval from dual; 1 2 3 . .

NEXTVAL: The nextval pseudocolumns is used to

extract successive nos from a specified sequence.

We must qualify nextval with the seq.name.

When we reference seq.nextval, a new sequence number is generated and the current sequence number is placed in currval.

Select seq1.nextval from dual;

And Insert into tablenm values(seq1.nextval,’abc’);

CURRVAL: The currval pseudocolumn is used to

refer to sequence number that the current user has just generated.

Nextval must be used to generate a sequence number in the current users session before currval can referrenced.

When seq.currval is referenced, the last value returned to that users process is displayed.

create sequence seq2 increment by 1 start with 1 maxvalue 4 cycle;

ERROR: number to CACHE must be less than one cycle.

In the above query maxvalue should be above 20.

It should be always more than 20 because oracle caches 20 sequences numbers by default.

So if you want above query to work than give any maxvalue, cycle and cache whose value will be less than max

EXAMPLE: create sequence seq2 increment by 1 start with 1

maxvalue 4 cycle cache 3; OUTPUT: 1 2 3 4 1

Recommended