Sql Server

SQL | SEQUENCES

SQL | SEQUENCES

SEQUENCES in Sql Server

A sequence is a list of numbers, in an ordered manner
It is a user-defined schema object that produces a list of numbers in accordance to specified value in SQL server.


CREATE SEQUENCE schema_name.sequence_name  
AS integer_type
START WITH start_value
INCREMENT BY increment_value
MINVALUE min_value
MAXVALUE max_value
CYCLE|NOCYCLE ;



Create a sequence that starts with 1 and is increased by 1.

CREATE SEQUENCE seq_incrementbyone
AS INT
START WITH 1
INCREMENT BY 1
minvalue 0
maxvalue 100
cycle;

--The above query will create a sequence named seq_incrementbyone. The sequence will start from 1 and will be incremented by 1 having maximum value of 100.
--The sequence will repeat itself from the start value after exceeding 100.



Reverse Sequence

CREATE SEQUENCE sequence_2
start with 100
increment by -1
min value 1
max value 100
cycle;

--The above query will create a sequence named sequence_2. The sequence will start from 100 and should be less than or equal to a maximum value and
will be incremented by -1 having a minimum value of 1.



Get Next value from sequence

SELECT NEXT VALUE FOR seq_incrementbyone;



Using a sequence object in a table.

CREATE TABLE staff
(
  sequence_id INT PRIMARY KEY,
  DOJ date NOT NULL
);

INSERT INTO staff(sequence_id, DOJ)
VALUES(NEXT VALUE FOR seq_incrementbyone, CONVERT(DATETIME,'11/22/1975'));



Use Sequence as Default value in Table

CREATE TABLE advisory
(
  id INT PRIMARY KEY DEFAULT (NEXT VALUE FOR seq_incrementbyone),
  DOJ DATE NOT NULL,
  City NVARCHAR(100)
);

INSERT INTO advisory(DOJ, City )
VALUES('2019-05-12', 'Delhi');



Get the detailed information of sequences in SQL server :

SELECT *
FROM sys.sequences;



Key Points Sequence
A sequence is a user-defined schema-bound object that generates a series 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 provide 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 it exceeds max_value.



Different Features of Sequences
A sequence is a database object that generates and produces integer values in sequential order.
It automatically generates the primary key and unique key values.
It may be in ascending or descending order.
It can be used for multiple tables.
Sequence numbers are stored and generated independently of tables.
It saves time by reducing application code.
It is used to generate unique integers.
It is used to create an auto number field.
Useful when you need to create a unique number to act as a primary key.
Oracle provides an object called a Sequence that can generate numeric values. The value generated can have maximum of 38 digits
Provide intervals between numbers.





Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.

Instagram