Surrogate Keys: Stable IDs for Unstable Data
A surrogate key is a meaningless, system-generated ID that never changes, unlike a 'natural' key (like an email) which can. Use it for stable joins between tables. The footgun is exposing these internal IDs in public URLs, which leaks data structure.
WHY IT EXISTS: Natural keys, identifiers from the real world like email addresses or product SKUs, seem convenient but have a fatal flaw: they can change. A person changes their email, a company rebrands a product. If that key is used to link tables, changing it requires updating every single reference—a risky and complex operation. Surrogate keys were invented to solve this stability problem.
THE MENTAL MODEL: Think of a surrogate key as a library's internal catalog number for a book. The book's real-world identifier, its ISBN (the natural key), might have multiple versions or even errors. But the sticker with the catalog number F-113.8 is unique within that library, never changes, and is the one true way librarians find the book on the shelf. The number itself means nothing; its stability and uniqueness are its only purpose.
HOW IT WORKS: When a new row is inserted into a table, the database automatically generates a unique value for the surrogate key column. This is typically done in one of two ways: first, using an auto-incrementing integer (1, 2, 3, ...) which is fast and compact, or second, using a Universally Unique Identifier (UUID), a long random string that is unique across all tables and databases. This generated ID becomes the primary key. Other tables then store this ID as a foreign key to create a stable relationship.
WHEN TO USE IT: Use a surrogate key as the primary key for almost any table that stores core business entities, like users, products, or orders. It's the default, safe choice. It decouples your database's internal linking mechanism from the volatile data of the real world. This prevents painful cascading updates and ensures referential integrity remains solid even when business data changes.
WHEN NOT TO USE IT: The main exception is for simple, truly static lookup tables (like a list of US state codes) where the natural key (CA, NY) is guaranteed to be unique and will never change. Another case is a join or association table, which is composed only of foreign keys from other tables; the combination of those foreign keys often serves as a natural composite primary key.
ONE CANONICAL EXAMPLE: A users table has columns for user_id (an auto-incrementing integer), email, and name. The email is a natural key candidate, but users change their emails, so user_id is the surrogate primary key. A separate orders table has an order_id (its own surrogate key) and a user_id column. To find a user's orders, you join on orders.user_id = users.user_id. If a user updates their email, only the users table is touched. All order relationships remain intact because they point to the unchanging user_id.
Read the original → en.wikipedia.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.