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

TermMeaning
DomainA set of atomic values an attribute may take, for example the integers 1 to 100 or strings of length at most 20.
Relation schema R(A1,…,An)R(A_1, \ldots, A_n)A name with a list of attributes; nn is the degree (arity).
Relation instance r(R)r(R)A finite set of tuples, each tuple an element of D1×⋯×DnD_1 \times \cdots \times D_n; the number of tuples is the cardinality.
TupleOne row: a function from attributes to values.
Database schemaThe set of relation schemas plus integrity constraints; a database instance assigns an instance to each schema.
NULLA 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 K⊆RK \subseteq R be a set of attributes.

  • Superkey. KK is a superkey if no two distinct tuples of any legal instance agree on all attributes of KK. The full attribute set RR is always a superkey (a relation has no duplicate tuples).
  • Candidate key. A minimal superkey: KK is a superkey and no proper subset of KK 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 FF of r1r_1 that refers to the primary key (or another candidate key) of r2r_2: every non-NULL value of FF in r1r_1 must appear as a key value in r2r_2. 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 KK is not a key (two tuples agree on KK) but can never prove that KK is a key.

Nested regions over the 16 attribute subsets of R(A, B, C, D) with candidate keys A and B: the 12 superkeys contain the 2 candidate keys A and B, of which A is the primary key and B the alternate key; the other 4 subsets are not superkeys
Figure 1. Superkeys, candidate keys and the primary key of R(A,B,C,D)R(A, B, C, D) when AA and BB are the candidate keys: 12 of the 16 attribute subsets are superkeys.

How it works: finding keys

When the constraints are given as functional dependencies, KK is a superkey exactly when its attribute closure K+K^+ is all of RR (closure is taught in Functional Dependencies, Closure and Canonical Cover). Two quick rules prune the search:

  1. An attribute that never appears on the right side of any dependency belongs to every candidate key: nothing can determine it.
  2. 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 RR, 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 RR.

Algorithms and pseudocode

Text
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 keys

The loop is exponential in ∣rest∣|rest| 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 O(∣F∣⋅∣R∣2)O(|F| \cdot |R|^2) with the simple repeated-scan algorithm (linear in the size of FF with the counter-based version).

Mathematical explanation

Counting superkeys. Let n=∣R∣n = |R|. 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 KK with ∣K∣=k|K| = k: the other n−kn - k attributes are free, giving 2n−k2^{n-k} superkeys.
  • Two candidate keys K1,K2K_1, K_2: by inclusion-exclusion, 2n−∣K1∣+2n−∣K2∣−2n−∣K1∪K2∣2^{n - |K_1|} + 2^{n - |K_2|} - 2^{n - |K_1 \cup K_2|}.
  • In general, the number of superkeys is ∣⋃i{X:X⊇Ki}∣\left| \bigcup_i \{ X : X \supseteq K_i \} \right|, computed by inclusion-exclusion over the candidate keys.

Maximum number of candidate keys. Candidate keys form an antichain under ⊆\subseteq (no candidate key contains another). By Sperner's theorem an antichain of subsets of an nn-set has at most (n⌊n/2⌋)\binom{n}{\lfloor n/2 \rfloor} members, and that bound is reached (make every ⌊n/2⌋\lfloor n/2 \rfloor-subset a key). For n=4n = 4 this is 6.

Worked examples

Example 1. R(A,B,C,D)R(A, B, C, D) with the single candidate key AA: 23=82^{3} = 8 superkeys.

Example 2. R(A,B,C,D)R(A, B, C, D) with candidate keys AA and BB: 23+23−22=122^3 + 2^3 - 2^2 = 12 superkeys (Figure 1). Prime attributes: AA, BB.

Example 3. R(A,B,C,D,E)R(A, B, C, D, E) with candidate keys ABAB and CDCD: 23+23−21=142^3 + 2^3 - 2^1 = 14 superkeys.

Example 4. R(A,B,C,D)R(A, B, C, D) with candidate keys ABAB and BCBC: 22+22−21=62^2 + 2^2 - 2^1 = 6 superkeys. They are AB,ABC,ABD,ABCD,BC,BCDAB, ABC, ABD, ABCD, BC, BCD.

Example 5: keys from dependencies. R(A,B,C,D,E)R(A, B, C, D, E) with A→BA \to B, BC→EBC \to E, ED→AED \to A.

  • CC and DD never appear on a right side, so every key contains CDCD. (CD)+=CD(CD)^+ = CD, not a key.
  • Add one attribute: (ACD)+=ABCDE(ACD)^+ = ABCDE, (BCD)+=BCDEA(BCD)^+ = BCDEA, (CDE)+=CDEAB(CDE)^+ = CDEAB. All three are keys.
  • Every larger candidate contains one of these, so the candidate keys are ACDACD, BCDBCD, CDECDE, and every attribute is prime.
  • Superkeys: the sets containing CDCD and at least one of AA, BB, EE: 23−1=72^3 - 1 = 7.

Example 6: what an instance can tell you. Instance of R(A,B,C)R(A, B, C): (1,x,p)(1, x, p), (2,x,q)(2, x, q), (3,y,p)(3, y, p), (1,y,q)(1, y, q). Sets with no repeated value combination in this instance: ABAB, ACAC, BCBC, ABCABC. So AA, BB and CC alone are certainly not keys, and ABAB, ACAC, BCBC may be keys; the instance cannot decide which.

Example 7: a cycle of dependencies. R(A,B,C,D)R(A, B, C, D) with A→BA \to B, B→CB \to C, C→AC \to A.

  • DD appears on no right side, so every key contains DD; D+=DD^+ = D is not a key.
  • AA, BB and CC determine one another: (AD)+=(BD)+=(CD)+=ABCD(AD)^+ = (BD)^+ = (CD)^+ = ABCD.
  • Candidate keys: ADAD, BDBD, CDCD. Every attribute is prime.
  • Superkeys: DD together with any nonempty subset of {A,B,C}\{A, B, C\}: 23−1=72^3 - 1 = 7.
  • If the cycle closes through DD as well (A→BA \to B, B→CB \to C, C→DC \to D, D→AD \to A), every single attribute is a key: four candidate keys and 24−1=152^4 - 1 = 15 superkeys (every nonempty subset).

Example 8: three candidate keys. R(A,B,C,D,E)R(A, B, C, D, E) with candidate keys AA, BCBC and CDCD. Let SKS_K be the set of supersets of KK within ABCDEABCDE, so ∣SK∣=25−∣K∣|S_K| = 2^{5 - |K|}.

  • Single terms: ∣SA∣=16|S_A| = 16, ∣SBC∣=8|S_{BC}| = 8, ∣SCD∣=8|S_{CD}| = 8.
  • Pairwise intersections are the supersets of the unions: SA∩SBC=SABCS_A \cap S_{BC} = S_{ABC}, SA∩SCD=SACDS_A \cap S_{CD} = S_{ACD}, SBC∩SCD=SBCDS_{BC} \cap S_{CD} = S_{BCD}, each of size 22=42^2 = 4.
  • Triple intersection SABCDS_{ABCD}: size 22.
  • Superkeys: 16+8+8−4−4−4+2=2216 + 8 + 8 - 4 - 4 - 4 + 2 = 22. Of these, 22−3=1922 - 3 = 19 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 AA and BB in nn attributes the count is 2n−1+2n−1−2n−2=3⋅2n−22^{n-1} + 2^{n-1} - 2^{n-2} = 3 \cdot 2^{n-2}, which is 12 for n=4n = 4 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. AA and BCBC 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 RR is a superkey and some subset of it is minimal.

Common misconceptions.

  • "The number of superkeys with candidate keys AA and BB is 2⋅2n−12 \cdot 2^{n-1}." No: sets containing both are counted twice; subtract 2n−22^{n-2}.
  • "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 nn-attribute relation: (n⌊n/2⌋)\binom{n}{\lfloor n/2 \rfloor}.
  • Definitions. Relation as a set (no duplicates) versus SQL table as a multiset; degree versus cardinality.

Summary

Sources

  1. 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.
  2. 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.

Next steps