What Is an Identity Column? What Auto-Generated Numbers Do and Don’t Guarantee
An identity column is configured so the database supplies a sequence value when a new row is inserted. It is commonly used for an internal row ID, but the feature only generates values; it does not by itself guarantee uniqueness, gap-free numbering, or secrecy.
What Is an Identity Column? What Auto-Generated Numbers Do and Don’t Guarantee
An identity column is configured so the database supplies a sequence value when a new row is inserted. It is commonly used for an internal row ID, but the feature only generates values; it does not by itself guarantee uniqueness, gap-free numbering, or secrecy. Enforce any needed property with a separate constraint or business rule.
When and how is a value generated?
An identity column usually takes its next value from an internal sequence or counter when an inserted row omits that column. An application can save the returned value as a key, but the function and syntax for retrieving it depend on the DBMS. An auto-generated number is generally an identifier that increases over time, not a clock recording the exact time or business order of events.
This PostgreSQL 18 example generates an internal ID for orders:
CREATE TABLE orders (
order_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id bigint NOT NULL,
order_date date NOT NULL
);
INSERT INTO orders (customer_id, order_date)
VALUES (42, DATE '2026-11-23')
RETURNING order_id;When order_id is omitted, PostgreSQL uses the attached sequence. RETURNING returns the ID created by this insert. This example is illustrative and was not executed; other DBMSs use different ways to retrieve the value.
Can you supply an ID explicitly?
That depends on the DBMS and column definition. PostgreSQL GENERATED ALWAYS AS IDENTITY normally rejects a value supplied by the user; an explicit INSERT value requires OVERRIDING SYSTEM VALUE. GENERATED BY DEFAULT AS IDENTITY gives a supplied value precedence over the sequence value. SQL Server can allow explicit values by enabling SET IDENTITY_INSERT. MySQL accepts explicit values for an AUTO_INCREMENT column, and those values can affect the next generated number. For imports or restores, check the counter and possible duplicates as well as preserving existing IDs.
Do generated numbers guarantee uniqueness or continuity?
Do not assume they guarantee uniqueness. PostgreSQL documents that an identity column is NOT NULL but does not guarantee uniqueness; a PRIMARY KEY or UNIQUE constraint is needed to reject duplicates. SQL Server likewise requires a unique constraint or index for that guarantee. Express the table’s actual duplicate rule with a constraint, regardless of how values are generated.
Generated numbers also may not be consecutive. Failed inserts, rollbacks, server failures, or caching may consume or skip numbers. With concurrent inserts, ID order is not necessarily transaction commit order or business-event order. Do not use an ordinary identity column directly as an invoice or legally regulated document number when gaps and issuance order have explicit requirements. Design a separate issuing process for the applicable jurisdiction and rules.
How do DBMS implementations differ?
| DBMS syntax | Typical generation | Explicit values and caveats |
|---|---|---|
PostgreSQL 18 GENERATED ... AS IDENTITY | From an attached sequence | ALWAYS normally restricts explicit input; BY DEFAULT permits it. Uniqueness needs a separate constraint. |
SQL Server IDENTITY(seed, increment) | From a starting value and increment | Only one per table. It does not promise uniqueness or consecutive values; explicit input requires IDENTITY_INSERT. |
MySQL 8.4 AUTO_INCREMENT | Automatically numbers an integer column | Values may be explicitly inserted or changed, affecting the next generated value. The column must be first or only in a suitable index. |
These forms do not have identical contracts. Before a migration, check the target version’s documentation for default generation, explicit-value rules, counter behavior after restart or restore, sequence range, and index requirements.
Is an auto-generated ID the same as a primary key?
No. Auto-generation determines how a new value is proposed; a primary key constrains rows to have unique identifiers. A common design declares both IDENTITY and PRIMARY KEY on the column. Adding an identity does not remove the need for UNIQUE constraints on natural keys or business identifiers. An ID is also not an authentication token or proof of authorization, so do not assume that exposing it is safe.
Checks before choosing and operating one
Consider identity or the DBMS’s auto-number feature for an internal row key within one database.
If multiple nodes must create IDs without central coordination, reassess whether a single-counter approach fits.
Enforce required uniqueness with a `PRIMARY KEY` or `UNIQUE` constraint, not just the generation attribute.
For a legally or operationally consecutive number, design a separate numbering process covering gaps, cancellations, retries, concurrent issuance, and audit records.
After importing or restoring explicit IDs, check that the next generated value will not collide with the existing maximum.
Apply access control separately; an exposed ID must not disclose protected data or bypass authorization checks.
Sources and scope
[PostgreSQL 18: Identity Columns](https://www.postgresql.org/docs/18/ddl-identity-columns.html)
[PostgreSQL 18: CREATE TABLE — identity clause](https://www.postgresql.org/docs/18/sql-createtable.html)
[Microsoft Learn: SQL Server IDENTITY property](https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property?view=sql-server-ver15)
[MySQL 8.4: Using AUTO_INCREMENT](https://dev.mysql.com/doc/refman/8.4/en/example-auto-increment.html)
[MySQL 8.4: AUTO_INCREMENT Handling in InnoDB](https://dev.mysql.com/doc/refman/8.4/en/innodb-auto-increment-handling.html)
Documentation checked on 2026-11-23. SQL is illustrative and was not executed. Explicit input, numbering gaps, restart behavior, and key constraints vary by DBMS and version.