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 with the dependencies " 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 with dependencies :
- is a superkey iff .
- is a candidate key iff it is a superkey and no proper subset of 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:
- An attribute that appears on no right side can never be derived: it is in every candidate key.
- 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 , 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 ; keep only minimal sets.
The procedure
- Compute the core (attributes on no right side). Compute .
- If it is : the single candidate key is the core. Stop.
- 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 and it contains no smaller candidate key found earlier.
- 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| schema | in every key | in no key | candidate keys | prime 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.
For with : and appear on no right side, so they are in every key; is not enough. Add one more attribute: gives (key), gives : then , all of (key), gives : , (key). Three candidate keys, , , ; every attribute is prime.
For with : is in every key ( is on no right side). . Add : (key). Add : (key). Keys and ; and are prime although has a non-key left side (this matters for 3NF).
A cycle of dependencies
, , , : every attribute determines all the others around the cycle, so , , and 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 ), so by Sperner's theorem (quoted) a schema with attributes has at most candidate keys. The bound is reached: for with the six dependencies , , , , , the candidate keys are exactly the six pairs, (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 among five attributes there are superkeys; with the three keys , , the superkeys are the sets that contain at least one of them.
Checking a proposed key
To check that is a candidate key: (1) ; (2) for every attribute , . Checking minimality needs only the subsets with one attribute removed: a superkey contained in is contained in some , and every superset of a superkey is a superkey, so if any proper subset of were a superkey, some would be one.
Worked examples
EX 1One key
Easy, . and appear on no right side: core . : the only candidate key is . Prime attributes ; non-prime .
EX 2A cycle
Easy, . Every one-attribute set has closure : candidate keys , , . All attributes are prime.
EX 3Keys from a longer chain
Medium, . and appear on no right side: core . appears only on right sides: in no key. : , then , , : all attributes. The only candidate key is .
EX 4Counting superkeys with several keys
Mediumwith keys and : the superkeys are the sets containing (, ) or (, ): , , , three superkeys. Inclusion–exclusion: .
EX 5Typical questions
GATE level(a) "The number of candidate keys of with :" . (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:" . (d) "A relation with the single candidate key of size on attributes has how many superkeys?" . (e) " and some proper subset of also has closure : is 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: . 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 the core is the only key; else add attributes of increasing number.
- Cycle : each attribute is a key.
- At most keys; one key of size gives superkeys.
Traps and GATE patterns
Connections
Builds on
Across subjects
No cross-subject links yet.
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
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)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...
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
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
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}
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______
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
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
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
, . Find the candidate keys.
Show answerHide
is in every key. . Add : (key). Add : : , : (key). Add : : , : (key). Keys , , .
Question 2
Which attributes are in every candidate key?
Show answerHide
Those that appear on no right side of any dependency.
Question 3
Can a schema with 4 attributes have 7 candidate keys?
Show answerHide
No: at most .
Question 4
How do you check that is a candidate key?
Show answerHide
and, for each attribute in , .
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 .
- Prime = in some key; single key of size : 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
My notes (saved in this browser)