Sub topic 16
Second Normal Form
This chapter assumes you can compute attribute closure and find candidate keys from functional dependencies (FDs). It also assumes you know what First Normal Form (1NF) requires. Second Normal Form (2NF) is the first normal form that is defined using keys and FDs together.
The definition
A relation is in 2NF if it is in 1NF and no non-prime attribute is partially dependent on any candidate key.
Three terms need to be exact:
- A prime attribute is an attribute that belongs to at least one candidate key.
- A non-prime attribute is an attribute that belongs to no candidate key.
- A partial dependency is an FD X -> A where X is a proper subset of some candidate key and A is non-prime.
So 2NF forbids one situation. A non-prime attribute must not be determined by only part of a key. If every candidate key has a single attribute, no proper subset can be non-empty, so the relation is automatically in 2NF.
Why partial dependencies cause trouble
Consider an enrollment table with the key {studentid, courseid}:
| student_id | course_id | grade | course_title |
|---|---|---|---|
| 1 | DB101 | A | Databases |
| 2 | DB101 | B | Databases |
| 1 | OS201 | A | Operating Systems |
The FD courseid -> coursetitle holds. The left side is only part of the key, and course_title is non-prime, so this is a partial dependency. The anomalies from the earlier chapter follow directly:
- Redundancy: "Databases" is stored once for every student in the course.
- Update anomaly: renaming the course means changing many rows. Missing one leaves inconsistent data.
- Insertion anomaly: you cannot record a new course until a student enrolls, because student_id is part of the key and cannot be null.
- Deletion anomaly: deleting the last enrollment for a course also deletes the course title.
Testing for 2NF
Follow these steps:
- Find all candidate keys, using attribute closure.
- List the prime attributes. Everything else is non-prime.
- For each candidate key with more than one attribute, check every proper subset. Compute its closure.
- If any closure contains a non-prime attribute, the relation is not in 2NF.
Step 3 is the part learners often skip. A partial dependency may be implied rather than given directly, so you must test closures and not just read the listed FDs.
The following program applies these steps. It reuses the closure function from the earlier chapter.
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(attrs, fds):
attrs = set(attrs)
keys = []
for size in range(1, len(attrs) + 1):
for combo in combinations(sorted(attrs), size):
s = set(combo)
if closure(s, fds) == attrs and not any(k <= s for k in keys):
keys.append(s)
return keys
def partial_dependencies(attrs, fds):
keys = candidate_keys(attrs, fds)
prime = set().union(*keys)
non_prime = set(attrs) - prime
found = []
for key in keys:
for size in range(1, len(key)):
for combo in combinations(sorted(key), size):
determined = closure(set(combo), fds) & non_prime
if determined:
found.append((set(combo), determined))
return keys, found
attrs = {'S', 'C', 'G', 'T'} # student, course, grade, title
fds = [({'S', 'C'}, {'G'}), ({'C'}, {'T'})]
keys, partial = partial_dependencies(attrs, fds)
print(keys) # [{'C', 'S'}]
print(partial) # [({'C'}, {'T'})]The output shows one key, {C, S}, and one partial dependency, C -> T. The relation is not in 2NF.
Worked example with several keys
Take R(A, B, C, D, E) with FDs: AB -> C, B -> D, C -> E.
- Closure of AB: AB gives C, then E, and B gives D. So AB+ = ABCDE. AB is a key.
- Neither A+ = A nor B+ = BD is the full set, and no other attribute appears only on a right side, so AB is the only candidate key. A and B appear in no right side, so they must be in every key.
- Prime attributes: A, B. Non-prime: C, D, E.
- Check proper subsets of AB. A+ = {A}, which contains no non-prime attribute. B+ = {B, D}, and D is non-prime.
B -> D is a partial dependency, so R is not in 2NF. Note that C -> E is not a partial dependency. Its left side is not a subset of a key. It is a transitive dependency, which is the concern of 3NF.
Decomposing into 2NF
To remove a partial dependency X -> A:
- Remove A from the original relation.
- Create a new relation containing X and every attribute that X determines among the non-prime attributes.
- Keep X in the original relation as a foreign key.
For the example, B -> D gives R1(B, D) and R2(A, B, C, E). In R2 the key is still AB, with AB -> C and C -> E. R2 has no partial dependency, so it is in 2NF. It still has C -> E, so it is not in 3NF.
The decomposition is lossless because the common attribute B is a key of R1. Check this with the test from the decomposition chapter.
Edge cases and common errors
- A relation whose keys are all single attributes is always in 2NF. Do not search for partial dependencies there.
- A dependency from part of a key to a prime attribute does not violate 2NF. The classic definition only restricts non-prime attributes. Such a case is handled by BCNF.
- Dependencies from a non-key attribute to another non-key attribute are not partial dependencies. They are transitive.
- With several candidate keys, check partial dependencies against every key, not just the primary key. An attribute is non-prime only if it is in none of them.
- Some textbooks define 2NF using only the primary key. GATE questions normally use the all-candidate-keys definition. Read the question for which definition it expects.
Where 2NF sits
Every 2NF relation is in 1NF, and every 3NF relation is in 2NF. The next chapter adds the transitive dependency rule.
Exercise
R(P, Q, R, S, T) has FDs PQ -> R, Q -> S, R -> T. Find the candidate keys, say whether R is in 2NF, and if not, give a 2NF decomposition.
Show answer
Closure of PQ: PQ gives R, S, then T, so PQ+ = PQRST. P and Q appear in no right side except Q -> S on the left, so P and Q are in every key. Q is on a left side only, so PQ is the only candidate key. Prime: P, Q. Non-prime: R, S, T. Test proper subsets: P+ = {P}, and Q+ = {Q, S}. S is non-prime, so Q -> S is a partial dependency and R is not in 2NF. Decompose into R1(Q, S) and R2(P, Q, R, T). R2 has key PQ, with PQ -> R and R -> T. It has no partial dependency, so it is in 2NF, though R -> T is a transitive dependency that violates 3NF.
Exercise
R(A, B, C) has FDs AB -> C and C -> B. Is R in 2NF?
Show answer
The candidate keys are AB and AC, so all three attributes are prime. There are no non-prime attributes, so no partial dependency on a non-prime attribute can exist. R is in 2NF. (It is also in 3NF but not BCNF.)