Sub topic 20
Lossless Join and Dependency Preservation
This chapter assumes you can compute attribute closures and find candidate keys from functional dependencies (FDs). It also assumes you know 3NF and BCNF and can project a relation onto a subset of its attributes.
Why decomposition needs a quality check
Normalization splits a relation R into smaller relations R1, R2, ..., Rn. Splitting removes redundancy, but a careless split can damage the data. Two properties decide whether a decomposition is acceptable:
- Lossless join: joining the pieces gives back exactly the original relation. No tuples are lost, and no spurious (extra) tuples appear.
- Dependency preservation: every FD in F can be checked inside a single piece. If it can't, enforcing it needs a join on every update.
Lossless join is mandatory. Dependency preservation is desirable and sometimes impossible to get together with BCNF.
Lossless join
The binary test
For a split of R into R1 and R2, the decomposition is lossless if and only if at least one of these holds in F+ (the closure of F):
- (R1 ∩ R2) → R1
- (R1 ∩ R2) → R2
In words, the common attributes must be a superkey of at least one piece. The shared attributes act as the join column, and a key on one side means each row on the other side matches at most one row. That is why no spurious tuples appear.
The chase (tableau) test
For three or more pieces, use the chase:
- Build one row per piece Ri and one column per attribute of R.
- In row i, write the distinguished symbol
ain each column whose attribute is in Ri. Write a unique symbolbin every other column. - For each FD X → Y, find rows that agree on all columns of X. Make them agree on each column of Y. If one of the two symbols is distinguished, replace the other with it everywhere in that column. Otherwise merge the two
bsymbols. - Repeat until nothing changes. The decomposition is lossless if some row is all distinguished symbols.
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 is_lossless(attrs, schemas, fds):
attrs = sorted(attrs)
rows = [{a: ('a', a) if a in s else ('b', i, a) for a in attrs}
for i, s in enumerate(schemas)]
changed = True
while changed:
changed = False
for lhs, rhs in fds:
for r1 in rows:
for r2 in rows:
if all(r1[a] == r2[a] for a in lhs):
for a in rhs:
if r1[a] != r2[a]:
keep, drop = min(r1[a], r2[a]), max(r1[a], r2[a])
for row in rows:
if row[a] == drop:
row[a] = keep
changed = True
return any(all(row[a][0] == 'a' for a in attrs) for row in rows)
R = {'A', 'B', 'C'}
F = [({'A', 'B'}, {'C'}), ({'C'}, {'B'})]
print(is_lossless(R, [{'C', 'B'}, {'A', 'C'}], F)) # True
print(is_lossless(R, [{'A', 'B'}, {'B', 'C'}], F)) # FalseThe first split shares C, and C → B makes C a key of R1(C, B), so it is lossless. The second split shares only B, and B determines nothing, so the join produces spurious tuples.
Dependency preservation
The projection of F onto Ri, written Fi, is the set of FDs in F+ whose attributes all lie in Ri. The decomposition preserves dependencies if the closure of F1 ∪ ... ∪ F_n equals F+. It is enough to show that every FD in F is implied by the union.
Computing F+ is exponential, so use the restricted-closure test. For each FD X → Y in F:
- Start with Z = X.
- Repeat: for each piece Ri, set Z = Z ∪ ((Z ∩ Ri)+ ∩ Ri), where the closure uses the full F.
- Stop when Z stops growing. The FD is preserved if Y ⊆ Z.
def preserves(fds, schemas):
for lhs, rhs in fds:
z = set(lhs)
changed = True
while changed:
changed = False
for s in schemas:
gain = closure(z & s, fds) & s
if not gain <= z:
z |= gain
changed = True
if not rhs <= z:
return False
return True
print(preserves(F, [{'C', 'B'}, {'A', 'C'}])) # False: AB -> C is lost
print(preserves(F, [{'A', 'B', 'C'}])) # TrueRun on the spine example, R(A, B, C) with AB → C and C → B, the test shows the problem. The only nontrivial FD that survives in R1(C, B) is C → B. R2(A, C) has no nontrivial FD. Starting from AB, no piece contains both A and B. The closure never reaches C, so AB → C is lost.
The two properties are independent
| Decomposition of R(A,B,C), F = {A→B} | Lossless | Dependency preserving |
|---|---|---|
| {A,B}, {A,C} | Yes (A is a key of the first piece) | Yes |
| {A,B}, {B,C} | No (B is not a key of either piece) | Yes |
| {B,C}, {A,C} | No (C is not a key of either piece) | No |
| {A,B}, {C} | No (empty intersection) | Yes |
The second row shows a decomposition that preserves every FD yet is lossy. The spine example shows the reverse: lossless but not preserving.
What each normal form guarantees
- BCNF decomposition (repeatedly split on a violating FD X → Y into XY and R − Y) is always lossless. It may lose dependencies. R(A, B, C) with AB → C and C → B is the standard counterexample.
- 3NF synthesis (build a minimal cover, make one relation per FD, add a relation holding a candidate key if none contains one, remove redundant relations) is always both lossless and dependency preserving.
Common errors
- Checking only that the pieces cover all attributes. Covering R is necessary but does not make a decomposition lossless.
- Calling a decomposition lossless because the pieces share some attribute. The shared set must be a key of a piece.
- Declaring an FD lost because no single piece contains all its attributes. It may still follow from FDs held in the pieces.
- Applying the binary test to three or more pieces. Use the chase, or apply the binary test step by step to a tree of splits.
Exercise
R(A, B, C, D) has F = {A → B, B → C, C → D}. R is decomposed into R1(A, B), R2(B, C), R3(C, D). Is the decomposition lossless? Is it dependency preserving?
Show answer
Lossless. In the chase tableau, rows 1, 2 and 3 are built from R1, R2, R3. B → C makes rows 1 and 2 agree on column C, so row 1 gets the distinguished symbol a in C. Then C → D makes row 1 gain a in D. Row 1 is now all distinguished symbols. Dependency preserving. A → B lies in R1, B → C in R2 and C → D in R3, so every FD of F is checkable inside one piece.