Lecture05 PDF
Lecture05 PDF
Lecture05 PDF
Outline
• Functional dependencies and keys (3.5)
• Normal forms: BCNF (3.7)
1
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
Functional Dependencies
Definition:
2
Examples
EmpID Name Phone Position
E0045 Smith 1234 Clerk
E1847 John 9876 Salesrep
E1111 Smith 9876 Salesrep
E9999 Mary 1234 lawyer
In General
• To check A B, erase all other columns
… A … B
X1 Y1
X2 Y2
… …
3
Example
More Examples
Product: name price, manufacturer
Person: ssn name, age
Company: name stock price, president
4
Finding the Keys of a Relation
Given a relation constructed from an E/R diagram, what is its key?
Rules:
1. If the relation comes from an entity set,
the key of the relation is the set of attributes which is the
key of the entity set.
name
Product buys Person
5
Finding the Keys
Except: if there is an arrow from the relationship to E, then
we don’t need the key of E as part of the relation key.
Product sname
card-no
Payment Method
Person ssn
6
Rules for FD’s
Why ?
7
Rules in FD’s (continued)
Closure Algorithm
Start with X={A1, …, An}.
if B , B , … B C is in S, and
1 2 n
B,B,…Bn are all in X, and
1 2
C is not in X
then
add C to X.
8
Example
Book Author Author_age
{Book} ->{Author} (if we know the book, we knows the author name)
{Author} does not ->{Book}
{Author} -> {Author_age}
Rule of transitive dependency: {Book} -> {Author_age}
- That makes sense because if we know the book name we can know the
author’s age.
9
Relational Schema Design
(or Logical Design)
Main idea:
• Start with some relational schema
• Find out its FD’s
• Use them to design a better relational
schema
Relational Model:
(plus FD’s)
Normalization:
10
Relational Schema Design
Goal: eliminate anomalies
• Redundancy anomalies
• Deletion anomalies
• Update anomalies
11
Relation Decomposition
Break the relation into two:
SSN Name
123-321-99 Fred
909-438-44 Joe
Decompositions in General
Let R be a relation with attributes A , A , … A
1 2 n
Such that:
B1, B2, … B m È C1, C2, … C l = A1, A2, … A n
And
-- R1 is the projection of R on B1, B2, … B m
12
Incorrect Decomposition
• Sometimes it is incorrect:
Name Price Category
Gizmo 19.99 Gadget
OneClick 24.99 Camera
DoubleClick 29.99 Camera
Incorrect Decomposition
13
Normal Forms
First Normal Form = all attributes are atomic
Others...
14
Example
Name SSN Phone Number
123-321-99 Fred
909-438-44 Joe SSN Name
15
What About This?
Name Price Category
It is already in BCNF
BCNF Decomposition
Find a dependency that violates the BCNF condition:
A1, A2, … A n B1, B2, … B m
Heuristics: choose B1 , B2, … Bm“as large as possible”
Decompose:
Continue until
Others A’s B’s there are no
Find a BCNF violations
2-attribute left.
relation that is
not in BCNF. R1 R2
16
Example Decomposition
Person:
Name SSN Age EyeColor PhoneNumber
Functional dependencies:
SSN Name, Age, Eye Color
Age Draft-worthy
Other Example
• R(A,B,C,D) A B, B C
• Key: A, D
• Violations of BCNF: A B, A C, A BC
• Pick A BC: split into R1(A,BC) R2(A,D)
• What happens if we pick A B first ?
17
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)
18