Primary Key
A primary key uniquely identifies each row in a relational table, enforcing uniqueness and non-null values.
A primary key is the column or combination of columns that uniquely identifies every row in a relational table. It is one of the most fundamental concepts in relational database design, providing the canonical handle by which a row is referenced.
How It Works
A primary key enforces two constraints: every value must be unique, and no part may be null. A table has at most one primary key, though that key may span multiple columns (a composite key). The database automatically creates a unique index on the primary key, making lookups by key very fast.
Designers choose between:
- Natural keys: existing meaningful data, such as an ISBN or email address.
- Surrogate keys: artificial values with no business meaning, such as an auto-incrementing integer or a UUID. Surrogate keys are usually preferred because they are stable, compact, and never need to change.
Other tables reference a primary key through foreign keys, forming relationships between tables.
Why It Matters
Primary keys are essential to data integrity. They guarantee that each entity is represented once and can be reliably located, updated, and related to other data. Without them, duplicate and ambiguous rows undermine correctness.
Key choice has performance implications: monotonic integer keys keep indexes compact and cache-friendly, while random UUIDs can fragment indexes unless ordered variants (e.g., UUIDv7) are used.
Related Terms
A primary key is referenced by foreign keys, is central to normalization, and is always backed by an index.