Using database sequences
To yield sequential numbers, databases such as PostgreSQL, SQL Server, and Oracle rely on sequences. A database sequence lives independently from tables – it can be associated with the primary key and non-primary key columns, it can be auto-generated (as in the case of PostgreSQL (BIG
)SERIAL
), it can be used across multiple tables, it can have independent permissions, it can have cycles, it can increment values in its own transactions to guarantee uniqueness across transactions using it, we can explicitly alter its values by setting minimum, maximum, increment, and current values, and so on.
For instance, let's consider the following sequence (employee_seq
), defined in our PostgreSQL schema for the employee.employee_number
primary key:
CREATE SEQUENCE "employee_seq" START 100000 INCREMENT 10 MINVALUE 100000 MAXVALUE 10000000 OWNED BY "employee...