Professional Documents
Culture Documents
Ch6 - Functional Dependencies and Normalization For Relational Databases
Ch6 - Functional Dependencies and Normalization For Relational Databases
GUIDELINE 4:
The relations should be designed to satisfy the
lossless join condition.
No spurious tuples should be generated by doing
a natural-join of any relations.
Note that:
Property (a) is extremely important and cannot be
sacrificed.
Property (b) is less stringent and may be sacrificed.
SSN ENAME
AG I
1. Does A R? == Is (A)+ R
2. Does G R? == Is (G)+ R
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Uses of Attribute Closure
There are several uses of the attribute closure algorithm:
Testing for superkey:
To test if is a superkey, we compute +, and check if + contains
all attributes of R.
Testing functional dependencies
To check if a functional dependency holds (or, in other
words, is in F+), just check if +.
That is, we compute + by using attribute closure, and then check
if it contains .
Is a simple and cheap test, and very useful
Computing closure of F
For each R, we find the closure +, and for each S +, we
output a functional dependency S.
32
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Algorithm for Finding a Key K for R Given a set F of Functional
Dependencies
Input: A relation R and a set of functional dependencies F on
the attributes of R.
1. Set K := R.
2. For each attribute A in K
{compute (K – A)+ with respect to F;
if (K – A)+ contains all the attributes in R,
then set K := K – {A} };
34
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Algorithm for Finding all candidate keys for R (cont.)
Example: Given Q(R), R = {A, B, C, D, E, G} and
F = { f1: AE C, f2 : CG A, f3 : BD G, f4: GA E}
Find all candidate keys of Q(R).
- Phase 1:
computing closures of (2n -1) not null subsets of R.
...
110110
Note: if ABD is a super ABDE ABDE
key then any set S ABD is also a super
key.
35
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Algorithm for Finding all candidate keys for R (cont.)
- The result of phase 1:
S = { ABD, BCD, ABCD, ABDE, BCDE, ABCDE, ABDG,
BCDG, ABCDG, ABDEG, BCDEG, ABCDEG}
- The result of phase 2 : choose minimum sets in S.
K = { ABD, BDC}
36
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Improved Algorithm for Finding all candidate keys for R
Comment:
If R is large, it takes a long time for computing closures of
(2n –1) subsets of R.
The above algorithm is improved as follows:
Given A R,
- A is called a source attribute if A is not in the right hand
side of any non trivial FD of F
The set of all source attributes denotes by N
- A is called a destination attribute if A is not a source
attribute and A is not in the left hand side of any non
trivial FD of F.
The set of all destination attributes denotes by D.
37
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Improved Algorithm for Finding all candidate keys for R
- The set of attributes which are not source or destination
attributes denotes by L.
38
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Improved Algorithm for Finding all candidate keys for R
Example: Given Q(R), R = {A, B, C, D, E, G} và
F = { f1: AE C, f2 : CG A, f3 : BD G, f4: GA E}
Find all candidate keys of Q
N = {B,D}, D= , L = {A,C, E, G}
- Phase 1:
Compute closure of subsets of R.
L= X= N U li X+F Siêu
ACEG L khóa
0000 BD
0001 BDG
39
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
3. Normalization of Relations
Normalization:
The process of decomposing unsatisfactory "bad"
relations by breaking up their attributes into
smaller relations
Normal form:
Condition using keys and FDs of a relation to
certify whether a relation schema is in a particular
normal form
we cannot
reconstruct the
original
employee
relation -- we
lose information
A B C A B B C
1 A 1 1 A
2 B 2 2 B
r A,B(r) B,C(r)
A B C
A (r) B (r)
1 A
2 B
R1 R2 R2
The above functional dependencies are a sufficient condition for
lossless join decomposition; the dependencies are a necessary
condition only if all constraints are functional dependencies
Repeat
for each Ri in the decomposition
t = (result Ri)+ Ri
result = result t
Until (result does not change)
If result contains all attributes in , then the functional
dependency is preserved.
We apply the test on all dependencies in F to check if a
decomposition is dependency preserving
This procedure takes polynomial time, instead of the exponential
time required to compute F+ and (F1 F2 … Fn)+
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Goals of Normalization
Let R be a relation scheme with a set F of functional
dependencies.
Decide whether a relation scheme R is in “good” form.
2. employee_id branch_name
dependency
No other attribute is extraneous, so we get F =
C
= building, budget
room_number, time_slot_id)
classroom and section are in BCNF.
Lossless join.
Dependency preservation.