Sequence in Database Management System(DBMS).
Sequence are used to generate unique numbers for the fields in the table which require values to remain unique.
If table has been specified with primary key constraints or an uniqueness constraints , sequence can be used to generate unique numbers.
If generation of unique numbers is not automatically, which will lead to be done manually, which will lead to inconsistent values of primary key and unique key.
Sequence are stored as database object and for it object in database, whenever we use create, we can use drop also if we want to remove them.
Syntax of sequence:
CREATE SEQUENCE sequence_name
START WITH integer_value
INCREMENT/DECREMENT BY integer_value
MINVALUE integer_value
MAXVALUE integer_value
CYCLE|NO-CYCLE
CACHE integer_value
ORDER|NO-ORDER
In the above sequence CREATE and SEQUENCE are keyword , sequence name is the name of sequence. Start with is a keyword and it is used to specify value of the sequence to be specified as an integer and it is a compulsory clause along with create sequence.
INCREMENT/DECREMENT BY are the keywords which can be used to specify ascending or descending order sequence starting with an integer value , it is also a compulsory clause.
MINVALUE is used to specify the minimum value of the sequence and it is also a keyword.It is an optional clause and if not specify then minimum value will be the default value of the system. If we need to wrapped the sequence then minimum value is compulsory.
MAXVALUE is the keyword used to specify the maximum value ti the sequence , It is also an optional clause and if not specify then maximum value will be the default maximum value of the integer . If sequence is to be wrapped around then max value is compulsory.
CYCLE|NO-CYCLE is keyword used to specify wrapping of the sequence , If cycle clause is specified then min value and max value both should be specified . If nothing is specified then non cycle is by default applicable.
CACHE is the keyword used to specify the number of sequence values to be kept in cache memory for faster access, if no cache is specified then default is two.
ORDER|NO-ORDER are the keyword used to specify the sequence values to be created on demand. This clause placed significant role if sequence are used to create time-stamp value otherwise for generating unique number order does not have any role.
After sequence is created , then unique values can be accessed with sequence name.nextval and it can be used in a table as follow:
INSERT INTO TABLE_NAME VALUE (sequencename.nextval,.....,.......);
Example:
Create a sequence to generate unique numbers starting with 4 digit and ending with 5 digit number. After sequence reaches to max value it should be wrapped by taking a default cache.
Syntax:
CREATE SEQUENCE unique_no
START WITH 1000
MAXVALUE 99999
CYCLE.
Comments
Post a Comment