Sub topic 17
Third Normal Form
This chapter assumes you can compute attribute closures, find candidate keys from a set of functional dependencies (FDs), and recognize 2NF violations from the previous chapters. It builds directly on those skills.
What 3NF Requires
A relation R is in third normal form (3NF) if, for every nontrivial FD X → A that holds in R, at least one of these is true:
- X is a superkey of R.
- A is a prime attribute, meaning A belongs to some candidate key of R.
A nontrivial FD is one where A is not already in X. A nonprime attribute is any attribute that is in no candidate key.
The original textbook phrasing says the same thing differently. R is in 3NF if it is in 2NF and no nonprime attribute is transitively dependent on a key. A transitive dependency is a chain key → Y → A, where Y is not itself a key and A is nonprime. The first definition is the one to use in GATE problems because it works mechanically, one FD at a time.
Why the Prime Exception Exists
Condition 2 makes 3NF weaker than BCNF. The exception guarantees that every relation has a 3NF decomposition that is lossless and preserves dependencies. BCNF cannot always promise the dependency-preserving part. You will see the cost of this in the BCNF chapter.
Worked Example: A Transitive Dependency
Consider Student(studentid, name, deptid, dept_name) with these FDs:
- studentid → name, deptid
- deptid → deptname
The only candidate key is studentid, so the prime attributes are just {studentid}. Check each FD:
| FD | LHS a superkey? | RHS prime? | Verdict |
|---|---|---|---|
| student_id → name | yes | no | ok |
| student_id → dept_id | yes | no | ok |
| dept_id → dept_name | no | no | violates 3NF |
The chain studentid → deptid → dept_name is a transitive dependency. The relation is in 2NF, because the key has a single attribute and so no partial dependency is possible. It is not in 3NF. The department name is repeated for every student in that department, which causes update anomalies.
The fix is to split on the offending FD:
- Student(studentid, name, deptid)
- Department(deptid, deptname)
Both relations are now in 3NF. The join on dept_id restores the original, and both FDs are preserved.
Testing 3NF in Code
The program below applies the definition directly. It reuses the closure idea from earlier chapters, finds candidate keys by searching attribute subsets in increasing size, and then checks each FD.
from itertools import combinations
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(schema, fds):
schema = set(schema)
keys = []
for size in range(1, len(schema) + 1):
for combo in combinations(sorted(schema), size):
s = set(combo)
if any(k <= s for k in keys):
continue # contains a smaller key, so not minimal
if closure(s, fds) >= schema:
keys.append(s)
return keys
def is_3nf(schema, fds):
schema = set(schema)
keys = candidate_keys(schema, fds)
prime = set().union(*keys)
for lhs, rhs in fds:
for a in rhs - lhs:
is_superkey = closure(lhs, fds) >= schema
if not is_superkey and a not in prime:
return False, (lhs, a)
return True, None
# AB -> C, C -> B: in 3NF because B is prime
print(is_3nf('ABC', [({'A', 'B'}, {'C'}), ({'C'}, {'B'})]))
# A -> B, B -> C: B -> C violates 3NF
print(is_3nf('ABC', [({'A'}, {'B'}), ({'B'}, {'C'})]))The first call prints (True, None). The second prints (False, ({'B'}, 'C')).
Decision Procedure
Run this check for every FD. The relation is in 3NF only if all FDs pass.
3NF Where BCNF Fails
The spine's example is R(A, B, C) with AB → C and C → B. The candidate keys are AB and AC, so every attribute is prime. The FD C → B has a non-superkey left side, but B is prime, so 3NF accepts it. BCNF rejects it, because C is not a superkey. The BCNF decomposition R1(C, B), R2(A, C) cannot enforce AB → C within a single table. This is the trade-off in practice. 3NF tolerates a little redundancy to keep dependencies checkable without joins.
Synthesis: Getting to 3NF
The 3NF synthesis algorithm always yields a lossless, dependency-preserving decomposition:
- Compute a minimal cover of the FDs.
- For each FD X → A in the cover, create a relation with the attributes X ∪ {A}. You can merge FDs that share the same left side into one relation.
- If no relation contains a candidate key of the original R, add one relation holding a candidate key.
- Remove any relation whose attributes are contained in another relation.
Common Errors
- Testing only the primary key. 3NF depends on all candidate keys. Missing a key makes you wrongly label attributes nonprime.
- Forgetting that a prime attribute can appear on the right side of a bad-looking FD. Check condition 2 before declaring a violation.
- Treating the transitive-dependency rule as "any chain of FDs." The chain must start at a key and end at a nonprime attribute through a non-key.
- Skipping step 3 of synthesis. Without the key relation, the result may be dependency preserving but not lossless.
Exercise
R(A, B, C, D) has FDs A → B, B → C, and C → D... wait, use these exact FDs: AB → C and C → D. Find the candidate keys and decide whether R is in 3NF.
Show answer
The attributes A and B appear on no right side except through the FDs, so any key must contain A and B. The closure of AB is {A, B, C, D}, so AB is the only candidate key. The prime attributes are A and B. The FD C → D has a left side C that is not a superkey, and D is not prime, so it violates 3NF. R is not in 3NF. Decomposing gives R1(C, D) and R2(A, B, C).