Blog is developed for free online learning of Oracle Applications, SQL, PLSQL

Learn SQL Sequence


SQL Sequence :-

We will be learning SQL Sequences in this post,

Sequence: A sequence is a database item that generates a sequence of integers. We
create the sequence using the CREATE SEQUENCE statement.

Sequence
Sequence

Syntax: Create Sequence Sequence_name
[Start with Start_num]
[Increment by increment_num]
[ {MAXVALUE max_num | NOMAXVALUE} ]
[ {MINVALUE min_num | NOMINVALUE} ]
[ {CYCLE | NOCYCLE} ]
[ {ORDER | NOORDER} ];
Ex: SQL> Create Sequence medha_seq_0016
Start with 100 Increment by 1;
Sequence Created.
Using the Sequence:
Ex: SQL> Select medha_seq_0016.currval ”Currval”,
medha_seq_0016.nextval “Nextval”
From
Dual;
Output: Currval Nextval
--------- ----------
101 101
Modifying the Sequence: If we want to modify the sequence by using the ALTER
SEQUENCE we can do that.
Ex: SQL> Alter Sequence medha_seq_0016
Start with 1000 Increment by 2;
Sequence Altered.
Dropping a Sequence: If we want to drop the sequence then we use this DROP
STATEMENT.
Ex: SQL> Drop Sequence medha_seq_0016;
Sequence Dropped.