0% found this document useful (0 votes)
1 views6 pages

SQL Sequences

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

SQL Sequences

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

SQL Sequences

Sequence is a set of integers 1, 2, 3, … that are generated by some database systems to produce
unique values on demand.
 A sequence is a user defined schema bound object that generates a sequence of numeric
values.
 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.
Difference between Identity & Sequence in SQL Server
Point-1:

The IDENTITY property is tied to a SEQUENCE object is defined by the user


particular table and cannot be and can be shared by multiple tables
shared among multiple tables. since is it is not tied to any table.

Table1- PK with
Table1
IDENTITY Property
Table2
Table2
X Table3
Table3
Sequences
Difference between Identity & Sequence in SQL Server
Point 2:
 To generate the next IDENTITY value, a new row has to be inserted into the table.
 On the other hand, the next VALUE for a SEQUENCE object can simply be generated using the
NEXT VALUE FOR clause with the sequence object.

 Identity Column
Create Table test1
(
ID INT IDENTITY(1,1),
FName Varchar(100)
)
Insert Into Test(FName) Values(‘Smith’)
 Sequence
INSERT into students VALUES( NEXT VALUE FOR sequence_1,‘Smith');
Difference between Identity & Sequence in SQL Server

 Point 3
• The value for the IDENTITY property cannot be reset to its initial value.
• the value for the SEQUENCE object can be reset.
 Point 4
• A maximum value cannot be set for the IDENTITY property.
• the maximum value for a SEQUENCE object can be defined.
Anatomy of Sequqnece

CREATE SEQUENCE sequence_name Name of the sequence.

START WITH initial_value sequence starting value


INCREMENT BY increment_value
MINVALUE minimum value sequence increment step value
MAXVALUE maximum value
Minimum value of the sequence
CYCLE|NOCYCLE ;
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.
Anatomy of Sequqnece

Create Table test2


(
IDINT ,
FName Varchar(100)
)
Insert Into test2(id,FName) Values(next value for seq2,'Smith');
select * from test2

You might also like