Sub topic 21
Multivalued Dependencies and 4NF
This chapter assumes you can compute attribute closures, find candidate keys, and decide whether a relation is in BCNF. It also assumes you know what a lossless-join decomposition is. Here you learn a kind of redundancy that functional dependencies cannot see, and the normal form that removes it.
Why BCNF is not always enough
Suppose a course has several teachers and several textbooks. The teachers and the textbooks are independent facts about the course. Store them in one table:
| course | teacher | book |
|---|---|---|
| DBMS | Rao | Korth |
| DBMS | Rao | Elmasri |
| DBMS | Iyer | Korth |
| DBMS | Iyer | Elmasri |
The table has no nontrivial functional dependency. The only candidate key is all three attributes, {course, teacher, book}. So the table is in BCNF.
It is still badly designed. Every teacher is paired with every book. Adding a new book for DBMS means inserting one row per teacher. Forgetting one row makes the data inconsistent. This is redundancy, and it comes from a multivalued dependency.
Multivalued dependency
A multivalued dependency (MVD) X ->> Y holds in relation R(X, Y, Z) when the set of Y values paired with a given X value does not depend on Z. Here Z is every attribute of R that is not in X or Y.
Formally, X ->> Y holds if this is true for every pair of tuples t1 and t2 with t1[X] = t2[X]: there is a tuple t3 with these values:
t3[X] = t1[X]t3[Y] = t1[Y]t3[Z] = t2[Z]
By symmetry, a tuple t4 with Y taken from t2 and Z taken from t1 must also exist. In words, if one row has (x, y1, z1) and another has (x, y2, z2), then the rows (x, y1, z2) and (x, y2, z1) must also exist.
In the example, course ->> teacher and course ->> book both hold. The two MVDs are a pair, as shown below.
Properties you need
- Every FD is an MVD. If
X -> Y, thenX ->> Y. - Complementation: if
X ->> Yholds in R, thenX ->> Zholds, where Z = R − X − Y. This is why MVDs come in pairs, writtenX ->> Y | Z. - An MVD
X ->> Yis trivial ifY ⊆ XorX ∪ Y = R. Trivial MVDs always hold. - Unlike an FD, an MVD depends on the whole relation scheme, not only on X and Y. The same X and Y can satisfy an MVD in one relation and fail it when more attributes are added.
Fourth normal form
A relation R is in fourth normal form (4NF) if, for every nontrivial MVD X ->> Y that holds in R, X is a superkey of R.
This has the same shape as the BCNF rule, with MVDs in place of FDs. Since every FD is an MVD, every 4NF relation is in BCNF. The reverse is false, and the course example shows it.
| Normal form | Dependencies considered | Condition on each nontrivial dependency X -> Y or X ->> Y |
|---|---|---|
| BCNF | FDs | X is a superkey |
| 4NF | FDs and MVDs | X is a superkey |
In the course table, course ->> teacher is nontrivial because teacher is not in {course} and {course, teacher} is not all of R. Also course is not a superkey. So the table violates 4NF.
Decomposing into 4NF
The method resembles BCNF decomposition. While some relation R has a nontrivial MVD X ->> Y with X not a superkey, replace R by two relations:
- R1 = X ∪ Y
- R2 = R − Y (which keeps X and the remaining attributes)
The split is lossless exactly when X ->> Y holds. This is the MVD version of the lossless-join test. Applied to the example with course ->> teacher:
- R1(course, teacher)
- R2(course, book)
Each new table has only trivial MVDs, so both are in 4NF. A natural join on course rebuilds the original table.
from itertools import product
course_teacher = {("DBMS", "Rao"), ("DBMS", "Iyer")}
course_book = {("DBMS", "Korth"), ("DBMS", "Elmasri")}
# Natural join on course
joined = {
(c1, t, b)
for (c1, t), (c2, b) in product(course_teacher, course_book)
if c1 == c2
}
original = {
("DBMS", "Rao", "Korth"),
("DBMS", "Rao", "Elmasri"),
("DBMS", "Iyer", "Korth"),
("DBMS", "Iyer", "Elmasri"),
}
print(joined == original) # True: the decomposition is losslessThe pieces now store 4 values instead of 12. Adding a book is one insert, not one per teacher.
Checking whether an MVD holds
For a small table, apply the definition directly. Group rows by X. Within each group, the set of (Y, Z) combinations must equal the full cross product of the Y values and the Z values in that group.
def mvd_holds(rows, x_idx, y_idx, z_idx):
groups = {}
for r in rows:
key = tuple(r[i] for i in x_idx)
groups.setdefault(key, []).append(r)
for group in groups.values():
ys = {tuple(r[i] for i in y_idx) for r in group}
zs = {tuple(r[i] for i in z_idx) for r in group}
pairs = {(tuple(r[i] for i in y_idx), tuple(r[i] for i in z_idx)) for r in group}
if len(pairs) != len(ys) * len(zs):
return False
return True
rows = [
("DBMS", "Rao", "Korth"),
("DBMS", "Rao", "Elmasri"),
("DBMS", "Iyer", "Korth"),
("DBMS", "Iyer", "Elmasri"),
]
print(mvd_holds(rows, [0], [1], [2])) # True
print(mvd_holds(rows[:3], [0], [1], [2])) # False: the (Iyer, Elmasri) row is missingEdge cases and common errors
- Two unrelated multivalued attributes are the usual cause. If teacher and book were linked, for example each teacher uses only one book, the MVD would not hold, and the table would not have this redundancy.
- An MVD needs at least three attributes (X, Y, and a nonempty Z). With only X and Y,
X ->> Yis trivial. - Do not claim
X ->> Yfrom a few sample rows. The definition requires the cross-product rows for every pair of tuples, so a missing row is a violation, as the second Python check shows. - If X is a superkey, every row in a group is unique, so the MVD holds trivially and 4NF is satisfied.
- A 4NF decomposition is lossless but may not preserve FDs. As with BCNF, check dependency preservation separately.
- Converting a relation to BCNF first does not guarantee 4NF. You must still look for MVDs.
Link to 5NF
4NF handles dependencies that split a relation into two parts. Some relations can be rebuilt only from three or more projections. That case, the join dependency, is the subject of 5NF, covered in the next chapter.
Exercise
R(Student, Hobby, Language) stores each student's hobbies and spoken languages, and every student has every combination of their hobbies and languages. The only candidate key is {Student, Hobby, Language}. Is R in BCNF? Is it in 4NF? If not, give a 4NF decomposition.
Show answer
R is in BCNF because it has no nontrivial FD, so no FD violates the BCNF rule. R is not in 4NF, because Student ->> Hobby | Language holds, is nontrivial, and Student is not a superkey. Decompose into R1(Student, Hobby) and R2(Student, Language). Joining them on Student gives back R, so the decomposition is lossless.
Exercise
A relation R(A, B, C) contains the rows (1, x, p), (1, y, q). What rows must be added for A ->> B to hold?
Show answer
The rows (1, x, q) and (1, y, p). For the two existing tuples, t1 = (1, x, p) and t2 = (1, y, q), the definition needs a tuple with B from t1 and C from t2, which is (1, x, q). It also needs a tuple with B from t2 and C from t1, which is (1, y, p).
Exercise
True or false: if X ->> Y holds in R and X is a superkey of R, then R may still violate 4NF because of this MVD.
Show answer
False. 4NF only requires that X be a superkey for each nontrivial MVD X ->> Y. When X is a superkey, the MVD satisfies the condition, so it causes no violation.