Surrogate keys: design them alongside natural keys
Learn when an internally assigned row identifier helps, what it does not guarantee, and how to choose between generated integers and UUIDs.
Surrogate keys: design them alongside natural keys
A surrogate key is an identifier assigned to a row without carrying business meaning. A customer table might use an internal customer_id while managing email as a separate business attribute with its own uniqueness rule. This separates relationships from changes to email or identifier collisions across source systems, but automatic generation alone guarantees neither uniqueness, gaplessness, nor security.
How is it different from a natural key?
A natural key is a value or combination already meaningful to the business, such as an employee number, ISBN, or order_id + line_number within an order. Ask whether it is truly unique, present for every row, and stable for the entity’s lifetime. An email can be unique today yet unsuitable as a durable identifier if it can change or be reused.
A surrogate key has no business meaning: to know which customer customer_id = 8142 represents, follow the database relationships. It is not a public customer number or an authorization token. Preserve required business uniqueness with a separate UNIQUE constraint even when the table also has a surrogate key.
When does it help, and what does it cost?
If a customer changes email, orders can continue to reference the same customer_id. When two source systems both use identifiers such as A-17, an internal key can distinguish their records while source-system name and source key are retained separately. This is also useful in a data warehouse that combines sources or insulates relationships from changes in source data.
The extra identifier needs storage, indexes, and mapping logic. Without a uniqueness constraint on business values, duplicate emails or codes can still enter the table. A stable row identifier does not replace a business duplicate rule. Avoid encoding meaning into a key: changing that meaning can break references and integrations.
Choose between an integer and a UUID
For rows created in one database and referenced internally, a generated integer is compact and simple. PostgreSQL 18 provides GENERATED ... AS IDENTITY; SQL Server provides IDENTITY. If several databases or offline clients must create IDs without a central coordinator, consider UUIDs. PostgreSQL documents UUIDs as 128-bit values designed to make collisions very unlikely across independent generators; collision probability is not zero.
| Criterion | Generated integer | UUID |
|---|---|---|
| Typical generator | Database identity/sequence | Database, client, or other independent generator |
| Multiple systems | Requires coordination of sequence ranges | Better suited to independent generation, but collisions are still theoretically possible |
| Storage/index size | Usually smaller | Larger than an integer |
| Exposure | Sequence can be guessable | Opaque-looking, but does not replace access control |
These are general selection criteria, not a performance benchmark. Real index behavior depends on the database, key-generation method, write pattern, and index design. Measure before claiming a speed advantage. Enforce uniqueness for UUIDs too, and consider integer range and distributed-generation needs for integers.
PostgreSQL 18 example: keep the two key roles separate
This PostgreSQL 18 example is illustrative and was not run:
CREATE TABLE customers (
customer_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
source_system text NOT NULL,
source_customer_id text NOT NULL,
email text NOT NULL,
UNIQUE (source_system, source_customer_id),
UNIQUE (email)
);
CREATE TABLE orders (
order_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id bigint NOT NULL REFERENCES customers(customer_id)
);customer_id is the surrogate primary key for internal references. The pair (source_system, source_customer_id) prevents duplicates within the stated source-key scope, while this example also makes email unique as a separate business rule. That email rule would be wrong for a service where addresses can change or family accounts can share one; constraints must match the actual requirements.
GENERATED ALWAYS restricts supplying an explicit value in ordinary inserts, but the identity property itself does not guarantee uniqueness. PostgreSQL marks an identity column NOT NULL; a PRIMARY KEY or UNIQUE constraint enforces uniqueness. Automatically generated numbers can have gaps, for example after a failed or rolled-back insert. Do not reuse an identity value as an invoice number when legal or business rules require consecutive numbering; design a separate issuing process for the applicable jurisdiction and workflow.
Questions to validate your choice
Is a candidate natural key unique, required, and stable over time, and can constraints enforce those properties?
Are keys generated in one database, or also by multiple nodes or offline clients?
May users see the key? A surrogate ID is neither a secret token nor an authorization check.
Have you documented the difference between the business identifier and internal row identifier?
For source integration or history tracking, do you retain both source system and source key with a clearly defined uniqueness scope?
A surrogate key helps separate internal relationships from business values that can change. Keep business duplicate checks separately, and choose the generator to fit distributed creation, storage, and operational rules.
Sources
[PostgreSQL 18: Identity Columns](https://www.postgresql.org/docs/18/ddl-identity-columns.html)
[PostgreSQL 18: Constraints](https://www.postgresql.org/docs/18/ddl-constraints.html)
[PostgreSQL 18: UUID Type](https://www.postgresql.org/docs/18/datatype-uuid.html)
[PostgreSQL 18: UUID Functions](https://www.postgresql.org/docs/18/functions-uuid.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-ver17)
[Microsoft Learn: Modeling dimension tables in Fabric](https://learn.microsoft.com/fabric/data-warehouse/dimensional-modeling-dimension-tables)
Documentation checked 2026-11-23. SQL is illustrative and was not executed. Identity and UUID details vary by DBMS and version.