Chapter 3 - Functional Dependencies
Chapter 3 - Functional Dependencies
Chapter 3 - Functional Dependencies
Chapter 3
Design theory for
Relational Database - FDs
Objectives
5 Understand what are closing sets of FD’s and how to determine them
1 Functional Dependencies
6 Projecting FD’s
1. Functional Dependency Definition
Movies1
1. TRUE
(title, year) → (length, genre, studioName)
2. FALSE
(title, year) → (starName)
Example: Functional Dependency
1. TRUE/FALSE
A→B
2. TRUE/FALSE
AB → C
2. Properties of
functional dependencies
Given that X, Y, and Z are sets of attributes in a relation R,
one can derive several properties of functional dependencies.
Among the most important are Armstrong’s axiom, which are used in
database normalization:
• Subset Property (Reflexivity): If Y is a subset of X, then X → Y
• Augmentation (Augmentation): If X → Y, then XZ → YZ
• Transitivity (Transitivity): If X → Y and Y → Z, then X → Z
Exercise 1:
(title, year) forms a key?
Exercise 2:
(title, year, starName) forms a key ?
Example: Keys
Exercise 1:
(title, year) forms a key? Not a key
Row 1 and 2: agree in (title, year) but do not agree in
starName → (title, year) do not determine starName
Exercise 2:
(title, year, starName) forms a key ? A key
Find keys
A B C D E F
13
Find keys
A B C D E F
Custid Cphone
14
3. Super-key
Step 2: result = A;
25
Some LEMAs
26
Exercises 1
R (A, B, C, D)
S = {BC→D, D→A, A→B}
What are all the keys of R?
What are all the super-keys for R that are not
keys?
Exercises 2
R (A, B, C, D)
S = {AD→B, AB→C, BC→D, CD→A}
What are all the keys of R?
What are all the super-keys for R that are not
keys?
Closing sets of FD’s
A minimal basis (or closing sets) of FD’s for a relation
is a set of functional dependencies B that satisfies three
conditions:
All the FD’s in B have singleton right sides.
If any FD is removed from B, the result is no longer the
basic.
If for any FD in B we remove one or more attributes
from the left side, the result is no longer the basic.
No trivial FD can be in a minimal basis.
Projecting FD’s
Algorithm for projecting a set of FD’s
Ex. The attribute closures
1. R = (A,B,C,D,E), F = {A -> C, E -> D, B -> C}
a. {A, B, E}+ b. {A, B, C, E}+ c. {A, B, D, E}+
2. R = (A,B,C,D,E), F = {A -> BE, C -> BE, B -> D}
a. {A, C}+ b. {A, B, C}+ c. {A, C, E}+
3. R = (A,B,C,D,E,F), F = {A -> B, B -> D, C -> D, E -> F}
a. {A, C, E}+ b. {A, B, C, E}+ c. {A, C, D, E, F}+
4. R = (A,B,C,D), F = {AB -> C, BC -> D, CD -> A}
a. {A, B}+ b. {B, C}+ c. {B, C, D}+
5. R = (A,B,C,D), F = {A -> BCD, C -> A}
a. {A}+ b. {C}+ c. {A, D}+ d. {A, B, C}+
33
Objectives
1 Anomalies
Movies1 Movies2
title year length genre studioName title year starName
Star Wars 1977 124 SciFi Fox Star Wars 1977 Carrie Fisher
Gone With The Wind 1939 231 drama MGM Star Wars 1977 Mark Hamill
Wayne's World 1992 95 comedy Paramount Star Wars 1977 Harrison Ford
Gone With The1939
Wind Vivien Leigh
Wayne's World1992 Dana Carvey
Wayne's World1992 Mike Meyers
Discuss
Movies1 Movies2
title year length genre studioName title year starName
Star Wars 1977 124 SciFi Fox Star Wars 1977 Carrie Fisher
Gone With The Wind 1939 231 drama MGM Star Wars 1977 Mark Hamill
Wayne's World 1992 95 comedy Paramount Star Wars 1977 Harrison Ford
Gone With The1939
Wind Vivien Leigh
Wayne's World1992 Dana Carvey
Wayne's World1992 Mike Meyers
Movies1
title year length genre studioName
Star Wars 1977 124 SciFi Fox
Gone With The Wind 1939 231 drama MGM
Wayne's World 1992 95 comedy Paramount
Movies1
title year length genre studioName
Star Wars 1977 124 SciFi Fox
Gone With The Wind 1939 231 drama MGM
Wayne's World 1992 95 comedy Paramount
24
1NF
25
1NF
26
1NF
27
1NF
28
2NF
A relation R is in second normal form (2NF) if and
only if it is in 1NF and every non-key attribute
is fully dependent on the primary key
Example: 2NF? Why?
2NF
A relation R is in second normal form (2NF) if and
only if it is in 1NF and every non-key attribute
is fully dependent on the primary key
Example: 2NF? Why?
2NF
A relation R is in second normal form (2NF) if and
only if it is in 1NF and every non-key attribute
is fully dependent on the primary key
Example: 2NF? Why?
2NF
2NF
33
2NF
34
2NF
35
2NF
36
2NF
37
2NF
38
39
40
41
3NF
A relation R is in third normal form (3NF) if and
only if it is in 2NF and every non-key attribute
is non-transitively dependent on the primary
key.
An attribute C is transitively dependent on
attribute A if there exists an attribute B such that:
A->B and B->C
Note that 3NF is concerned with transitive
dependencies which do not involve candidate keys.
A relation with more than one candidate key will
clearly have transitive dependencies of the form:
primary_key -> other_candidate_key -> any_non-
key_column
Employee No. → Department No.
3NF
Department No. → Department Name
A B C D
43
3NF
44
1NF -> 2NF -> 3NF?
45
1NF -> 2N -> 3NF?
46
MASV HOTEN DIACHI 1NF ->
MAMON 2N
TENMON ->DIEM
3NF?
A01 Lê Na 12 Thái Hà M01 CSDL 8
A01 Lê Na 12 Thái Hà M02 Anh 9
A02 Trần An 56 Mã Mây M01 CSDL 8
A03 Hà Nam 24 Cầu Gỗ M01 CSDL 6
A03 Hà Nam 24 Cầu Gỗ M02 Anh 8
A03 Hà Nam 24 Cầu Gỗ M03 Toán 1 9
48
BCNF
3NF Boyce-Codd
a nontrivial functional dependency: a nontrivial functional dependency
X -> A holds in R, either X -> A holds in R, then:
(a) X is a superkey of R, or a) X is a superkey of R
(b) A is a prime attribute of R.
Note:Attributes which are parts of any candidate key of relation are called
as prime attribute, others are non-prime attributes
A functional dependency X -> Y is a full functional dependency if
removal of any attribute A from X means that the dependency does not
hold any more; A partial functional dependency is not a full functional
dependency
1 or 2 or 3 NF?
1 or 2 or 3 NF?
BCNF?
BCNF?
BCNF?
5. Decompose a relation
into BCNF, 3NF
72
❖Step 2. Find all candidate keys. The set of candidates
keys is { (b,d) }.
The set of key attributes is: { b,d }.
73
❖Step 3: Merge FDs with same LHS and whose RHS are
non-key attributes, we get the set F1 which contains:
b --> c
d --> a
74
❖Step 4: Check each FD in the set F1 for violation of 3NF,
and split table accordingly.
Checking FD b --> c: The FD violates 3NF as its LHS is not a
superkey (and RHS is a set of non-key attributes).
The following 3NF table is obtained: b,c; with FDs: b --> c
Checking FD d --> a: The FD violates 3NF as its LHS is not a
superkey (and RHS is a set of non-key attributes).
The following 3NF table is obtained: d,a; with FDs: d --> a
75
❖Step 5: Finally, add the following table into normalized
3NF table set (obtained by removing RHS attributes of FDs
using which we produced a table): b,d; with FDs: empty
76
77
Step 1: Find the minimal cover of FDs, which contains
s,d --> p
j,p --> c
j --> s
78
Step 2. Find all candidate keys. The set of candidates
keys is { (j,d,q,v), }.
The set of key attributes is: { j,d,q,v }.
79
Step 3: Merge FDs with same LHS and whose RHS are
non-key attributes, we get the set F1 which contains:
s,d --> p
j,p --> c
j --> s
80
Step 4: Check each FD in the set F1 for violation of 3NF, and
split table accordingly.
Checking FD s,d --> p: The FD violates 3NF as its LHS is not a
superkey (and RHS is a set of non-key attributes).
The following 3NF table is obtained:s,d,p; with FDs: d,s --> p
Checking FD j,p --> c: The FD violates 3NF as its LHS is not a
superkey (and RHS is a set of non-key attributes).
The following 3NF table is obtained: j,p,c; with FDs: j,p --> c
Checking FD j --> s: The FD violates 3NF as its LHS is not a
superkey (and RHS is a set of non-key attributes).
The following 3NF table is obtained: j,s; with FDs: j --> s
81
Step 5: Finally, add the following table into normalized 3NF table
set (obtained by removing RHS attributes of FDs using which we
produced a table): j,d,q,v; with FDs: empty
82
Summary 1
87
Fourth Normal Form
88
Fifth Normal Form (5NF)
89
Fifth Normal Form (5NF)
90
Reference
https://www.javatpoint.com/dbms-canonical-cover
91
ĐẠI HỌC FPT CẦN THƠ