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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…