Sub topic 18
Boyce-Codd Normal Form
This chapter assumes you can compute attribute closures, find candidate keys from functional dependencies (FDs), and define 3NF. It builds on all three.
The definition
A relation R is in Boyce-Codd Normal Form (BCNF) if, for every non-trivial FD X → Y that holds in R, X is a superkey of R. A trivial FD is one where Y ⊆ X.
The rule has no exception for prime attributes. 3NF allows X → A when X is not a superkey, as long as A is a prime attribute (part of some candidate key). BCNF removes that allowance. Every BCNF relation is therefore in 3NF, but not the reverse.
| Normal form | A non-trivial FD X → A is allowed if |
|---|---|
| 3NF | X is a superkey, or A is prime |
| BCNF | X is a superkey |
When 3NF is not BCNF
A relation can be in 3NF but not BCNF only when it has overlapping candidate keys. Take the example from the plan:
R(A, B, C) with FDs: AB -> C, C -> B
Candidate keys: AB, AC
Prime attributes: A, B, C
3NF: yes (in C -> B, B is prime)
BCNF: no (C is not a superkey)
BCNF decomposition: R1(C, B), R2(A, C); the FD AB -> C is not preserved.Check the keys. The closure of AC is {A, C}, then C → B adds B, then AB → C changes nothing, so AC determines every attribute. Neither A nor C alone is a key, so AB and AC are the candidate keys. The FD C → B has a left side C whose closure is {B, C}, which is not all of R. C is not a superkey, so BCNF fails.
The relation still has redundancy. Whenever the same C value appears in several rows, B is stored repeatedly.
Testing for BCNF
To test BCNF properly, check every attribute set X, not only the left sides of the FDs you were given. A violation can come from an FD that is implied but not listed. For each X, compute X⁺ and look for a violation: X⁺ contains attributes outside X, but X⁺ is not all of R.
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 bcnf_violations(schema, fds):
schema = set(schema)
found = []
for size in range(1, len(schema)):
for subset in combinations(sorted(schema), size):
x = set(subset)
c = closure(x, fds) & schema
if c > x and c != schema:
found.append((sorted(x), sorted(c - x)))
return found
fds = [({'A', 'B'}, {'C'}), ({'C'}, {'B'})]
print(bcnf_violations({'A', 'B', 'C'}, fds)) # [(['C'], ['B'])]An empty list means the relation is in BCNF. The function tests all subsets, so it takes time exponential in the number of attributes. That is fine for exam-sized relations.
The test also works on a sub-relation. Pass the sub-schema together with the original FD set. Intersecting the closure with the sub-schema gives exactly the FDs that hold in the projection.
The BCNF decomposition algorithm
Decomposition repeats until no violation remains.
For the earlier example, the violating FD is C → B. This gives R1(C, B) and R2(A, C). R1 has the FD C → B with C a key, and R2 has only trivial FDs, so both are in BCNF.
Lossless but maybe not dependency preserving
The split on a violating FD X → Y is always lossless. R1 ∩ R2 = X, and X determines all of R1, so the common attributes form a key of R1. This satisfies the lossless-join condition from the previous chapter.
Dependency preservation can fail. In the example, the original FD AB → C needs A, B and C together. After the split, no single table contains all three, so the FD cannot be checked inside one table. Joining R1 and R2 would be required to enforce it. The closure of AB under the FDs preserved in R1 and R2 (only C → B) is just {A, B}, so AB → C is lost.
This is the central trade-off with 3NF:
| Property | BCNF decomposition | 3NF synthesis (from a minimal cover) |
|---|---|---|
| Lossless join | Always | Always (a key relation is added if needed) |
| Dependency preserving | Not guaranteed | Always |
| Redundancy from FDs | None | May remain |
Common errors
- Checking only the given FDs. A violation can come from an implied FD, so test X⁺ for the relevant sets.
- Treating "all attributes prime" as proof of BCNF. It proves only 3NF. The example has all attributes prime and still fails BCNF.
- Forgetting that a relation with only two attributes is always in BCNF.
- Decomposing on a violation and then not re-testing the new relations. Each part must be tested again.
- Assuming BCNF decomposition is unique. Choosing a different violation first can give a different result.
Exercise
R(A, B, C, D) has FDs A → B, B → C, C → D. Find the candidate key and the highest normal form, then give a BCNF decomposition.
Show answer
A⁺ = {A, B, C, D}, so A is the only candidate key. B → C has a left side B that is not a superkey, and C is non-prime, so 3NF fails. The relation is in 2NF, because the key is a single attribute and no partial dependency is possible. Decompose on B → C: R1(B, C) and R2(A, B, D). In R2, the FD B → D is implied (B → C → D), but B⁺ restricted to R2 is {B, D}, so decompose again into R21(B, D) and R22(A, B). The final result is (B, C), (B, D), (A, B). Every determinant is a key in its own table and every original FD is preserved.
Exercise
R(A, B, C) has FDs AB → C and C → A. Is R in 3NF? Is it in BCNF?
Show answer
The keys are AB and BC. BC works because C → A gives A and then AB → C is not needed: BC⁺ = {B, C, A}. All attributes are prime, so R is in 3NF. In C → A, C is not a superkey, since C⁺ = {C, A}. R is therefore not in BCNF.