Monday, October 15, 2012

Creating and Using a Sequence - Overview



SEQUENCE:
                Sequence is a database object which automatically generates unique and numeric values. By default sequence number starts with ‘1’ and incremented by ‘+1’ upto hard disk capacity.
--> We can’t apply the sequence mechanism on character column.

Syntax to create Sequence : 
====================
Syntax: create sequence <sequence_name>
Example: Create sequence s; 

Applying Sequence : 
=============== 
Syntax:  insert into <table_name> values(sequence_name.nextval,&col2,…….,&coln); 
Example: insert into product values(s.nextval,’&pname’,&cost); 


NOTE : In the above example nextval represents pseudo column, it initiates the sequence value and also it will give next value of the sequence. 

Syntax to know the current value of Sequence : 
==================================
Syntax:select <sequence_name>.currval from dual;
Example: select s.currval from dual; 

NOTE : Here currval represents pseudo column, it gives the current value of the sequence. 



Create Sequence using start value, ending value and increment value :
 ===================================================
Syntax:Create sequence <sequence_name>  Minvalue integer Maxvalue integer increment by integer;
 Example:Create sequence s1 Minvalue 101 Maxvalue 110 increment by 1; 

Modify the Sequence : 
=================
Syntax: alter sequence <sequence_name> Maxvalue integer ; 
Example: alter sequence s1 maxvalue 120; 

Create sequence with decrement value : 
==============================
Example: Create sequence s2 Minvalue 110 Maxvalue 101 Increment by -1; 

INCREMENT BY
Specify the interval between sequence numbers. This integer value can be any positive or negative integer, but it cannot be 0. This value can have 28 or fewer digits. The absolute of this value must be less than the difference of MAXVALUE and MINVALUE. If this value is negative, then the sequence descends. If the value is positive, then the sequence ascends. If you omit this clause, then the interval defaults to 1.

START WITH 
 Specify the first sequence number to be generated. Use this clause to start an ascending sequence at a value greater than its minimum or to start a descending sequence at a value less than its maximum. For ascending sequences, the default value is the minimum value of the sequence. For descending sequences, the default value is the maximum value of the sequence. This integer value can have 28 or fewer digits.
----------------------------------------------------------------------------------------
Note:This value is not necessarily the value to which an ascending cycling sequence cycles after reaching its maximum or minimum value. 
----------------------------------------------------------------------------------------
MAXVALUE 
Specify the maximum value the sequence can generate. This integer value can have 28 or fewer digits. MAXVALUE must be equal to or greater than START WITH and must be greater than MINVALUE.

NOMAXVALUE 
Specify NOMAXVALUE to indicate a maximum value of 1027 for an ascending sequence or -1 for a descending sequence. This is the default.

MINVALUE
Specify the minimum value of the sequence. This integer value can have 28 or fewer digits. MINVALUE must be less than or equal to START WITHand must be less than MAXVALUE.

NOMINVALUE 
Specify NOMINVALUE to indicate a minimum value of 1 for an ascending sequence or -1026 for a descending sequence. This is the default.

--> We can apply the sequence on more than one column in the table.

  • Example: insert into product values(s2.nextval,’&pname’,s2.nextval); 

--> Applying more than one sequence in the same table. 

  • Example: insert into product values(s2.nextval,’&pname’,s3.nextval); 


List Of Sequences :
===============
Syntax: Select * from user_sequences; 

CYCLE 

Specify CYCLE to indicate that the sequence continues to generate values after reaching either its maximum or minimum value. After an ascending sequence reaches its maximum value, it generates its minimum value. After a descending sequence reaches its minimum, it generates its maximum value.
  • Example: Create sequence s3 Minvalue 101 Maxvalue 110 Nocache Cycle; 

NOCYCLE 
Specify NOCYCLE to indicate that the sequence cannot generate more values after reaching its maximum or minimum value. This is the default.

CACHE
Specify how many values of the sequence the database preallocates and keeps in memory for faster access. This integer value can have 28 or fewer digits. The minimum value for this parameter is 2. For sequences that cycle, this value must be less than the number of values in the cycle. You cannot cache more values than will fit in a given cycle of sequence numbers. Therefore, the maximum value allowed for CACHE must be less than the value determined by the following formula:

 (CEIL (MAXVALUE - MINVALUE)) / ABS (INCREMENT)

If a system failure occurs, then all cached sequence values that have not been used in committed DML statements are lost. The potential number of lost values is equal to the value of the CACHE parameter.
----------------------------------------------------------------------------------------
Note: Oracle recommends using the CACHE setting to enhance performance if you are using sequences in an Oracle Real Application Clusters environment. 
----------------------------------------------------------------------------------------
NOCACHE 
Specify NOCACHE to indicate that values of the sequence are not preallocated. If you omit both CACHE and NOCACHE, then the database caches 20 sequence numbers by default.

ORDER 
Specify ORDER to guarantee that sequence numbers are generated in order of request. This clause is useful if you are using the sequence numbers as timestamps. Guaranteeing order is usually not important for sequences used to generate primary keys. ORDER is necessary only to guarantee ordered generation if you are using Oracle Real Application Clusters. If you are using exclusive mode, then sequence numbers are always generated in order.

NOORDER 
Specify NOORDER if you do not want to guarantee sequence numbers are generated in order of request. This is the default.


To Drop the Sequence :
=================
Syntax:Drop sequence <sequence_name>
Example: Drop sequence s2; 

To rename the Sequence :
===================
Syntax : Rename <old_sequence_name> to <new_sequence_name>;
Example: rename s1 to sss;




No comments:

Post a Comment

back to top