Generate unique sequential values with database sequences, identity columns, and custom patterns.
Sequences provide a way to generate unique sequential numbers independent of tables. They're useful for order numbers, invoice IDs, and other business identifiers.
| Feature | PostgreSQL | MySQL | SQLite | MSSQL | MongoDB |
|---|---|---|---|---|---|
| Sequences | ✅ | ❌ | ❌ | ✅ | ❌ |
| Identity Columns | ✅ | ✅ AUTO_INCREMENT | ✅ AUTOINCREMENT | ✅ IDENTITY | ✅ ObjectId |
| Custom Start/Increment | ✅ | ✅ | ❌ | ✅ | ✅* |
| nextval/currval | ✅ | ❌ | ❌ | ✅ | ✅* |
* MongoDB uses counter collections with findAndModify
Build sequences with the Sequence::builder() API.
Get, set, and use sequence values in queries.
Cross-database utilities for auto-incrementing columns.
MongoDB doesn't have built-in sequences, but you can implement them with atomic findAndModify.
Pre-built patterns for common sequence use cases.
Order numbers like ORD-2024-00001
Invoice numbers that reset each year
Distribute work across workers evenly
Limited inventory with atomic decrement
Configure auto-incrementing primary keys in your schema.
Add sequences via migrations.
Pre-allocate sequence values to reduce database round-trips. Higher cache values improve performance but can leave gaps on restart.
In PostgreSQL, use GENERATED AS IDENTITY (SQL standard) instead of
SERIAL (PostgreSQL-specific) for new tables.
Sequences can have gaps due to rollbacks, cache invalidation, and concurrent access. Never rely on sequential values being contiguous.
Use auto-increment for internal IDs, but separate sequences for business numbers like order numbers that may need resets or prefixes.