Sub topic 13
Finding Candidate Keys from FDs
This chapter assumes you can compute an attribute closure (the previous chapter) and know the definitions of superkey and candidate key. Here you use closure to find every candidate key of a relation from its functional dependencies (FDs). GATE CS asks for this constantly, and it is the first step in deciding a normal form.
What you are looking for
A superkey is a set of attributes whose closure is all attributes of the relation R. A candidate key is a minimal superkey: if you remove any attribute from it, it stops being a superkey. A relation can have several candidate keys. An attribute that appears in at least one candidate key is prime. All other attributes are non-prime.
Checking a single set is easy: compute its closure and compare it with the full attribute set. The hard part is searching efficiently. Trying every subset costs 2^n closures, so we use structure in the FDs to cut the search down.
Classify attributes by where they appear
Look at every FD and sort each attribute into one of four groups.
| Group | Where the attribute appears | Consequence |
|---|---|---|
| Never on any side | In no FD at all | Must be in every key |
| Left side only | Only on left sides of FDs | Must be in every key |
| Right side only | Only on right sides of FDs | In no key |
| Both sides | On both left and right sides | Maybe in a key. Test it. |
The reasoning is simple. An attribute that is never derived by any FD can only be obtained by being in the key itself. An attribute that appears only on right sides can always be derived from others, so including it would make a set non-minimal, unless it is needed to derive something else, which cannot happen because it is on no left side.
The procedure
- Classify the attributes as in the table.
- Let core be the set of attributes that appear in no FD or only on left sides. Every key contains core.
- Compute the closure of core. If it equals R, then core is the only candidate key. Stop.
- Otherwise, add attributes from the "both sides" group, first one at a time, then in pairs, and so on. Compute the closure of core plus the added attributes each time.
- Whenever a set reaches all of R, it is a key if no smaller key found so far is contained in it. Do not extend a set that is already a key, since any superset of a key is not minimal.
- Stop when all combinations of the current size are exhausted and no larger set can avoid containing a known key.
Worked example 1: a single key
Let R(A, B, C, D, E) with FDs A -> B, B -> C, CD -> E.
- A appears only on a left side. B and C appear on both sides. D appears only on a left side. E appears only on a right side.
- Core = {A, D}.
- Closure of {A, D}: start with {A, D}. A -> B gives {A, B, D}. B -> C gives {A, B, C, D}. CD -> E gives {A, B, C, D, E}. This is all of R.
So {A, D} is a key. It is the only candidate key, because every key must contain A and D, and {A, D} is already minimal. This matches the Python example in the Topic Spine.
You can check it with code:
def closure(attrs, fds):
result = set(attrs)
changed = True
while changed:
changed = False
for lhs, rhs in fds:
if lhs <= result and not rhs <= result:
result |= rhs
changed = True
return result
def candidate_keys(relation, fds):
from itertools import combinations
relation = set(relation)
attrs = sorted(relation)
keys = []
for size in range(1, len(attrs) + 1):
for combo in combinations(attrs, size):
s = set(combo)
if any(k <= s for k in keys):
continue
if closure(s, fds) == relation:
keys.append(s)
return keys
fds = [({'A'}, {'B'}), ({'B'}, {'C'}), ({'C', 'D'}, {'E'})]
for k in candidate_keys('ABCDE', fds):
print(sorted(k)) # ['A', 'D']The code tries subsets in increasing size and skips any set that contains a known key, which is exactly the minimality rule. It is brute force, so it suits small examples and checking your hand work, not large schemas.
Worked example 2: several keys
Let R(A, B, C) with FDs AB -> C, C -> B. This is the relation from the Topic Spine.
- A appears only on a left side, so core = {A}.
- Closure of {A} is {A}. Not all of R.
- Add B: closure of {A, B}: AB -> C gives {A, B, C}. This is a key.
- Add C: closure of {A, C}: C -> B gives {A, B, C}. This is a key.
Candidate keys are AB and AC. All three attributes are prime. Notice that B appears on the right side of C -> B, yet B is still in a key. That is why attributes on both sides must be tested and not discarded.
Worked example 3: keys from a cycle
Let R(A, B, C, D) with FDs A -> B, B -> C, C -> A.
- D is in no FD, so D is in every key. Core = {D}.
- Closure of {D} is {D}.
- Try {A, D}: A -> B, B -> C give {A, B, C, D}. Key.
- Try {B, D}: B -> C, C -> A, A -> B give everything. Key.
- Try {C, D}: C -> A, A -> B give everything. Key.
Candidate keys are AD, BD, and CD. When FDs form a cycle among attributes, each member of the cycle can stand in for the others, which multiplies the number of keys.
Common errors
- Forgetting attributes that appear in no FD. They must be in every key. Examples that leave out an attribute like D above lead to wrong answers.
- Stopping at the first key. A relation can have several. Find all of them before you classify attributes as prime or non-prime.
- Listing non-minimal sets as keys. ABD is a superkey in example 3 but not a candidate key, since AD is already a key inside it.
- Dropping right-side attributes from consideration. An attribute on the right of some FD can still be in a key if it is the left side of an FD that is needed, as B in example 2.
- Computing closure with only the original FDs. Use all FDs, and apply them repeatedly until nothing changes.
- Counting superkeys by mistake. If a question asks for the number of superkeys, count all supersets of candidate keys, using inclusion-exclusion when keys overlap.
Counting superkeys
With n attributes and one candidate key of size k, the number of superkeys is 2^(n-k). In example 1, n = 5 and k = 2, so there are 2^3 = 8 superkeys. With several keys, count the union of their superset families. In example 2, supersets of AB are AB and ABC. Supersets of AC are AC and ABC. The union is {AB, AC, ABC}, so there are 3 superkeys.
Exercise
R(A, B, C, D, E) has FDs A -> B, BC -> D, D -> E, E -> C. Find all candidate keys.
Show answer
A is on a left side only, so it is in every key. Closure of {A} is {A, B}, which is not all of R. Try {A, C}: A -> B gives {A, B, C}, BC -> D gives {A, B, C, D}, D -> E gives all of R. So AC is a key. Try {A, D}: D -> E gives {A, B, D, E}, E -> C gives all of R. So AD is a key. Try {A, E}: E -> C gives {A, B, C, E}, BC -> D gives all of R. So AE is a key. Candidate keys are AC, AD, and AE.
Exercise
R(A, B, C, D) has FDs AB -> C, C -> D, D -> A. How many candidate keys does it have, and how many superkeys?
Show answer
B is on a left side only, so B is in every key. Closure of {B} is {B}. Try {A, B}: AB -> C, C -> D gives all of R. Key. Try {C, B}: C -> D, D -> A gives {A, B, C, D}. Key. Try {D, B}: D -> A gives {A, B, D}, then AB -> C gives all of R. Key. So the candidate keys are AB, BC, and BD, which is 3 keys. Superkeys are the sets containing at least one of them. Sets containing B number 2^3 = 8, namely B with any subset of {A, C, D}. Of these, only B alone fails, because its closure is just {B}. So there are 7 superkeys.