Functional Dependency
Functional Dependency
Functional Dependencies
2
Functional Dependencies
COURSES Relation
Functional Dependencies
Key Example
SIN LastName
SIN FirstName
SIN Address
4
Functional Dependencies
SUPPLYING Relation
Functional Dependencies
ADDRESS Relation
Simplifying Hypothesis
6
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, Number, Street
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 or Candidate 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?)
d) Prime Attribute is an attribute of a key.
8
Functional Dependencies
COURSE(Name,Hour,Room,Prof)
N HR, HR P
We can proof from 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 sharing the same value for the course have also the
same professor name: N P
We can define some properties and rules on the FD that permit to deduct others FDs.
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
Functional Dependencies
2) Transitivity
If X Y and Y Z, then X Z
Example :
R(SIN, ZIPCODE, CITY)
SIN ZIPCODE, ZIPCODE CITY SIN CITY
“If we know the zipcode from a sin number and the city from the zipcode then we
know the city from the sin number.”
3) Augmentation
X Y XZ YZ
Example:
SIN ZIPCODE SIN CITY ZIPCODE CITY
11
Functional Dependencies
4) Union et decomposition
X A, X B X AB
5) Pseudotransitivity
X Y, WY Z WX Z
REMARK: Union, decomposition and pseudotransitivity can be deduced from the others
axioms.
12
Functional Dependencies
From the set of functional dependencies given from the real world and using the previous
properties (Armstrong’s axioms) we can deduce others FDs:
Examples:
1) R(A,B,C,D)
= A B, B C
Transitivity: A C
Notation: A C
13
Functional Dependencies
The union of and every deduced FD is called closing set, or attribute closure of and
it is denoted .
14
Functional Dependencies
Minimal Closure
The minimal closure of a set of functional dependencies (FD) is a set of FD such that:
15
Functional Dependencies
Example
16
Functional Dependencies
Answer:
Second demonstration
17
Functional Dependencies
Minimal Closure
18
Functional Dependencies
or if:
X A
19
Functional Dependencies
20
Functional Dependencies
½
Step i: We start
For every Y A, such that Y ½
, Y A , we add A to ½
.
We get .
· ·½
= =
21
Functional Dependencies
Examples
1. Show that HS is a key for R(CHSNRP) with the set 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
UPDATE ANOMALIES
Update Anomalies
Example
Supplier(SNAME, FADDRESS)
Product(SNAME, PNAME, PRICE)
24
Update Anomalies
Anomalies
25
Update Anomalies
3) DELETION: if JOHN do not supply COMPUTER anymore, we delete the second tuple we
lost any information about JOHN.
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
JOHN Montreal COMPUTER 900
SMITH Toronto KEYBOARD 400
DURAND NICE
26
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
Update Anomalies
28
DECOMPOSITION AND NORMAL FORM
30
Decomposition and Normal Form
Relation 1NF
31
Lossless-Join Decomposition
Example
R ( A B C )
a b c
a b a
c b d
and = AB
We decompose in:
R1 ( A B ) R2 ( B C )
a b b c
c b b a
b d
32
Decomposition and Normal Form
R1 = (R)
R2 = (R)
R’ = R1 ½ R2 R:
R’ ( A B C )
a b c
a b a
a b d
c b c
c b a
c b d
33
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.
34
Decomposition and Normal Form
Theorem:
If (R1, R2) is decomposition of R and a set of FD, then (R1, R2) is lossless-join
w.r.t. , iff:
R1 R2 R1 - R2
or
R1 R2 R2 - R1
belongs to ·.
35
Examples
R (A, B, C)
= AB
36
Decomposition and Normal Form
37
Definitions
1. Projection of a set of FD on Z U
( ) =
Example: R(A,B,C,D), = AB C, C A, A D
( ) = AB C, C A
2. Decomposition which preserves the FDs of
Let = (R1,...,Rk) be a decomposition, and a set of FD.
preserves the FDs of , if we can find again every FDs of from the union of
all FDs projected from in ½( ), . . . , ( ):
· = ·
38
Decomposition and Normal Form
Examples
R(A,B,C,D)
= AB C, C A, A D
= (ABC, BD) do not preserve the FDs of
= (ABC, AD) preserves the FDs of
R(A,B,C)
= A B, B A, A C
= (AB, BC) preserve the FDs of
39
R(A, B, C, D)
= A B, B C, AB D
The decomposition:
R1(AC) ½ = A C
R2(AB) ¾ = A B
R3(CD) ¿ =
does not preserve the FDs of . Why?
40
Decomposition and Normal Form
41
42
Decomposition and Normal Form
43
44
Decomposition and Normal Form
Remark: It is not necessary for to be a minimal closure. It is enough that for all FD X A
of ,
A is only composed by one attribute.
A is not one of the attribute of X
The definition of a 3NF schema becomes:
Definition: A relation R is in 3NF, if every FD X A of satisfies the preceding
conditions,
either X is a superkey
or A belongs to one of the keys.
Besides, it is not necessary to check all FD of .
It is enough to check the ones belonging to !
45
Remark (Cont’d):
R(A,B,C,D)
= AB C, B D, D B, B A
46
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:
47
Remark:
Theorem: Every 1NF relation has a decomposition in 3NF relations which are
lossless-join and preserve the functional dependencies.
48
Decomposition and Normal Form
Algorithm
49
Examples
1) R(A,B,C,D)
= AB C, B D, C A
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) ½ = AB C, C A
R1(ABC) ¾ = B D
We can check that R1 et R2 are in 3NF.
50
Decomposition and Normal Form
2) R(A,B,C,D,E)
= AB C, C D, C A 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) = AB C, C A
R2(CD) = C D
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) = AB C, C A
R2’(CAD) = C A, C D
R4(ABE) =
What happened if we have chosen the key CBE?
51
3) R(A,B,C,D)
= AB C, C D, C A, AB D
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?
52
Decomposition and Normal Form
Keys: CN, DN
53
¯ is lossless-join
¯ preserves the FD.
54
Decomposition and Normal Form
Phone Example
Keys: CN, DN
55
Decomposition Algorithm
56
Decomposition and Normal Form
Example 1
Pick B O:
R1 (BO)
R2 (BSQID)
R3 (SD)
R4 (BSQI)
R5(IB)
R6(ISQ)
57
Example 1: Cont’d
R1 (BO) ½ = B O
R3 (SD) = S D
R5 (IB) = I B
R6 (ISQ) = IS Q
58
Decomposition and Normal Form
Example 2
R1(ABC) ½ = A BC
R2(ADF) ¾ = D AF
R3(DE) ¿ =
59
Example 3
R1(AC) ½ = CA
R2(CB) ¾ =
60
Decomposition and Normal Form
61
Definition: Cont’d
¯ satisfies if for every two tuples ½ ¾ in with ½ ¾, there exists a
tuple with:
½
´ µ
´ µ ½
62
Decomposition and Normal Form
Example
63
Example
64
Decomposition and Normal Form
We will need to compute all the multivalued dependencies that are logically implied by a
given set of multivalued dependencies.
65
The following set of inference rules is sound and complete. The first three rules are
Armstrong’s axioms.
66
Decomposition and Normal Form
Other Axioms
67
¯ 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 of the form , where
and :
– 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.
Decomposition algorithm is the same than for BCNF using MVD instead FD.
68
Decomposition and Normal Form
Summarize
69