Natural and surrogate keys: stable identity and business uniqueness
Overview
A natural key has meaning in the business domain, such as a product code unique within a tenant. A surrogate key is an identifier assigned by the system, independent of that meaning. In the V50 fixture each item has an integer id and a UNIQUE(tenant_id, sku) rule. Load professional-fixture.sql in a fresh SQLite database with foreign keys enabled. All seven lessons share this fixture; the examples do not change it.
First example
SELECT id,tenant_id,sku FROM pro_item ORDER BY id;Observed SQLite result (rows in JSON):
[ [ 101, 1, "P1" ], [ 102, 1, "P2" ], [ 201, 2, "P1" ]]How it works
The first query finds items by their stable identifiers. The second shows that the same SKU may occur in different tenants. A surrogate key does not replace business uniqueness. If the code changes, dependent references to id stay stable; the unique constraint still rejects duplicates within the tenant. A composite natural key may be appropriate when stable and small. Integer identities are compact but not gapless; UUIDs can support distributed generation but are wider and their ordering depends on the version. Neither is authorization or a secret. PostgreSQL identity syntax differs from SQLite INTEGER PRIMARY KEY.
Practical example
SELECT sku,COUNT(*) AS n FROM pro_item GROUP BY sku HAVING COUNT(*)>1 ORDER BY sku;Observed SQLite result (rows in JSON):
[ [ "P1", 2 ]]Practice and mistakes
Practice: in a disposable copy rename SKU P1 of tenant 1 to P3 and verify that item id 101 and its movements are unchanged. Then try P2 and predict the uniqueness error. Choose identifiers based on business stability, reference width and generation needs.