Sequences
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
703
On Slideshare
702
From Embeds
1
Number of Embeds
1

Actions

Shares
Downloads
11
Comments
0
Likes
0

Embeds 1

http://blackboard.cpsb.org 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1.  
  • 2.
    • 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.
  • 3.
    • Create sequence s equencename [ 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.
  • 4.
    • SEQUENCENAME:I t 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.
  • 5.
    • create sequence seq1 increment by 1 start with 1;
    • select seq1.nextval from dual;
    • Output:
    • NEXTVAL ----------
    • 1
  • 6.
    • 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
  • 7.
    • 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.
  • 8.
    • alter sequence seq1 nomaxvalue nominvalue ;
    • OUTPUT:
    • select seq1.nextval from dual;
    • 1
    • 2
    • 3
    • .
    • .
    • .
  • 9.
    • 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.
  • 10.
    • Create sequence seq2 increment by 1 start with1 maxvalue 21 cycle;
    • OUTPUT:
    • Select seq2.nextval from dual;
    • 1
    • 2
    • .
    • .
    • 21
    • 1
  • 11.
    • create sequence seq2 increment by 1 start with 1 maxvalue 2 nocycle ;
    • OUTPUT:
    • Select seq2.nextval from dual;
    • 1
    • 2
    • Error:exceed maximum value.
  • 12.
    • 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.
  • 13.
    • Create sequence seq1 increment by 1 start with 1 cache ;
    • OUTPUT
    • Select seq1.nextval from dual;
    • 1
    • 2
    • 3
    • .
    • .
  • 14.
    • 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.
  • 15.
    • Select seq1.nextva l from dual;
    • And
    • Insert into tablenm values( seq1.nextval ,’abc’);
  • 16.
    • 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.
  • 17.
    • create sequence seq2 increment by 1 start with 1 maxvalue 4 cycle;
  • 18.
    • 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.
  • 19.
    • 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
  • 20.