Functional Dependencies and Relational Schema Design
Functional Dependencies and Relational Schema Design
name
Product
price
Relational Model:
(plus FDs)
Normalization:
Person
buys
name
ssn
Functional Dependencies
A form of constraint (hence, part of the
schema)
Finding them is part of the database design
Also used in normalizing the relations
Outline
Functional dependencies and keys (3.4,3.5)
Normal forms: BCNF (3.6)
Functional Dependencies
Definition:
If two tuples agree on the attributes
A1, A2, A n
then they must also agree on the attributes
B1, B2, B m
Formally:
A1, A2, A n
B1, B2, B m
Examples
EmpID
E0045
E1847
E1111
E9999
Name
Smith
John
Smith
Mary
Phone
1234
9876
9876
1234
Position
Clerk
Salesrep
Salesrep
lawyer
EmpID
Name, Phone, Position
Position
Phone
but Phone
Position
In General
To check A
A B
X1
Y1
X2
Y2
Example
EmpID
E0045
E1847
E1111
E9999
Name
Smith
John
Smith
Mary
Phone
1234
9876
9876
1234
Position
Clerk
Salesrep
Salesrep
lawyer
More Examples
Product: name
Person:
ssn
Company: name
price, manufacturer
name, age
stock price, president
Person
address
name
ssn
Person
buys
price
name
date
ssn
Product
name
card-no
Purchase
Payment Method
Person
Store
ssn
B1, B2, B m
Is equivalent to
A1, A2, A n
B1
A1, A2, A n
B2
A1, A2, A n
Bm
Splitting rule
and
Combing rule
A1, A2, A n
Why ?
Ai
Trivial Rule
A1, A2, A n
and
B1, B2, B m
C1, C2 , C
then
A1, A2, A n
C1, C2 , C
Why ?
B1, B2 , B
Closure Algorithm
Start with X={A1, , An}.
Repeat until X doesnt change do:
if B1, B2, B n
B,B,Bn
1
C is not in X
then
add C to X.
C is in S, and
are all in X, and
Example
A B
A D
B
A F
C
E
D
B
Closure of {A,B}:
X = {A, B,
X = {A, F,
name
Product
price
Relational Model:
(plus FDs)
Normalization:
Person
buys
name
ssn
Anomalies:
123-321-99
123-321-99
909-438-44
909-438-44
(201)
(206)
(908)
(212)
555-1234
572-4312
464-0028
555-4000
Redundancy
= repeat data
update anomalies = need to update in many places
deletion anomalies = need to delete many tuples
Relation Decomposition
Break the relation into two:
SSN
Name
123-321-99
909-438-44
SSN
123-321-99
123-321-99
909-438-44
909-438-44
Fred
Joe
Phone Number
(201)
(206)
(908)
(212)
555-1234
572-4312
464-0028
555-4000
Decompositions in General
Let R be a relation with attributes A , A , A
1
2
n
Create two relations R1 and R2 with attributes
B1, B2, B m
Such that:
B1, B2, B m
C1, C2, C l
C1, C2, C l
And
-- R1 is the projection of R on
-- R2 is the projection of R on
A1, A2, A n
B1, B2, B m
C1, C2, C l
Incorrect Decomposition
Name
Price
Category
Gizmo
19.99
Gadget
OneClick
24.99
Camera
DoubleClick
29.99
Camera
Category
Price
Category
Gizmo
Gadget
19.99
Gadget
OneClick
Camera
24.99
Camera
DoubleClick
Camera
29.99
Camera
Name
Price
Category
Gizmo
19.99
Gadget
OneClick
24.99
Camera
OneClick
29.99
Camera
DoubleClick
24.99
Camera
DoubleClick
29.99
Camera
Normal Forms
First Normal Form = all attributes are atomic
Second Normal Form (2NF) = old and obsolete
Third Normal Form (3NF) = this lecture
Boyce Codd Normal Form (BCNF) = this lecture
Others...
Example
Name
Fred
Fred
Joe
Joe
SSN
123-321-99
123-321-99
909-438-44
909-438-44
Phone Number
(201)
(206)
(908)
(212)
555-1234
572-4312
464-0028
555-4000
Name
Fred
Joe
SSN
Phone Number
(201)
(206)
(908)
(212)
555-1234
572-4312
464-0028
555-4000
Name
Price
Category
Gizmo
OneClick
$19.99
$24.99
gadgets
camera
Name
Price, Category
BCNF Decomposition
Find a dependency that violates the BCNF condition:
A1, A2, A n
B1, B2, B m
R1
As
Bs
R2
Continue until
there are no
BCNF violations
left.
Example Decomposition
Person:
Name SSN Age EyeColor PhoneNumber
Functional dependencies:
SSN
BNCF:
Draft-worthy
Other Example
R(A,B,C,D)
B,
Key:
Violations of BCNF:
Pick
: split into R1( ) R2( )
Correct Decompositions
A decomposition is lossless if we can recover:
R(A,B,C)
R1(A,B)
R2(A,C)
R(A,B,C) = R(A,B,C)
R is in general larger than R. Must ensure R = R