Types of Database Keys
Relational Database Keys
Introduction to Keys
A relational database key is a column or combination of columns that uniquely identifies a row in a table. Keys enforce uniqueness and establish relationships between tables. There are many overlapping categories of keys, and a single key may belong to several types simultaneously. Understanding these categories clarifies how databases ensure data integrity and enable reliable joins.
Primary Key
A primary key is a column (or set of columns) that uniquely identifies each row in a table. The database platform enforces uniqueness and no null values are allowed in any primary key attribute.
Uniqueness constraint: Attempting to insert a duplicate primary key value causes the insert to fail.
Non-null requirement: Null is not a value, so it cannot serve as an identifier.
Only one primary key per table is permitted.
Example: In a US States table, the state_code column (e.g., TX, GA) can serve as the primary key because each U.S. state has a unique postal abbreviation that is stable (unlikely to change).
Definition: A primary key is a unique, non-null identifier for a row, chosen from among the candidate keys.
Candidate Key
A candidate key is any column or combination of columns that could serve as the primary key. When designing a table, there may be several attributes that uniquely identify rows; these are the candidates.
Irreducible (minimal): Every attribute in a candidate key is necessary to guarantee uniqueness. Removing any attribute would break uniqueness.
Uniqueness across time: A candidate key must guarantee uniqueness not just for current data but for all possible future additions. A value that happens to be unique today may not remain so indefinitely.
Example: In US States, both state_code and state_name are candidate keys because each state has a unique name and a unique code. However, state_capital is not a candidate key because capitals can change (historically they have) and could become duplicated (e.g., Augusta, Georgia and Augusta, Maine).
Definition: A candidate key is a super key that is irreducible — no subset of its attributes also forms a unique identifier.
Super Key
A super key is any set of attributes that contains a candidate key. Unlike a candidate key, a super key may include extra attributes that are not needed for uniqueness.
Every candidate key is a super key.
A super key that is not a candidate key has redundant attributes that could be removed without breaking uniqueness.
Example: In US States, the combination {state_code, region} is a super key. Because state_code alone already identifies each state, the addition of region is unnecessary. This over-inclusive key is not irreducible, so it is not a candidate key — it is merely a super key.
Definition: A super key is any set of attributes that uniquely identifies a row, possibly containing extra attributes beyond those needed for uniqueness.
Alternate Key
An alternate key is a candidate key that is not chosen as the primary key. Once the primary key is decided, the remaining candidate keys can be explicitly declared as alternate keys to enforce uniqueness on those columns as well.
The database platform will enforce uniqueness on alternate keys if the designer explicitly sets a unique constraint.
This prevents accidental duplicates even on columns that are not the primary key.
Example: If state_code is the primary key, then state_name can be set as an alternate key. With that constraint, the database will reject any attempt to insert a duplicate state name (e.g., two rows with state_name = 'Georgia').
Definition: An alternate key is a candidate key that is not the primary key, but is still enforced as unique by the database.
Foreign Key
A foreign key is a column (or set of columns) in one table that references the primary key (or a candidate key) of another table. Its purpose is to maintain referential integrity between tables.
Every value in the foreign key column must match a value in the referenced primary key column (or be null, if allowed).
This ensures that related data remains consistent across tables.
Example: A Voters table may have a column state_of_voter_registration that is a foreign key to the state_code column in the US States table. This prevents entering an invalid code like QX because no matching row exists in the referenced table.
-- Sample join enabled by foreign key
SELECT *
FROM Voters
JOIN US_States ON Voters.state_of_voter_registration = US_States.state_code;Definition: A foreign key constrains data in a child table to match values in the parent table's primary key, preserving referential integrity.
Surrogate Key and Natural Key
These two types describe the origin of the key values.
Natural Key
A natural key uses an attribute that already exists in the real world to identify rows. Its values have meaning outside the database (e.g., a postal abbreviation, Social Security Number, email address).
Often stable and intuitive to users.
Also called a business key.
Examples: state_code (GA, TX), isbn for books (though limitations exist), email_address, customer number mailed to clients.
Surrogate Key
A surrogate key is an artificial identifier invented by the database designer, typically a system-generated number (e.g., an auto-incrementing integer). Its values have no real-world meaning — they only identify rows within the database context.
Essential when no reliable natural key exists.
Surrogate keys must represent one specific entity — they are not mere row IDs. Two different surrogate key values should never refer to the same real-world object.
Example: Using state_id as an integer primary key (e.g., 25 for Georgia) instead of state_code. The number 25 means nothing outside the database.
Definition: A natural key uses real-world values as identifiers; a surrogate key uses fabricated, system-assigned values that have meaning only within the database.
Choosing Between Surrogate and Natural
Use a natural key when a stable, unique real-world identifier exists (e.g., U.S. state codes, ISBNs — but beware scope limits).
Use a surrogate key when no good natural key exists (e.g., a recipe table) or when the natural key is not stable or has null values (e.g., ISBN for ebooks).
A surrogate key can become a natural key if its values are exposed to the public (e.g., a product code displayed on a website).
Simple Key, Composite Key, and Compound Key
These terms describe the structure of a key — how many attributes it contains.
Simple Key
A simple key consists of exactly one attribute (column). Most primary keys in examples are simple.
Example: state_code as primary key of US States.
Composite Key
A composite key consists of two or more attributes that together uniquely identify a row. Each attribute is part of the key; none alone suffices.
Example: In a Coffee Options table, the combination (coffee_type, cup_size) is a composite key because the same coffee type sold in different cup sizes are distinct options.
Compound Key
A compound key is a type of composite key where at least one of its attributes is a foreign key referencing another table. Not all database practitioners distinguish this term, but some use it for the special case where a junction table's primary key consists entirely of foreign keys.
Example: In a Customer_Account junction table that links customers to accounts, the primary key (customer_id, account_id) has both columns as foreign keys (to the Customer and Account tables respectively). This is a compound key.
Key Type | Number of Attributes | Special Feature |
|---|---|---|
Simple | 1 | Single column |
Composite | ≥2 | Multiple columns together ensure uniqueness |
Compound | ≥2 | Composite where at least one column is a foreign key |
Definition: A composite key uses more than one attribute; a compound key is a composite key that includes one or more foreign keys.
Intelligent Key
An intelligent key is a natural key that is deliberately built from multiple meaningful parts concatenated together. Each part of the key carries semantic information.
Example: A product code CLS-TRN-016 where:
CLS= product category (Classic)TRN= product subcategory (Train set)016= sequential number within that subcategory
Pitfalls of Intelligent Keys
Embedded meaning invites misuse: Users may parse the key to extract category or subcategory information, rather than using dedicated columns. This breaks if the coding scheme changes.
Instability when classification changes: If the company stops using "Classic" vs. "Modern" and switches to "Battery Operated" vs. "Non-Battery Operated", existing product codes become contradictory and may need massive updates.
Best practice: Do not encode business semantics into key values. Store such attributes in separate columns and use a simple or surrogate key for identification.
When Intelligent Keys Are Acceptable
They work well when the components of the key are truly essential and stable — for example, a customer identifier combining date of birth (which never changes) with a sequence number. Even then, an entirely arbitrary identifier might be simpler.
Definition: An intelligent key is a natural key whose value is composed of multiple sub-parts that each carry separate meaning; care is required to avoid fragility and misuse.
Summary of Key Types
All key types described above can coexist in the same key. For instance, state_code in the US States table is a primary key, candidate key, single-key (simple), and a natural key. The Coffee Options key (coffee_type, cup_size) is a primary key, candidate key, composite key, and natural key. Understanding these overlapping categories helps database designers make informed decisions about data integrity, performance, and maintainability.

Ask anything
We've read this whole study set, so questions about it get the best answers.