G27
Self-checkedTier 1 8 min readdbms.candidate-keys

Finding candidate keys

Finding all candidate keys of a relation from its functional dependencies: the closure test for a superkey, attributes that must be in every key and attributes that are in none, the systematic search, the number of keys a schema can have (up to the central binomial coefficient), prime and non-prime attributes, and the common patterns (a cycle of dependencies makes every attribute a key); results checked against the brute-force list of all minimal superkeys.

Why it exists

Candidate keys decide the normal form of a schema (which attributes are prime), the superkeys the number of which GATE asks to count, and the way a table is stored and indexed. The question "find all candidate keys of RR with the dependencies FF" appears in almost every paper.

Prerequisites

You need attribute closures and implication (functional dependencies) and the notions of superkey and candidate key (relational model and keys).

Precise definitions

For a schema RR with dependencies FF:

  • K⊆RK \subseteq R is a superkey iff K+=RK^+ = R.
  • KK is a candidate key iff it is a superkey and no proper subset of KK is a superkey (it is minimal).
  • An attribute is prime if it belongs to some candidate key, otherwise non-prime.

How it works

Two quick rules

Look at where each attribute occurs in the dependencies:

  1. An attribute that appears on no right side can never be derived: it is in every candidate key.
  2. An attribute that appears only on right sides (never on a left side) is derived from others and is useless for deriving anything: it is in no candidate key.

Attributes of the first kind form the core. If core+=R\text{core}^+ = R, the core is the only candidate key. Otherwise add attributes from the "middle" group (those on both sides) one at a time, or in groups of increasing size, until the closure is all of RR; keep only minimal sets.

The procedure

  1. Compute the core (attributes on no right side). Compute core+\text{core}^+.
  2. If it is RR: the single candidate key is the core. Stop.
  3. Otherwise try the core plus one of the remaining candidate attributes; then plus two; and so on. A set is a candidate key if its closure is RR and it contains no smaller candidate key found earlier.
  4. The sets found are all the candidate keys. The prime attributes are the union of the keys.

Examples, found with this procedure and compared with the list of all minimal superkeys:

Finding candidate keys: start

Computed by a script, not typed
schemain every keyin no keycandidate keysprime attributes
R(A, B, C, D, E), F = { A → B, BC → E, DE → A }
R(A, B, C), F = { AB → C, C → B }
R(A, B, C, D), F = { A → B, B → C, C → D, D → A }
R(A, B, C, D), F = { AB → C, C → D, D → A }
R(A, B, C, D, E, G), F = { AB → C, C → D, D → E, G → A }

Step 0

Attributes that appear on no right side must belong to every candidate key; attributes that appear only on right sides belong to none. The candidate keys are then the smallest sets, built from the first group, whose closure is all of R. The answers are found by the algorithm and re-derived by listing all minimal superkeys.

0 / 5
computed by script: dependencies, closures, keys and decompositions are computed by scripts/db_fd.py. python3 scripts/gen-db3-traces.py

For R(A,B,C,D,E)R(A, B, C, D, E) with F={A→B, BC→E, DE→A}F = \{A \to B,\ BC \to E,\ DE \to A\}: CC and DD appear on no right side, so they are in every key; CD+=CDCD^+ = CD is not enough. Add one more attribute: AA gives ACD+=ABCDEACD^+ = ABCDE (key), BB gives BCD+BCD^+: BC→EBC \to E then DE→ADE \to A, all of RR (key), EE gives CDE+CDE^+: DE→ADE \to A, A→BA \to B (key). Three candidate keys, ACDACD, BCDBCD, CDECDE; every attribute is prime.

For R(A,B,C)R(A, B, C) with {AB→C, C→B}\{AB \to C,\ C \to B\}: AA is in every key (AA is on no right side). A+=AA^+ = A. Add BB: AB+=ABCAB^+ = ABC (key). Add CC: AC+=ABCAC^+ = ABC (key). Keys ABAB and ACAC; BB and CC are prime although C→BC \to B has a non-key left side (this matters for 3NF).

A cycle of dependencies

A→BA \to B, B→CB \to C, C→DC \to D, D→AD \to A: every attribute determines all the others around the cycle, so AA, BB, CC and DD are each a candidate key. Cycles are the typical way to get many keys.

How many candidate keys can there be

No candidate key contains another (they form an antichain of subsets of RR), so by Sperner's theorem (quoted) a schema with nn attributes has at most (n⌊n/2⌋)\binom{n}{\lfloor n/2 \rfloor} candidate keys. The bound is reached: for R(A,B,C,D)R(A, B, C, D) with the six dependencies AB→CDAB \to CD, AC→BDAC \to BD, AD→BCAD \to BC, BC→ADBC \to AD, BD→ACBD \to AC, CD→ABCD \to AB the candidate keys are exactly the six pairs, (42)=6\binom{4}{2} = 6 (checked by program).

Counting superkeys

Once the candidate keys are known, count superkeys by inclusion–exclusion over the keys (relational model and keys): with the single key ACDACD among five attributes there are 25−3=42^{5-3} = 4 superkeys; with the three keys ACDACD, BCDBCD, CDECDE the superkeys are the sets that contain at least one of them.

Checking a proposed key

To check that KK is a candidate key: (1) K+=RK^+ = R; (2) for every attribute a∈Ka \in K, (K−{a})+≠R(K - \{a\})^+ \ne R. Checking minimality needs only the subsets with one attribute removed: a superkey contained in KK is contained in some K−{a}K - \{a\}, and every superset of a superkey is a superkey, so if any proper subset of KK were a superkey, some K−{a}K - \{a\} would be one.

Worked examples

EX 1One key

Easy

R(A,B,C,D)R(A, B, C, D), F={A→B, B→C}F = \{A \to B,\ B \to C\}. AA and DD appear on no right side: core ADAD. AD+=ABCDAD^+ = ABCD: the only candidate key is ADAD. Prime attributes A,DA, D; non-prime B,CB, C.

EX 2A cycle

Easy

R(A,B,C)R(A, B, C), F={A→B, B→C, C→A}F = \{A \to B,\ B \to C,\ C \to A\}. Every one-attribute set has closure ABCABC: candidate keys AA, BB, CC. All attributes are prime.

EX 3Keys from a longer chain

Medium

R(A,B,C,D,E,G)R(A, B, C, D, E, G), F={AB→C, C→D, D→E, G→A}F = \{AB \to C,\ C \to D,\ D \to E,\ G \to A\}. BB and GG appear on no right side: core BGBG. EE appears only on right sides: in no key. BG+BG^+: G→AG \to A, then AB→CAB \to C, C→DC \to D, D→ED \to E: all attributes. The only candidate key is BGBG.

EX 4Counting superkeys with several keys

Medium

R(A,B,C)R(A, B, C) with keys ABAB and ACAC: the superkeys are the sets containing ABAB (ABAB, ABCABC) or ACAC (ACAC, ABCABC): ABAB, ACAC, ABCABC, three superkeys. Inclusion–exclusion: 23−2+23−2−23−3=2+2−1=32^{3-2} + 2^{3-2} - 2^{3-3} = 2 + 2 - 1 = 3.

EX 5Typical questions

GATE level

(a) "The number of candidate keys of R(A,B,C,D)R(A, B, C, D) with F={A→B, B→C, C→D, D→A}F = \{A \to B,\ B \to C,\ C \to D,\ D \to A\}:" 44. (b) "An attribute that appears on no right side:" is in every candidate key. (c) "The maximum number of candidate keys of a relation with 5 attributes:" (52)=10\binom{5}{2} = 10. (d) "A relation with the single candidate key KK of size kk on nn attributes has how many superkeys?" 2n−k2^{n-k}. (e) "X+=RX^+ = R and some proper subset of XX also has closure RR: is XX a candidate key?" No (not minimal). (f) "Is the primary key always unique among candidate keys?" The designer picks one; all candidate keys have equal rights.

Cheat box

Cheat box

  • Superkey: K+=RK^+ = R. Candidate key: minimal superkey. Prime: in some candidate key.
  • On no right side: in every key (the core). Only on right sides: in no key.
  • If core+=R\text{core}^+ = R the core is the only key; else add attributes of increasing number.
  • Cycle A→B→⋯→AA \to B \to \cdots \to A: each attribute is a key.
  • At most (n⌊n/2⌋)\binom{n}{\lfloor n/2 \rfloor} keys; one key of size kk gives 2n−k2^{n-k} superkeys.

Traps and GATE patterns

Connections

Prime attributes and superkeys decide the normal form (normal forms); the closure is described on the previous page.

PYQs

9 previous year questions from your book. I re-solved each one myself (8 by running or simulating, 1 by reasoning only); the book's key is shown next to my answer. All PYQs

year not read yetQ3 · page 369 of the book
Consider a relational database schema with a relation R(A,
B, C, D). If {A, B} and {A, C} are the only two candidate
keys of the relation R, then the number of super keys of
relation R is ______ (answer in integer)
GATE 2022 · NATQ11 · page 370 of the book
Consider a relation R(A,B,C,D,E) with the following three
functional dependencies.

AB→C; BC→D; C→E

The number of super keys in the relation R is...
GATE 2016 · Set 1 · MCQQ21 · page 372 of the book
Which of the following is NOT a super key in a relational
schema with attributes V, W, X, Y ,Z and primary key
VY?
  • (a)VXYZ
  • (b)VWXZ
  • (c)VWXY
  • (d)VWXYZ
GATE 2014 · Set 3 · MCQ · 1 markQ24 · page 372 of the book
A prime attribute of a relation scheme R is an attribute that
appears:
  • (a)in all candidate keys of R,
  • (b)in some candidate key of R
  • (c)in a foreign key of R.
  • (d)only in the primary key of R. DATABASE MANAGEMENT SYSTEM
GATE 2014 · Set 1 · MCQ · 1 markQ25 · page 373 of the book
Consider the relation scheme R = (E, F, G, H, I, J, K, L,
M, N) and the set of functional dependencies {{E,F} →
{G}, {F} → {I,J}, {E,H} → {K,L}, {K} → {M}, {L}
→ {N}} on R. What is the key for R?
  • (a){E,F}
  • (b){E,F,H}
  • (c){E,F,H,K,L}
  • (d){E}
GATE 2014 · Set 2 · NATQ26 · page 373 of the book
Given an instance of the STUDENTS relation as shown
below.

For (StudentName, StudentAge) to be the key for

this instance, the value X should NOT be equal to______
GATE 2013 · MCQ · 2 marksQ27 · page 373 of the book
Relation R has eight attributes ABCDEFGH. Fields of R
contain only atomic values.

F = {CH → G, A → BC, B → CFH, E → A, F → EG}
is a set of functional dependencies (FDs) so that F+ is
exactly the set of FDs that hold for R.

How many candidate keys does the relation R have?
  • (a)3
  • (b)4
  • (c)5
  • (d)6
GATE 2006 · MCQ · 2 marksQ37 · page 375 of the book
Consider a relation R with five attributes V, W, X, Y,
and Z. The following functional dependencies hold:
VY→
W,
WX
→
Z,
and
ZY
→
V.
Which of the following is a candidate key for R?
  • (a)VXZ
  • (b)VXY
  • (c)VWXY
  • (d)VWXYZ
GATE 2005 · MCQ · 2 marksQ38 · page 375 of the book
Consider a relation schema R = (A, B, C, D, E, H) on
which the following functional dependencies hold:
 { A → B, BC → D, E → C, D → A }.
What are the candidate keys of R?
  • (a)AE, BE
  • (b)AE, BE, DE
  • (c)AEH, BEH, BCH
  • (d)AEH, BEH, DEH

Self-check

Question 1

R(A,B,C,D)R(A, B, C, D), F={AB→C, C→D, D→A}F = \{AB \to C,\ C \to D,\ D \to A\}. Find the candidate keys.

Show answer

BB is in every key. B+=BB^+ = B. Add AA: AB+=ABCDAB^+ = ABCD (key). Add CC: BC+BC^+: C→DC \to D, D→AD \to A: ABCDABCD (key). Add DD: BD+BD^+: D→AD \to A, AB→CAB \to C: ABCDABCD (key). Keys ABAB, BCBC, BDBD.

Question 2

Which attributes are in every candidate key?

Show answer

Those that appear on no right side of any dependency.

Question 3

Can a schema with 4 attributes have 7 candidate keys?

Show answer

No: at most (42)=6\binom{4}{2} = 6.

Question 4

How do you check that KK is a candidate key?

Show answer

K+=RK^+ = R and, for each attribute aa in KK, (K−{a})+≠R(K - \{a\})^+ \ne R.

Revision card

One-screen revision

  • Core = attributes on no right side (in every key); right-only attributes in no key.
  • Grow the core by closure; keep minimal sets.
  • Cycles give many keys; at most (n⌊n/2⌋)\binom{n}{\lfloor n/2 \rfloor}.
  • Prime = in some key; single key of size kk: 2n−k2^{n-k} superkeys.

References

  • ·Silberschatz, Korth, Sudarshan, Database System Concepts (7th edition), Chapter 7, Section 7.4.1 to 7.4.4 (closure of attribute sets and keys)
  • ·Elmasri, Navathe, Fundamentals of Database Systems (7th edition), Chapter 14, Section 14.3 and Chapter 15 (keys and normal forms)

Verification

scripts/enum-checks/db_fd_check.py: the key-finding algorithm of scripts/db_fd.py (attributes on no right side are in every key, attributes only on right sides in none, then growth by closure) was compared with the list of all minimal superkeys (a superkey is a set whose closure is all attributes) on 600 random schemas with three to six attributes and equals it; no candidate key contains another; on four attributes the six dependencies AB -> CD, AC -> BD, AD -> BC, BC -> AD, BD -> AC, CD -> AB give exactly the six keys AB, AC, AD, BC, BD, CD (the maximum C(4, 2)); the cycle A -> B -> C -> D -> A gives four one-attribute keys; the trace tables were recomputed. The semantic correctness of the closure test is checked on the page on functional dependencies. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and re-derived against the textbook definitions and the sample database. No defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never