Chapter 7
Chapter 7
2
First Normal Form (Cont’d)
3
Goal — Devise a Theory for the Following
• Decide whether a particular relation R is in “good” form.
• In the case that a relation R is not in “good” form, decompose it into a set of
relations {R1, R2, ..., Rn} such that
• each relation is in good form
• the decomposition is a lossless-join decomposition
• Require that the value for a certain set of attributes determines uniquely
the value for another set of attributes.
5
Functional Dependencies (Cont.)
• Let R be a relation schema
α ⊆ R and β ⊆ R
• The functional dependency
α→β
holds on R if and only if for any legal relations r(R), whenever any two
tuples t1 and t2 of r agree on the attributes α, they also agree on the attributes
β. That is,
t1[α] = t2 [α] ⇒ t1[β ] = t2 [β ]
7
Functional Dependencies (Cont.)
10
Functional Dependencies (Cont.)
• A functional dependency is trivial if it is satisfied by all
instances of a relation
• Example:
• customer_name, loan_number → customer_name
• customer_name → customer_name
• In general, α → β is trivial if β ⊆ α
11
Closure of a Set of Functional Dependencies
• Given a set F of functional dependencies, there are other
functional dependencies that are logically implied by F.
• For example: If A → B and B → C, then we can derive
that A → C
F+=F
repeat
for each functional dependency f in F+
apply reflexivity and augmentation rules on f
add the resulting functional dependencies to F +
for each pair of functional dependencies f1and f2 in F +
if f1 and f2 can be combined using transitivity
then add the resulting functional dependency to F +
until F + does not change any further
15
Closure of Functional Dependencies (Cont.)
16
Closure of Attribute Sets
⚫ Given a set of attributes α, define the closure of α under F
(denoted by α+) as the set of attributes that are functionally
determined by α under F
result := α;
while (changes to result) do
for each β → γ in F do
begin
if β ⊆ result then result := result ∪ γ
end
17
Example of Attribute Set Closure
• R = (A, B, C, G, H, I)
• F = {A → B
A→C
CG → H
CG → I
B → H}
• (AG)+
1. result = AG
2. result = ABCG (A → C and A → B)
3. result = ABCGH (CG → H and CG ⊆ AGBC)
4. result = ABCGHI (CG → I and CG ⊆ AGBCH)
18
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.
19
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.
• In the case that a relation scheme R is not in “good” form, decompose
it into a set of relation scheme {R1, R2, ..., Rn} such that
• each relation scheme is in good form
• the decomposition is a lossless-join decomposition
• Preferably, the decomposition should be dependency preserving.
20
Second Normal Form (2NF)
• A table is in 2NF if it is in 1NF and each non-key field is functionally
dependent on the entire primary key.
Example
Airport – City
Dulles – Washington, DC
21
Second Normal Form (2NF)
A table is in 2NF if it is in 1NF and each non-key field is functionally
dependent on the entire primary key.
Employees_Projects Violates 2NF
*EmpID Lname Fname *ProjNum ProjTitle
EN1-25 O’Brien Sean 30-452-T3 STAR Manual
EN1-25 O’Brien Sean 30-457-T3 ISO
Procedures
EN1-25 O’Brien Sean 31-124-T3 Employee
Handbook
EN1-33 Guya Amy 30-452-T3 STAR Manual
EN1-33 Guya Amy 30-482-TC Web site 22
Tables in 2NF
Employees
*EmployeeID LastName FirstName
EN1-26 O’Brien Sean
EN1-33 Guya Amy
Employees_Projects Projects
*EmployeeID *ProjNum *ProjNu Title
EN1-26 30-452-T3 m
30-452-T3 STAR manual
EN1-33 30-457-T3
30-457-T3 ISO procedure
23
Boyce-Codd Normal Form
A relation schema R is in BCNF with respect to a set F of functional
dependencies if for all functional dependencies in F+ of the form
α→ β
where α ⊆ R and β ⊆ R, at least one of the following holds:
• α → β is trivial (i.e., β ⊆ α)
• α is a super key for R
24
Decomposing a Schema into BCNF
• Suppose we have a schema R and a non-trivial dependency
α→β causes a violation of BCNF.
We decompose R into:
•(αU β )
•( R - ( β - α ) )
• In our example,
• α = loan_number
• β = amount
and bor_loan is replaced by
• (αU β ) = ( loan_number, amount ) 25
• ( R - ( β - α ) ) = ( customer_id, loan_number )
Sample BCNF Violation
Course_Students_TAs
CourseNum Student TA
ENG101 Jones Clark
ENG101 Grayson Chen
ENG101 Samara Chen
MAT350 Grayson Powers
MAT350 Jones O’Shea
MAT350 Berg Powers
26
Tables in BCNF
Courses
*CourseNum *Student
ENG101 Jones
MAT350 Grayson
Students TAs
*Student *TA *CourseNum *TA
Jones Clark ENG101 Clark
Grayson Chen MAT350 Chen
27
Canonical Cover
• Set of functional dependencies may have redundant dependencies that can
be inferred from the others
• For example: A → C is redundant in: {A → B, B → C}
• Parts of a functional dependency may be redundant
• E.g.: on RHS: {A → B, B → C, A → CD} can be simplified to
{A → B, B → C, A → D}
• E.g.: on LHS: {A → B, B → C, AC → D} can be simplified to
{A → B, B → C, A → D}
28
Canonical Cover
31
Third Normal Form: Motivation
32
Third Normal Form
• Solution: define a weaker normal form, called Third
Normal Form (3NF)
• Allows some redundancy
• But functional dependencies can be checked on individual
relations without computing a join.
• There is always a lossless-join, dependency-preserving
decomposition into 3NF.
33
Third Normal Form
• A relation schema R is in third normal form (3NF) if for all:
α → β in F+
at least one of the following holds:
• α → β is trivial (i.e., β ∈ α)
• α is a super key for R
• Each attribute A in β – α is contained in a candidate key for R.
(NOTE: each attribute may be in a different candidate key)
• If a relation is in BCNF it is in 3NF (since in BCNF one of the first two
conditions above must hold).
• Third condition is a minimal relaxation of BCNF to ensure dependency
preservation .
34
3NF Example
• Relation R:
• R = (J, K, L )
F = {JK → L, L → K }
• Two candidate keys: JK and JL
• R is in 3NF
JK → L JK is a superkey
L → K K is contained in a candidate key
35
Comparison of BCNF and 3NF
36
Thank You