Introduction
The relational model stores all data as relations: tables whose rows are tuples of atomic values. It was designed so that queries could be expressed as operations on sets, independent of how the rows are laid out on disk. Relational algebra, tuple calculus, SQL and normalisation all assume these definitions.
Keys are the centre of the model. A key is the set of attributes that identifies a tuple; it is what foreign keys point to, what normal forms are defined against, and what an index is usually built on. The two kinds of result are counting superkeys and candidate keys, and the difference between "unique in this instance" and "unique in every legal instance".
Prerequisites
- Sets: subsets, Cartesian product, power set and inclusion-exclusion, used to count superkeys.
Core concepts
| Term | Meaning |
|---|---|
| Domain | A set of atomic values an attribute may take, for example the integers 1 to 100 or strings of length at most 20. |
| Relation schema | A name with a list of attributes; is the degree (arity). |
| Relation instance | A finite set of tuples, each tuple an element of ; the number of tuples is the cardinality. |
| Tuple | One row: a function from attributes to values. |
| Database schema | The set of relation schemas plus integrity constraints; a database instance assigns an instance to each schema. |
| NULL | A special marker for "unknown" or "not applicable"; not a value of any domain. |
Properties of a relation in the formal model.
- A relation is a set: no two tuples are identical, and the order of tuples carries no meaning.
- With named attributes, the order of attributes carries no meaning either.
- Values are atomic (first normal form): no lists or nested tables in a cell.
SQL tables differ: a table without a key can hold duplicate rows (a multiset, or bag), and SELECT keeps duplicates unless DISTINCT is written. See SQL: Data Definition, Data Manipulation and Basic Queries.
Keys
Let be a set of attributes.
- Superkey. is a superkey if no two distinct tuples of any legal instance agree on all attributes of . The full attribute set is always a superkey (a relation has no duplicate tuples).
- Candidate key. A minimal superkey: is a superkey and no proper subset of is.
- Primary key. The candidate key chosen by the designer to identify tuples; it may not be NULL. The other candidate keys are alternate keys.
- Prime attribute. An attribute that belongs to at least one candidate key. All others are non-prime.
- Foreign key. A set of attributes of that refers to the primary key (or another candidate key) of : every non-NULL value of in must appear as a key value in . Details in Integrity Constraints.
- Composite key. A key with more than one attribute. A surrogate key is an artificial single attribute (a serial number) introduced instead of a long natural key.
Instance versus schema. A key is a property of the schema, meaning of all legal instances. A particular instance can show that is not a key (two tuples agree on ) but can never prove that is a key.

How it works: finding keys
When the constraints are given as functional dependencies, is a superkey exactly when its attribute closure is all of (closure is taught in Functional Dependencies, Closure and Canonical Cover). Two quick rules prune the search:
- An attribute that never appears on the right side of any dependency belongs to every candidate key: nothing can determine it.
- An attribute that appears only on the right side, never on the left, belongs to no candidate key: it is determined by the others and never helps determine anything.
Start from the attributes of rule 1. If their closure is , they form the only candidate key. Otherwise add the "both sides" attributes in all combinations, smallest first, and keep the minimal sets whose closure is .
Algorithms and pseudocode
CANDIDATE-KEYS(R, F)
core <- attributes of R not on any right side of F
rest <- attributes on both a left and a right side of F
keys <- empty list
for k <- 0 to |rest|
for each subset S of rest with |S| = k
X <- core union S
if CLOSURE(X, F) = R and no key in keys is a subset of X then add X to keys
return keysThe loop is exponential in in the worst case, which is unavoidable: the number of candidate keys itself can be exponential (see Maximum number of candidate keys in Mathematical explanation). Each closure costs with the simple repeated-scan algorithm (linear in the size of with the counter-based version).
Mathematical explanation
Counting superkeys. Let . Every superset of a superkey is a superkey, so the superkeys are exactly the sets that contain at least one candidate key.
- One candidate key with : the other attributes are free, giving superkeys.
- Two candidate keys : by inclusion-exclusion, .
- In general, the number of superkeys is , computed by inclusion-exclusion over the candidate keys.
Maximum number of candidate keys. Candidate keys form an antichain under (no candidate key contains another). By Sperner's theorem an antichain of subsets of an -set has at most members, and that bound is reached (make every -subset a key). For this is 6.
Worked examples
Example 1. with the single candidate key : superkeys.
Example 2. with candidate keys and : superkeys (Figure 1). Prime attributes: , .
Example 3. with candidate keys and : superkeys.
Example 4. with candidate keys and : superkeys. They are .
Example 5: keys from dependencies. with , , .
- and never appear on a right side, so every key contains . , not a key.
- Add one attribute: , , . All three are keys.
- Every larger candidate contains one of these, so the candidate keys are , , , and every attribute is prime.
- Superkeys: the sets containing and at least one of , , : .
Example 6: what an instance can tell you. Instance of : , , , . Sets with no repeated value combination in this instance: , , , . So , and alone are certainly not keys, and , , may be keys; the instance cannot decide which.
Example 7: a cycle of dependencies. with , , .
- appears on no right side, so every key contains ; is not a key.
- , and determine one another: .
- Candidate keys: , , . Every attribute is prime.
- Superkeys: together with any nonempty subset of : .
- If the cycle closes through as well (, , , ), every single attribute is a key: four candidate keys and superkeys (every nonempty subset).
Example 8: three candidate keys. with candidate keys , and . Let be the set of supersets of within , so .
- Single terms: , , .
- Pairwise intersections are the supersets of the unions: , , , each of size .
- Triple intersection : size .
- Superkeys: . Of these, are superkeys that are not candidate keys.
The pairwise terms use the union of the two keys, not their intersection. With two single-attribute keys and in attributes the count is , which is 12 for as in Example 2.
Important properties and observations
- Superset closure. Any superset of a superkey is a superkey; any subset of a non-superkey is a non-superkey.
- Candidate keys may differ in size. and can both be candidate keys of the same relation; "minimal" means no proper subset is a superkey, not "smallest size".
- Primary key versus candidate key. Every primary key is a candidate key; PRIMARY KEY automatically makes all its attributes NOT NULL. UNIQUE alone does not; other columns may separately be declared NOT NULL.
- Foreign keys may be NULL and may repeat; they need not be part of a key of their own table.
- A relation always has at least one candidate key, because is a superkey and some subset of it is minimal.
Common misconceptions.
- "The number of superkeys with candidate keys and is ." No: sets containing both are counted twice; subtract .
- "If a column has distinct values in the given table, it is a key." Only for that instance; a key is a schema constraint.
- "Prime attributes are the primary-key attributes." Prime means "in some candidate key", not only the chosen primary key.
GATE examination perspective
- Superkey counts. Given candidate keys, count superkeys with inclusion-exclusion over the sets that contain each key.
- Candidate key enumeration. Given functional dependencies, use the "never on a right side" attributes as the core and grow from there.
- Instance questions. "Which of the following can be a key of this relation instance?" Eliminate sets with repeated value combinations; the rest are only possible keys.
- Maximum candidate keys. Consider an -attribute relation: .
- Definitions. Relation as a set (no duplicates) versus SQL table as a multiset; degree versus cardinality.
Summary
Sources
- A. Silberschatz, H. F. Korth, S. Sudarshan, Database System Concepts, 7th ed., McGraw-Hill, 2019 (2020 copyright), 2.1 Structure of Relational Databases, 2.3 Keys.
- R. Elmasri, S. B. Navathe, Fundamentals of Database Systems, 7th ed., Pearson, 5.1 Relational Model Concepts, 5.2 Relational Model Constraints and Relational Database Schemas.