0% found this document useful (0 votes)
23 views9 pages

Sequence

Uploaded by

s g shrinivas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views9 pages

Sequence

Uploaded by

s g shrinivas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

SEQUENCE

A sequence is a user-defined schema bound object that generates a


sequence of numeric values according to the specification with which
the sequence was created.
A sequence is a user defined schema bound object that generates a
sequence of numeric values.
Sequences are frequently used in many databases because many
applications require each row in a table to contain a unique value and
sequences provides an easy way to generate them.
The sequence of numeric values is generated in an ascending or
descending order at defined intervals and can be configured to restart
when exceeds max_value.
Syntax
CREATE SEQUENCE sequence_name [INCREMENT BY interval] [START WITH
first_number] [MAXVALUE max_value | NOMAXVALUE] [MINVALUE min_value
| NOMINVALUE] [CYCLE | NOCYCLE] [CACHE cache_size | NOCACHE] [ORDER
| NOORDER];
Explanation
sequence_name: Name of the sequence.

initial_value: starting value from where the sequence starts.


Initial_value should be greater than or equal
to minimum value and less than equal to maximum value.
increment_value: Value by which sequence will increment itself.
Increment_value can be positive or negative.
minimum_value: Minimum value of the sequence.
maximum_value: Maximum value of the sequence.
cycle: When sequence reaches its set_limit
it starts from beginning.
nocycle: An exception will be thrown
if sequence exceeds its max_value.
Examples
CREATE SEQUENCE sequence_1
start with 1
increment by 1
minvalue 0
maxvalue 100
cycle;

CREATE SEQUENCE sequence_2


start with 100
increment by -1
minvalue 1
maxvalue 100
cycle;
Using SEQUENCE in tables
create a table named students with columns as id and name.
CREATE TABLE students
(
ID number(10),
NAME char(20)
);
Now insert values into table

INSERT into students VALUES(sequence_1.nextval,'Ramesh');


INSERT into students VALUES(sequence_1.nextval,'Suresh');
Example Contd.,
CREATE SEQUENCE id_seq
INCREMENT BY 10
START WITH 10
MINVALUE 10
MAXVALUE 100
CYCLE
CACHE 2;
SELECT
id_seq.NEXTVAL
FROM
dual;
Code language: SQL (Structured Query Language) (sql)
Here is the output:

NEXTVAL
----------
10
Code language: SQL (Structured Query Language) (sql)
To get the current value of the sequence, you use the CURRVAL pseudo-
column:
SELECT
id_seq.CURRVAL
FROM
dual;
Code language: SQL (Structured Query Language) (sql)
The current value is 10:

CURRVAL
----------
10
Code language: SQL (Structured Query Language) (sql)
This SELECT statement uses the id_seq.NEXTVAL value repeatedly:
SELECT
id_seq.NEXTVAL
FROM
dual
CONNECT BY level <= 9;
Code language: SQL (Structured Query Language) (sql)
Here is the output:

NEXTVAL
----------
20
30
40
50
60
70
80
90
100

9 rows selected

You might also like