Functional Dependencies
Functional Dependencies
Functional Dependencies
CSC343
Functional Dependencies Functional Dependencies
CSC343
Functional Dependencies Functional Dependencies
COURSES Relation
STUDENT Name
0,n
enroll
0,n
COURSES Name
1,1
teach
0,n
PROF Name
CSC343
Functional Dependencies Functional Dependencies
Key Example
SIN ! LastName
SIN ! FirstName
SIN ! Address
CSC343
Functional Dependencies Functional Dependencies
SUPPLYING Relation
0,n
PRODUCT Pname
CSC343
Functional Dependencies Functional Dependencies
ADDRESS Relation
Simplifying Hypothesis
Keys of the relation ADDRESS: (Street Number City) and (Number Street
Zipcode)
CSC343
Functional Dependencies Functional Dependencies
Definitions
a) Functional Dependencies
Let R(U) a relational schema, r one relation of the schema R, X U, Y U two
attribute sub-set of R. The functional dependencies
X ! Y
is true in r, iff (if and only if) for every tuples of r that share the same value for all
attributes of X, they also share the same value for all attributes of Y.
Example:
ADDRESS(STREET, NUMBER, CITY, ZIPCODE)
X = ZipCode
Y = City
CSC343
Functional Dependencies Functional Dependencies
b) SuperKey
Let R(U) a schema and X U a attribute subset.
X is a superkey superkey of r of the schema R, if X ! U.
Example:
SIN LASTNAME ! SIN LASTNAME FIRSTNAME ADDRESS
SIN LASTNAME is a superkey
c) KEY
X is a key, if:
1. X is a superkey: X ! U
2. it does not exist Y X, such that Y ! U
Example:
STREET NUMBER CITY ! STREET NUMBER CITY ZIPCODE
STREET NUMBER CITY is a key (why?)
CSC343
Functional Dependencies Functional Dependencies
COURSE(Name,Hour,Room,Prof)
F =N ! HR, HR ! P
We can proof from F that if we know the name of the course, we also know the name of
the professor. (Name is a key):
1. N ! HR: two tuples that share the name of the course share also the hour and the
room value.
2. HR ! P: two tuples that share the hour and the room share also the name of the
professor.
3. FD 1 and 2 implie that two tuples which have the same name for the course have
also the same professor name: N ! P
We can define some properties and rules on the FD which permit to deduct others FDs.
CSC343
Functional Dependencies Functional Dependencies
(Armstrong’s Axioms)
1) Reflexivity
If X Y then Y ! X (for every attribute subsets X et Y)
Example:
NAME CITY ! NAME
Trivial: “Two persons who have the same name and live in the same city have the
same name.”
10
CSC343
Functional Dependencies Functional Dependencies
2) Transitivity
If X ! Y and Y ! Z, then X ! Z
Example :
R(SIN, ZIPCODE, CITY)
fSIN ! ZIPCODE, ZIPCODE ! CITYg j= SIN ! CITY
“If we know the zipcode from a sin number and the city form the zipcode then we
know the city from the sin number.”
3) Augmentation
X ! Y j= XZ ! YZ
Example:
SIN ! ZIPCODE j= SIN CITY ! ZIPCODE CITY
11
CSC343
Functional Dependencies Functional Dependencies
4) Union et decomposition
fX ! A, X ! Bg,X ! AB
5) Pseudotransitivity
fX ! Y, WY ! Z g j= WX ! Z
REMARK: Union, decomposition and pseudotransitivity can be deduced from the others
axioms.
12
CSC343
Functional Dependencies Functional Dependencies
From the functional dependencies set given from the real world and using the preceding
properties (Armstrong’s axioms) we can deduce others FDs:
Examples:
1) R(A,B,C,D)
F = f A ! B, B ! C g
Transitivity: A ! C
Notation: F j= A ! C
13
CSC343
Functional Dependencies Functional Dependencies
The union of F and every deduced FD is called closing set, or attribute closure of F is
denoted F +.
14
CSC343
Functional Dependencies Functional Dependencies
Minimal Closure
The minimal closure G of a set F of functional dependencies (FD) is a set of FD such that:
15
CSC343
Functional Dependencies Functional Dependencies
Example
16
CSC343
Functional Dependencies Functional Dependencies
Answer:
Second demonstration
17
CSC343
Functional Dependencies Functional Dependencies
Minimal Closure
18
CSC343
Functional Dependencies Functional Dependencies
or if:
X ! A 2 F+
Compute F + from F might take time.
19
CSC343
Functional Dependencies Functional Dependencies
20
CSC343
Functional Dependencies Functional Dependencies
X+
Step i: We start X 1 i
X + = X +1 = X
i i
21
CSC343
Functional Dependencies Functional Dependencies
Examples
1. Show that HS is a key for R(CHSNRP) with the set F of FD given before.
2. for the relation ADDRESS(CITY, STREET, NUMBER, ZIPCODE), show
that CITY STREET NUMBER is a key. What is the other one?
22
CSC343
UPDATE ANOMALIES
CSC343
Functional Dependencies Update Anomalies
Example
Supplier(SNAME, FADDRESS)
Product(SNAME, PNAME, PRICE)
24
CSC343
Functional Dependencies Update Anomalies
Anomalies
25
CSC343
Functional Dependencies Update Anomalies
3) DELETION: if JONS do not supply COMPUTER anymore, we delete the second tuple we
lost any information about JONS.
4) INSERTION: we can not insert a new supplier and its address if we do not know, at least,
one product that it supplies.
SMITH Toronto COMPUTER 1000
JONS Montreal COMPUTER 900
SMITH Toronto KEYBOARD 400
DURAND NICE
26
CSC343
Functional Dependencies Update Anomalies
Integrity Constraints
The list of attributes is not sufficient to describe the semantic of the real world.
It is the dependencies which permits a good schema conception, i.e., the decomposition in
“good” relations.
27
CSC343
Functional Dependencies Update Anomalies
28
CSC343
DECOMPOSITION AND NORMAL FORM
CSC343
Functional Dependencies Decomposition and Normal Form
30
CSC343
Functional Dependencies Decomposition and Normal Form
Relation 1NF
Relation N1FN
31
CSC343
Functional Dependencies Decomposition and Normal Form
32
CSC343
Functional Dependencies Decomposition and Normal Form
33
CSC343
Functional Dependencies Decomposition and Normal Form
34
CSC343
Functional Dependencies Decomposition and Normal Form
35
CSC343
Functional Dependencies Decomposition and Normal Form
Remark: It is not necessary for F to be a minimal closure. It is enough that for all FD X ! A
of F +,
A is only composed by one attribute.
A is not one of the attribute of X
The definition of a 3NF schema becames:
Definition: A relation R is in 3NF, if every FD X ! A of F + satisfies the preceding
conditions,
either X is a superkey
or A belongs to one of the keys.
Actually, it is not necessary to check all FD of F +.
It is enough to check the ones belonging to F !
36
CSC343
Functional Dependencies Decomposition and Normal Form
RemarK (Cont’d):
Why superkey in the first condition?
With conditions 1 and 2 weaker than the condition on the minimal closure
it may exist some non elementary conditions:
If X ! A is a non elementary condition
and if X+ = U, then X is a superkey.
37
CSC343
Functional Dependencies Decomposition and Normal Form
Remark (end):
R(A,B,C,D)
F = f AB ! C, B ! D, D ! B, B ! A g
F is not a minimal closure, why?
(R, F ) is in 3NF, why?
38
CSC343
Functional Dependencies Decomposition and Normal Form
Lossless-Join Decomposition
Example
R ( A B C )
a b c
a b a
c b d
and F = f A ! B g
We decompose in:
R1 ( A B ) R2 ( B C )
a b b c
c b b a
b d
39
CSC343
Functional Dependencies Decomposition and Normal Form
R1 = (R) A;B
R2 = (R)
R’ = R1 1 R2 6= R:
B;C
R’ ( A B C )
a b c
a b a
a b d
c b c
c b a
c b d
40
CSC343
Functional Dependencies Decomposition and Normal Form
R1’( A B ) R2’ ( A C )
a b a c
c b a a
c d
R’’ ( A B C )
a b c
a b a
c b d
The condition is that after the join we found the same information than before the
decomposition.
41
CSC343
Functional Dependencies Decomposition and Normal Form
Theorem:
If (R1, R2) is decomposition of R and F a set of FD, then (R1, R2) is lossless-join
w.r.t. F , iff:
R1 \ R2 ! R1 - R2
or
R1 \ R2 ! R2 - R1
belongs to F + .
42
CSC343
Functional Dependencies Decomposition and Normal Form
Examples
R (A, B, C)
F =fA!Bg
1) R1(A, B), R2(B, C)
AB \ BC = B
AB - BC = A
BC - AB = C
The FD B ! A does not exist, nor B ! C in F +
) The decomposition loses information.
43
CSC343
Functional Dependencies Decomposition and Normal Form
44
CSC343
Functional Dependencies Decomposition and Normal Form
Definitions
1. Projection of a set of FD om Z U
(F ) = fX ! Y 2 F + j XY Z g
Z
Example: R(A,B,C,D), F = f AB ! C, C ! A, A ! D g
(F ) = f AB ! C, C ! A g
ABC
G+ = F +
45
CSC343
Functional Dependencies Decomposition and Normal Form
Examples
R(A,B,C,D)
F = f AB ! C, C ! A, A ! D g
= (ABC, BD) do not preserve the FDs of F
= (ABC, AD) preserves the FDs of F
R(A,B,C)
F = f A ! B, B ! A, A ! C g
= (AB, BC) preserve the FDs of F
46
CSC343
Functional Dependencies Decomposition and Normal Form
R(A, B, C, D)
F = f A ! B, B ! C, AB ! D g
The decomposition
R1(AC)
R2(AB)
R3(CD)
do not preserve the FDs of F . Why?
47
CSC343
Functional Dependencies Decomposition and Normal Form
Given a schema (R, F) not in 3NF, i.e. with some anomalies, we want to find a
decomposition of R:
48
CSC343
Functional Dependencies Decomposition and Normal Form
Remark:
49
CSC343
Functional Dependencies Decomposition and Normal Form
Algorithm
50
CSC343
Functional Dependencies Decomposition and Normal Form
Examples
1) R(A,B,C,D)
F = f AB ! C, B ! D, C ! A g
Keys: AB, BC
Step 1: R1(ABC) R2(BD) R3(CA)
Step 2: No need to create a new relation:
the key AB belongs to R1
Step 3: Delete R3: CA ABC
Good decomposition: R1(ABC) R2(BD)
We can check that R1 et R2 are in 3NF.
51
CSC343
Functional Dependencies Decomposition and Normal Form
2) R(A,B,C,D,E)
F = f AB ! C, C ! D, C ! A g Keys: ABE, BCE
Step 1: R1(ABC) R2(CD) R3(CA)
Step 2: We add a relation of schema for the key ABE: R4(ABE)
Step 3: Delete R3: CA ABC
Good decomposition: R1(ABC) R2(CD) R4(ABE)
Other solutions:
Step 4: We replace R2 and R3 from step 1 by the relation of schema (CAD)
Other good decomposition: R1(ABC) R2’(CAD) R4(ABE)
What happened if we have chosen the key CBE?
52
CSC343
Functional Dependencies Decomposition and Normal Form
3) R(A,B,C,D)
F = f AB ! C, C ! D, C ! A, AB ! D g
Keys: BA, BC
The relation is not in 3NF. Why?
Step 1: R1(ABC) R2(CD) R3(CA) R4(ABD)
Step 2: we do not add the relation: key AB R1(ABC)
Step 3: Delete R3: CA ABC
Step 4: We replace R1 et R4 by R5(ABCD) ) we can delete R2.
Decomposition: R5(ABCD)
This decomposition is not in 3NF. Where is the problem?
53
CSC343
Functional Dependencies Decomposition and Normal Form
Example: Post(City,Street,ZipCode), F = f VC ! Z, V ! C g
Keys: CS, SZ
54
CSC343
Functional Dependencies Decomposition and Normal Form
is lossless-join
preserves the FD.
55
CSC343
Functional Dependencies Decomposition and Normal Form
Post Example
Post(City,Street,ZipCode), F = f CS ! Z, Z ! C g
Keys: CS, SZ
56
CSC343
Functional Dependencies Decomposition and Normal Form
The insertion of Toronto Queen M4, i.e. Toronto M4 and King M4 respects Z !
C but do not respect anymore CS ! Z
57
CSC343
Functional Dependencies Decomposition and Normal Form
Decomposition Algorithm
58
CSC343
Functional Dependencies Decomposition and Normal Form
Example 1
Pick B ! O:
R1 (BO)
R2 (BSQID)
Now we decompose R2 using S ! D
R3 (SD)
R4 (BSQI)
Now we decompose R4 using I ! B
R5(IB)
R6(ISQ)
59
CSC343
Functional Dependencies Decomposition and Normal Form
R1 (BO) F1 = f B ! O g
R3 (SD) F3 = f S ! D g
R5 (IB) F5 = f I ! B g
R6 (ISQ) F6 = f IS ! Q g
60
CSC343
Functional Dependencies Decomposition and Normal Form
Example 2
R1(ABC) F1 = f A ! BC g
R2(ADF) F2 = f D ! AF g
R3(DE) F3 = ;
61
CSC343
Functional Dependencies Decomposition and Normal Form
Example 3
R1(AC) F1 = f C ! A g
R@(DE) F2 = ;
62
CSC343
Functional Dependencies Decomposition and Normal Form
63
CSC343
4NF
CSC343
Functional Dependencies Decomposition and Normal Form
Definition
65
CSC343
Functional Dependencies Decomposition and Normal Form
Example
66
CSC343
Functional Dependencies Decomposition and Normal Form
Example
67
CSC343
Functional Dependencies Decomposition and Normal Form
We will need to compute all the multivalued dependencies that are logically implied by a
given set of multivalued dependencies.
68
CSC343
Functional Dependencies Decomposition and Normal Form
The following set of inference rules is sound and complete. The first three rules are
Armstrong’s axioms.
69
CSC343
Functional Dependencies Decomposition and Normal Form
70
CSC343
Functional Dependencies Decomposition and Normal Form
Other Axioms
71
CSC343
Functional Dependencies Decomposition and Normal Form
We saw that a BCNF schema was not an ideal design as it suffered from repetition of
information.
We can use the given multivalued dependencies to improve the database design by
decomposing it into fourth normal form.
A relation schema R is in 4NF with respect to a set D of functional and multivalued
dependencies if for all multivalued dependencies in D+ of the form X Y , where
X R and Y R :
–X Y is a non trivial multivalued dependency; and
– X is a superkey for schema R.
A database design is in 4NF if each member of the set of relation schemas is in 4NF.
The definition of 4NF differs from the BCNF definition only in the use of multivalued
dependencies:
– Every 4NF schema is also in BCNF.
72
CSC343
Functional Dependencies Decomposition and Normal Form
Summarize
73
CSC343