SQL SEQUENCES

Sequences are used for on demand unique value generation as integers 1,2,3,4… in some databases as many applications requires each row in a table contain unique value .

Sequences can be in ascending order or descending order and generated at a defined intervals and they restart from top when exceed a maximum value.

Syntax

CREATE SEQUENCE sequence_name
START WITH first_value
INCREMENT BY increment_value
MINVALUE minimum value
MAXVALUE maximum value
CYCLE|NOCYCLE ;

Example

CREATE SEQUENCE seq_1
start with 1
increment by 2
minvalue 0
maxvalue 50
cycle;

This will create a sequence name ‘seq_1’ which will start from 1 and incremented by 2 until it reaches 50 and once it will exceed it will repeat from beginning with 1.

Learn more : SQL Tutorial , SQL Interview Questions