đĸ A SEQUENCE is a database object that generates a sequence of unique numeric values. Unlike AUTO_INCREMENT or IDENTITY, a sequence is independent of any specific table and can be shared across multiple tables or applications.
đ What is a Sequence?
A sequence produces numbers in a defined order whenever its next value is requested. It is commonly used to generate primary key values, invoice numbers, order numbers, and other unique identifiers.
Information
đ¯ Why Use Sequences?
Sequences provide a flexible way to generate unique numbers without being tied to a single table.
- đ Generate unique identifiers automatically.
- đ Share number generation across multiple tables.
- đ Customize starting values and increments.
- đ Improve flexibility compared to table-specific auto-numbering.
- đ Support enterprise applications requiring centralized numbering.
đ Basic Syntax
Create a Sequence
CREATE SEQUENCE sequence_name
START WITH 1
INCREMENT BY 1;đ Sequence Components
| Option | Description |
|---|---|
| START WITH | Specifies the first generated value. |
| INCREMENT BY | Determines how much the value increases each time. |
| MINVALUE | Defines the minimum value. |
| MAXVALUE | Defines the maximum value. |
| CYCLE | Restarts from the beginning after reaching the maximum value. |
| CACHE | Stores generated values in memory to improve performance. |
đĄ Create a Simple Sequence
Student Sequence
CREATE SEQUENCE StudentSeq
START WITH 1001
INCREMENT BY 1;The first generated value is 1001, followed by 1002, 1003, and so on.
âļī¸ Get the Next Sequence Value
The syntax for retrieving the next sequence value varies between database systems.
Oracle
SELECT StudentSeq.NEXTVAL
FROM dual;PostgreSQL
SELECT nextval('StudentSeq');SQL Server
SELECT NEXT VALUE FOR StudentSeq;â Use a Sequence During INSERT
Insert Using a Sequence (Oracle Example)
INSERT INTO Students
(StudentID, Name, Department)
VALUES
(StudentSeq.NEXTVAL, 'Alice', 'Computer Science');Each new row automatically receives the next available sequence value.
âī¸ Create a Custom Sequence
Advanced Sequence
CREATE SEQUENCE InvoiceSeq
START WITH 10000
INCREMENT BY 10
MINVALUE 10000
MAXVALUE 99990
CYCLE;This sequence begins at 10000, increases by 10, and starts again from the minimum value after reaching the maximum value because of the CYCLE option.
đ Sample Sequence Output
| Request | Generated Value |
|---|---|
| First | 1001 |
| Second | 1002 |
| Third | 1003 |
| Fourth | 1004 |
âī¸ Sequence vs IDENTITY vs AUTO_INCREMENT
| Feature | Sequence | IDENTITY | AUTO_INCREMENT |
|---|---|---|---|
| Independent Object | â Yes | â No | â No |
| Shared Across Tables | â Yes | â No | â No |
| Automatic Value Generation | â Yes | â Yes | â Yes |
| Highly Configurable | â Yes | Limited | Limited |
đŧ Real-World Example
A logistics company uses one centralized sequence to generate shipment IDs across multiple warehouse tables. This guarantees that every shipment number is unique regardless of which table stores the record.
Shipment Sequence
CREATE SEQUENCE ShipmentSeq
START WITH 50000
INCREMENT BY 1;đī¸ Database Compatibility
| Database System | Sequence Support |
|---|---|
| Oracle | â Fully supported. |
| PostgreSQL | â Fully supported. |
| SQL Server | â Supported (SQL Server 2012 and later). |
| MySQL | Limited native support; AUTO_INCREMENT is commonly used instead. |
| SQLite | Does not provide standalone sequence objects. |
â ī¸ Common Mistakes
- â Assuming sequence values are always gap-free.
- â Forgetting that requesting the next value advances the sequence.
- â Using one sequence for unrelated numbering schemes without planning.
- â Assuming identical syntax across all database systems.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ A sequence is an independent database object.
- đ It generates unique numeric values on demand.
- đ Multiple tables can share the same sequence.
- đ Sequences support configurable starting values, increments, limits, and cycling.
- đ Syntax differs among database systems.
- đ Sequence values are intended to be unique, not necessarily consecutive.