Top Banner
20
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Sequences
Page 2: Sequences

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.

Page 3: Sequences

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.

Page 4: Sequences

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.

Page 5: Sequences

create sequence seq1 increment by 1 start with 1;

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

1

Page 6: Sequences

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

Page 7: Sequences

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.

Page 8: Sequences

alter sequence seq1 nomaxvalue nominvalue;

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

Page 9: Sequences

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.

Page 10: Sequences

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

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

Page 11: Sequences

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

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

Page 12: Sequences

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.

Page 13: Sequences

Create sequence seq1 increment by 1 start with 1 cache;

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

Page 14: Sequences

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.

Page 15: Sequences

Select seq1.nextval from dual;

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

Page 16: Sequences

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.

Page 17: Sequences

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

Page 18: Sequences

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.

Page 19: Sequences

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

Page 20: Sequences