What is an SQL Sequence?
Database systems offer sequence to produce unique numeric values in a series according to defined standards.
The simplest way in MySQL to use sequences is to define a column as AUTO_INCREMENT and leave the rest to MySQL to take care of.
Syntax :
CREATE SEQUENCE sequence-name
START WITH initial-value
INCREMENT BY increment-value
MAXVALUE maximum-value
CYCLE | NOCYCLE;
where,
CREATE SEQUENCE seq_1
start with 0
increment by 1
minvalue 0
maxvalue 99
cycle;
Now let’s insert a value to a Student table using the above-created sequence:
ID | NAME |
1 | Rabecca |
2 | Varun |
Query to insert a record into the Student table:
INSERT INTO Student VALUE(seq_1.nextval, 'John');
Output:
ID | NAME |
1 | Rabecca |
2 | Varun |
3 | John |
Note: If we use nextval, the sequence will continue to grow even if no records are added to the table.
Note: also read about SQL: SET Operations
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…