SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
In the context of relational databases, a sequence is a database object that generates a sequence of numbers. Sequences are typically used to auto-generate unique identifiers for rows within a table, often serving as primary keys.
Sequences provide a way to generate unique numbers without having to rely on locks or other concurrency controls, ensuring that even in a high-concurrency environment, values are generated without conflicts.
Increment Value: The value by which the sequence is incremented. It can be positive (ascending sequence) or negative (descending sequence).
Start with Value: The starting value of the sequence.
Minimum and Maximum Value: The boundaries that the sequence will never cross.
Cycle or No Cycle: Determines whether the sequence starts over from the beginning (or end, for descending sequences) once the maximum (or minimum) is reached.
Cache: Some systems allow caching a certain number of sequence values for performance reasons. When cached, a batch of sequence numbers are reserved in memory, reducing the need for disk I/O.
Oracle:
CREATE SEQUENCE seq_name START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 10000 CACHE 10;
SELECT seq_name.NEXTVAL FROM DUAL;
PostgreSQL:
PostgreSQL uses sequences in conjunction with serial types. When you create a column with a SERIAL
type, PostgreSQL automatically creates a sequence for that column.
Creating a Table with a Serial Type:
CREATE TABLE table_name ( id SERIAL PRIMARY KEY, name VARCHAR (100) );
Fetching the Current Value:
SELECT currval('table_name_id_seq');
SQL Server:
SQL Server introduced sequences in version 2012.
Creating a Sequence:
CREATE SEQUENCE seq_name START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 10000 CACHE 10;
Fetching a Value:
SELECT NEXT VALUE FOR seq_name;
Concurrent Access: One of the major benefits of sequences is their ability to generate unique numbers in a multi-user environment without the need for locks.
Gaps: Sequences can produce gaps (missing numbers in the sequence). This can happen due to rollbacks, server crashes, or cached values that haven't been used. It's important for developers to understand that sequences guarantee uniqueness but not a gap-free series.
Performance: Using caching with sequences can improve performance, especially in systems that frequently request new sequence values.
Sequences offer a reliable method to auto-generate unique identifiers in a database. However, the exact implementation details and features can vary among different database management systems, so always refer to the documentation specific to your DBMS when working with sequences.
How to create sequences in SQL:
CREATE SEQUENCE my_sequence START WITH 1 INCREMENT BY 1 MAXVALUE 1000 CYCLE;
Using sequences for auto-incrementing values:
CREATE TABLE my_table ( id INT DEFAULT NEXT VALUE FOR my_sequence, name VARCHAR(255) );
Altering and dropping sequences in SQL:
ALTER SEQUENCE my_sequence INCREMENT BY 2; DROP SEQUENCE my_sequence;
Sequences and concurrency control in SQL:
-- In a transaction BEGIN INSERT INTO my_table (id, name) VALUES (NEXT VALUE FOR my_sequence, 'John'); COMMIT;
Using sequences in INSERT and UPDATE statements:
INSERT INTO my_table (id, name) VALUES (NEXT VALUE FOR my_sequence, 'Jane'); UPDATE my_table SET id = NEXT VALUE FOR my_sequence WHERE name = 'Jane';
Generating unique values with sequences in SQL:
INSERT INTO my_table (id, name) VALUES (NEXT VALUE FOR my_sequence, 'Alice');
Sequences and identity columns in SQL:
-- Using identity column CREATE TABLE identity_table ( id INT IDENTITY(1,1), name VARCHAR(255) );
Handling gaps in sequence values:
-- Sequences may have gaps INSERT INTO my_table (id, name) VALUES (NEXT VALUE FOR my_sequence, 'Bob'); ROLLBACK;
Sequences vs. identity columns vs. auto-increment:
-- Using auto-increment (MySQL) CREATE TABLE auto_increment_table ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) );
Advanced features of sequences in SQL:
CREATE SEQUENCE advanced_sequence START WITH 100 INCREMENT BY 5 MAXVALUE 1000 MINVALUE 0 CYCLE CACHE 10 ORDER;
Sequences and transaction control in SQL:
BEGIN TRANSACTION; INSERT INTO my_table (id, name) VALUES (NEXT VALUE FOR my_sequence, 'Charlie'); COMMIT;
Sequences and data integrity in SQL:
CREATE TABLE person ( id INT PRIMARY KEY DEFAULT NEXT VALUE FOR person_sequence, name VARCHAR(255) );