click here
                

Sequence

  • A sequence is a database object that generates unique integer number in sequential order. It can be used to automatically generate primary key values or unique key values
  • Sequencs can either in ascending or descending order.

Features /characteristics of sequences:

  1. sequence are available to all users of the database.
  2. sequence are created using SQL statements.
  3. sequence have a minimum and maximum value (the default are minimum=0 and maximum=263-1); they can be droped, but not reset
  4. once a sequence returns a value, the sequence can never return the same value.
  5. while sequence values are not tied to any particular table, a sequence is usually used to generate values for only one table
  6. sequence increment by an amount specified when created (the default is 1)

creating sequences

  • To create a sequence in your schema, you must have the CREATE SEQUENCE system privilege the syntax for creating the sequence is,
CREATE SEQUENCE sequence_name [START WITH start_num] [INCREMENT BY increment_num] [{MAXVALUE maximum_num | NOMAXVALUE}] [{MINVALUE minimum_num | NOMINVALUE}] [{CYCLE | NOCYCLE}] [{CACHE cache_num | NO CACHE}] [{ORDER | NOORDER}] ;

    where,

  • START WITH : It specifies the start value for the sequence. the default START WITH for an ascending sequence is the sequence minimum value (1) and for descecnding sequence, it is the maximum value(-1)
  • INCREMENT BY : it specifies the value how the sequence increments each iteration. by default a sequence generator increments by 1 it can be any positive or negative value but not zero.
  • MINVALUE : this is the minimum value that the sequence will generate the value specified in MINVALUE must be greater than or equal to START WITH value NOMINVALUE is the default value that is equal to 1 for an ascending sequence and -1026 for decending sequence.
  • MAXVALUE : it specify value the bounds of the sequence generator. it specifies the highest value that it can generate. NOMAXVALUE is the default value that is equal to 1027 and -1 for descending sequence
  • CYCLE : specify CYCLE to indicate that when the maximum value is reached the sequence starts over again at the start value. specify NOCYCLE to generate an error upon reaching the maximum value.
  • CACHE : it specify how many values of a sequence Oracle pre-allocates and keep in memory for faster access. the minimum value for this parameter is two.
  • NOCACHE : it specify that values of a sequence are not pre-allocated
  • ORDER : It guarantees that sequence number are generated in the order of request.
  • NOORDER : this does not guarantee sequence numbers are assigned in order of request. you can use these parameter to indicate whether the seequence is ascending or descending. the starting point of the sequence, the minimum and maximum value, and the interval between sequence values.
  • example SQL > CREATE SEQUENCE EID START WITH 1 INCREMENT BY 1 MAXVALUE 100; sequence created.
  • On execution, Oracle will create a squence EID its START WITH value is 1, incrementing The sequence number by 1. Maximum value that it can generate is 100

Referencing a sequence :

  • A sequence is referenced in SQL statements with the NEXTVAL and CURRVAL
  1. NEXTVAL : Each new sequence number is generated by a reference to the sequence. for example for reference to the sequence For example for reference it should be used as 'seq_name.NEXTVAL'.
  2. CURVAL : The current sequence number can be referenced For example for reference it should be used as 'seq_name.CURRVAL'.
  • NEXTVAL and CURRVAL can be used in SQL statement such as SELECT, INSERT, or UPDATE

Generating sequence number with NEXTVAL :

  • NEXTVAL is used to generate unique number. to generate and use a sequence number, use seq_name.NEXTVAL.
  • Assume the table of 'EMPLOYEES' where we want to add the EID in the sequence starting from 1 to 100
  • For example:

  • SQL > INSERT INTO EMPLOYEES VALUES (EID.NEXTVAL, 'Divesh');
  • In the example 'EID' is the sequqnce number and 'NEXTVAL' is used to generate a unique sequence number. as defined, the first reference to EID.NEXTVAL return value 1. each subsequent statement that references EID.NEXTVAL generates the next sequence number (2,3,4,....).

Using sequence number with CURRVAL :

  • To use or refer to the current sequence value of your session, reference seq_name.CURRVAL.
  • CURRVAL can only be used if seq_name.NEXTVAL has been referenced in the current or a previous transaction.
  • SQL > SELECT EID.CURRVAL FROM table_name;

Altering Sequences

  • You can alter a sequence to change any of the parameters that define how it generates sequence numbers except the sequence starting number.
  • Can set or remove minvalue and maxvalue, change the increment value
  • Some important restrictions on altering the sequence:
    1. To change the stating point of a sequence, drop the sequence and then re-create it.
    2. NEW MAXVAL should not be less than starting value.

syntax:

ALTER SEQUENCE sequence_name [START WITH star_num] [INCREMENT BY increment_num] [{MAXVALUE maximum_num | NOMAXVALUE}] [{MINVALUE minimum_num | NOMINVALUE}] [{CYCLE | NOCYCLE}] [{CACHE cache_num | NOCACHE}] [{ORDER | NOORDER}] ;

Example:

  • This statement sets a new maximum value for the EID sequence: ALTER SEQUENCE EID MAXVALUE 1500;
  • This statement turns on CYCLE and CACHE for the EID sequence: ALTER SEQUENCE EID CYCLE CACHE 5;

Dropping Sequences

  • If a sequence is no longer required, you can drop the sequence using the DROP SEQUENCE statement.
  • To drop a sequence in another schema, you must have the DROP ANY SEQUENCE system privilege.

syntax:

    DROP SEQUENCE;

Example:

    SQL > DROP SEQUENCE EID;
sequence droped.
Thanks for Downloading...