24 - Computer Science & Applicaions
24 - Computer Science & Applicaions
1
Decomposing a Relation Problems with Decompositions
• Easiest fix is to create a relation RW to store these
associations, and to remove W from the main • There are three potential problems to consider:
schema: 1) May be impossible to reconstruct the original relation!
S N L R H (Lossiness)
123-22-3666 Attishoo 48 8 40 • Fortunately, not in the SNLRWH example.
R W
231-31-5368 Smiley 22 8 30 2) Dependency checking may require joins.
8 10
131-24-3650 Smethurst 35 5 30
5 7
• Fortunately, not in the SNLRWH example.
434-26-3751 Guldu 35 5 32 3) Some queries become more expensive.
612-67-4134 Madayan 35 8 40 Wages • e.g., How much does Guldu earn?
Hourly_Emps2
Tradeoff: Must consider these issues vs.
•Q: Are both of these relations are now in BCNF? redundancy.
•Decompositions should be used only when needed.
–Q: potential problems of decomposition?
• Semantics of R ba S are:
ba 231-31-5368 Smiley 22 8 30
131-24-3650 Smethurst 35 5 30 ba
R W
8 10
5 7
– Compute Cartesian Product of R and S 434-26-3751 Guldu 35 5 32
– Select out those tuples where the common attributes of R 612-67-4134 Madayan 35 8 40
and S have the same values
– Keep all unique attributes of these tuples plus one copy of S N L R W H
each of the common attributes.
123-22-3666 Attishoo 48 8 10 40
• More on this in the next lecture
= 231-31-5368 Smiley 22 8
131-24-3650 Smethurst 35 5
10 30
7 30
434-26-3751 Guldu 35 5 7 32
612-67-4134 Madayan 35 8 10 40
A B C A B B C
1 2 3 1 2 2 3 • Decomposition of R into X and Y is lossless-join w.r.t.
4 5 6 a set of FDs F if, for every instance r that satisfies F:
4 5 5 6
π X (r)
π Y (r) = r
7 2 8 7 2 2 8
• It is always true that r ⊆ π X (r)
π Y (r)
A → B; C → B – In general, the other direction does not hold! If it does,
the decomposition is lossless-join.
A B C
• Definition extended to decomposition into 3 or more
A B B C
ba
1 2 3 relations in a straightforward way.
1
4
2
5
2
5
3
6 = 4
7
5
2
6
8
• It is essential that all decompositions used to deal with
redundancy be lossless! (Avoids Problem #1)
7 2 2 8 1 2 8
7 2 3
2
More on Lossless Decomposition Lossless Decomposition (example)
• The decomposition of R into X and Y is
A B C A C B C
lossless with respect to F if and only if the
closure of F contains: 1 2 3 1 3 2 3
4 5 6 4 6 5 6
X ∩ Y → X, or
7 2 8 7 8 2 8
X∩Y→Y
in example: decomposing ABC into AB and BC is A → B; C → B
lossy, because intersection (i.e., “B”) is not a key
of either resulting relation. A C B C A B C
• Useful result: If W → Z holds over R and W ∩ Z is
empty, then decomposition of R into R-Z and WZ is
1
4
7
3
6
8
ba 2
5
2
3
6
8
= 1
4
7
2
5
2
3
6
8
loss-less.
3
Third Normal Form (3NF) What Does 3NF Achieve?
• If 3NF violated by X → A, one of the following holds:
• Reln R with FDs F is in 3NF if, for all X → A in F+
– X is a subset of some key K (“partial dependency”)
A ∈ X (called a trivial FD), or • We store (X, A) pairs redundantly.
X is a superkey of R, or • e.g. Reserves SBDC (C is for credit card) with key SBD and S→C
A is part of some candidate key (not superkey!) for R. – X is not a proper subset of any key. (“transitive dep.”)
(sometimes stated as “A is prime”) • There is a chain of FDs K → X → A, which means that we cannot
associate an X value with a K value unless we also associate an A
• Minimality of a key is crucial in third condition above! value with an X value (different K’s, same X implies same A!) –
problem with initial SNLRWH example.
• If R is in BCNF, obviously in 3NF.
• But: even if R is in 3NF, these problems could arise.
• If R is in 3NF, some redundancy is possible. It is a – e.g., Reserves SBDC (note: “C” is for credit card here), S → C, C
compromise, used when BCNF not achievable (e.g., no → S is in 3NF (why?), but for each reservation of sailor S, same
``good’’ decomp, or performance considerations). (S, C) pair is stored.
– Lossless-join, dependency-preserving decomposition of R • Thus, 3NF is indeed a compromise relative to BCNF.
into a collection of 3NF relations always possible.