A candidate key is a minimal set of one or more attributes whose values uniquely identify each tuple in a relation. In database fundamentals, a key qualifies as a candidate only when it satisfies two properties at once: uniqueness, meaning no two rows share the same value combination, and irreducibility, meaning no attribute can be removed without losing uniqueness.
Every relational table has at least one candidate key, and many have several. Designers choose one of them as the primary key, while the remaining ones are called alternate keys and are usually enforced with unique constraints. In databases built on the relational model, candidate keys are the foundation of normalization, referential integrity and correct query results.
The two defining properties of a candidate key
A set of attributes is a candidate key exactly when it is unique and irreducible. Uniqueness means that for every legal state of the table, no two rows agree on all attributes of the set. Irreducibility, also called minimality, means that every proper subset of the set fails the uniqueness test.
Both properties are statements about all valid states of the data, not about the rows that happen to exist today. A column that contains no duplicates in a sample of 500 customers is not automatically a candidate key; it becomes one only if the business rules guarantee that duplicates can never occur. This distinction separates semantic design from data profiling.
The table below shows how the two properties classify sets of attributes in a hypothetical employee table with the columns employee_id, email, national_id and department.
| Attribute set | Unique | Minimal | Classification |
|---|---|---|---|
| {employee_id} | Yes | Yes | Candidate key |
| {email} | Yes | Yes | Candidate key |
| {employee_id, department} | Yes | No | Superkey only |
| {department} | No | Not applicable | Not a key |
| {national_id} | Yes, if mandatory | Yes | Candidate key under that rule |
Candidate keys among other key types
Candidate keys sit in the middle of a hierarchy of key concepts. Every candidate key is a superkey, one candidate key becomes the primary key, and a candidate key with more than one attribute is a composite key. Confusing these terms leads to imprecise designs, so the distinctions are worth stating exactly.
| Key type | Definition | Relation to candidate key |
|---|---|---|
| Superkey | Any attribute set that is unique | A candidate key is a minimal superkey |
| Primary key | The candidate key chosen as the main identifier | Selected from the candidate keys |
| Alternate key | A candidate key not chosen as primary | The remaining candidate keys |
| Composite key | A key made of two or more attributes | A candidate key may be composite |
| Foreign key | Attributes referencing another table’s key | Usually references a candidate key |
| Surrogate key | A system-generated identifier without meaning | Becomes an added candidate key |
The SQL standard allows a foreign key to reference any column set declared UNIQUE, not only the primary key. In practice this means an alternate key, such as an email address or an ISO country code, can act as the target of referential integrity when it is the more natural identifier for the referencing table.
Finding candidate keys with functional dependencies
Candidate keys are derived formally from functional dependencies, written X → Y, which state that each value of X determines exactly one value of Y. A set of attributes K is a candidate key when its attribute closure contains every attribute of the relation and no proper subset of K has that property.
- List all functional dependencies that the business rules guarantee.
- Identify attributes that never appear on the right-hand side; they must belong to every candidate key.
- Compute the closure of that core set and check whether it covers all attributes.
- If it does not, add other attributes one at a time and recompute the closure.
- Discard any result that contains a smaller set already found to be a key.
Consider a relation enrollment(student_id, course_id, semester, grade, instructor) with the dependencies {student_id, course_id, semester} → grade and {course_id, semester} → instructor. Neither student_id, course_id nor semester appears on the right side, so all three are required, and their closure covers every attribute. The only candidate key is the composite set {student_id, course_id, semester}. Finding all candidate keys is computationally hard in the worst case, because a relation with n attributes can have exponentially many keys, but real schemas are small enough for manual or tool-assisted analysis.
Why candidate keys matter for normalization and integrity
Normal forms are defined in terms of all candidate keys, not only the primary key, so missing a candidate key can make a design look normalized when it is not. An attribute that belongs to any candidate key is called a prime attribute, and that classification drives the tests for the second and third normal forms.
| Normal form | Rule involving candidate keys | Anomaly it prevents |
|---|---|---|
| 2NF | No non-prime attribute depends on part of a candidate key | Repeated data in composite-key tables |
| 3NF | Non-prime attributes depend only on candidate keys | Update anomalies from transitive dependencies |
| BCNF | Every determinant is a superkey | Anomalies from overlapping candidate keys |
Enforcement matters as much as identification. A candidate key that exists only in a design document provides no protection against duplicates. Each alternate key should be declared with a UNIQUE constraint, which most database management systems implement with a unique index that also speeds up lookups on those columns.
Common mistakes when working with candidate keys
Most errors with candidate keys come from treating observed data as a rule or from assuming that values never change. The table below lists frequent mistakes, their typical symptom and the corrective practice.
| Mistake | Typical symptom | Fix |
|---|---|---|
| Inferring keys from current data | Duplicate insert fails months later | Derive keys from business rules |
| Declaring only the primary key | Duplicate emails or codes appear | Add UNIQUE constraints on alternate keys |
| Choosing a volatile natural key as primary | Costly cascading updates | Use a stable surrogate as primary key |
| Including extra columns in a key | Weak uniqueness guarantees | Reduce the set to a minimal key |
| Ignoring NULL in unique columns | Several rows with missing values | Add NOT NULL where the key is mandatory |
The NULL issue deserves attention. In most SQL implementations, a UNIQUE constraint treats NULL values as distinct, so several rows may hold NULL in a supposedly unique column. A true candidate key in the relational sense should therefore be declared NOT NULL, or the uniqueness guarantee covers only the rows where a value exists.
A plain-language summary
Imagine trying to find one specific person in a large office. A name is not enough, because two people can share it. An employee badge number or a work email, however, points to exactly one person. Each of these is a candidate key: a piece of information that always identifies exactly one record, with nothing unnecessary added.
A table can have several such identifiers. The database designer picks one as the official identifier, the primary key, and still protects the others so that they stay unique as well.
An example from practice
A regional library system designs a member table with the columns member_id, card_number, email, full_name and birth_date. During analysis, the team confirms three candidate keys: the generated member_id, the printed library card_number, and the email address, which the sign-up process requires to be unique. The combination of full_name and birth_date is rejected because two members could legitimately share both.
The designers choose member_id as the primary key because card numbers are reissued when a card is lost and emails change. They declare card_number and email as NOT NULL with UNIQUE constraints. Within the first year, the constraint on email blocks 37 duplicate registrations created by a faulty import script, and no loans are ever linked to the wrong member.