Database Lect5 FD
Database Lect5 FD
Functional Dependencies
Designing Good Schemas
We know how to create schemas, but ...
how do we create good schemas?
what does good mean?
t’
6
Example
Drinkers(name, addr, beersLiked,
manf, favoriteBeer)
na me ad dr be ersLi ked ma nf fa vo riteBe er
Janeway Vo ya ger Bud A.B. Wi ckedAl e
Janeway Vo ya ger Wi ckedAl e Pete's Wi ckedAl e
Spo ck Enterpris e Bud A.B. Bud
S# P# QTY
Redundancy Example
Where’s the redundancy?
Redundancy Example
Example FDs
Partial Key FD
16
Examples of Keys
Product(name, price, category, color)
name, category price
category color
Key Key
(2 attributes)
Superkey
name
Product buys Person
Product sname
card-no
CreditCard
Person ssn
Product sname
Incomplete
card-no
CreditCard
Person ssn (what does
it say ?)
Anomalies:
Redundancy = repeat data•
Update anomalies = Fred moves to “Bellvue”•
Deletion anomalies = Fred drops all phone numbers:•
what is his city ?
Relation Decomposition
Break the relation into two:
SSN PhoneNumber
123-45-6789 206-555-1234
123-45-6789 206-555-6543
987-65-4321 908-555-2121
987-65-4321 908-555-1234
Relational Schema Design
name
Conceptual Model: Product buys Person
Relational Model:
plus FD’s
Normalization:
Eliminates anomalies
Decompositions in General
R(A1, ..., An)
Create two relations R1(B1, ..., Bm) and R2(C1, ..., Cp)
and:
R1 = projection of R on B1, ..., Bm
R2 = projection of R on C1, ..., Cp
Incorrect Decomposition
Sometimes it is incorrect:
31
Normalization
0NF
remove
remove remove
remaining
FD anomal
BCNF multivalue 4NF remaining 5NF
dependencies anomalies
dependencies
Goals of Normalization
Let R be a relation scheme with a set F of
functional dependencies.
Decide whether a relation scheme R is in “good”
form.
In the case that a relation scheme R is not in
“good” form, decompose it into a set of relation
scheme {R1, R2, ..., Rn} such that
each relation scheme is in good form
the decomposition is a lossless-join decomposition
Preferably, the decomposition should be dependency
preserving.
1 NF
NO multi-valued attributes
No composite attribute
No nested relation
Proper translation
from ER multi-value
attributes will
achieve 1NF.
Decomposition:
INSTR I_OFFICE
3NF Example
Relation dept_advisor:
dept_advisor (s_ID, i_ID, dept_name)
F = {s_ID, dept_name i_ID, i_ID
dept_name}
Two candidate keys: s_ID, dept_name, and
i_ID, s_ID
R is in 3NF
s_ID, dept_name i_ID s_ID
dept_name is a superkey
i_ID dept_name
dept_name is contained in a candidate key
Redundancy in 3NF
There is some redundancy in this schema
Example of problems due to redundancy in
3NF J L K
j1 l1 k1
R = (J, K, L)
F = {JK L, L K } j2 l1 k1
j3 l1 k1
null l2 k2
Why?
1. Guarantees no redundancy due to FD’s.
2. Guarantees no update anomalies = one
occurrence of a fact is updated, not all.
3. Guarantees no deletion anomalies = valid
fact is lost when tuple is deleted.
Boyce-Codd Normal Form
A relation schema R is in BCNF with respect to a set F of
functional dependencies if for all functional dependencies in F+ of
the form
is trivial (i.e., )
is a superkey for R
Example schema not in BCNF:
50
Example
Students(name, addr, CarLiked, manf, favCar)
FD’s: name->addr favCar, carsLiked->manf
Only key is {name, CarsLiked}.
In each FD, the left side is not a superkey.
Any one of these FD’s shows Students is not
in BCNF
51
Boyce-Codd Normal Form
We say a relation R is in BCNF if whenever
X ->A is a nontrivial FD that holds in R, X is
a superkey.
Remember: nontrivial means A is not a member
of set X.
Remember, a superkey is any superset of a key
(not necessarily a proper superset).
52
Example
Students(name, addr, CarsLiked, manf, favCar)
F = name->addr, name -> favCar, CarsLiked->manf
Pick BCNF violation name->addr.
Close the left side: {name}+ = {name, addr, favCar}.
Decomposed relations:
1. Students1(name, addr, favCar)
2. Students2(name, CarsLiked, manf)
53
3NF and BCNF
3rd Normal Form (3NF) modifies the BCNF
condition so we do not have to decompose
in this problem situation.
X ->A violates 3NF if and only if X is not a
superkey, and also A is not prime.
54
Exercises
The following relation schema
is not in third normal form (3NF).
SHIPMENT
SID FROM_CITY TO_CITY DISTANCE WEIGHT
It has 2 fields:
1. Primary key of the data file
a problem
Clustering index
data file.
same file
Example
Create a database for managing class enrollments in a single
semester. You should keep track of all students (their names, Ids,
and addresses) and professors (name, Id, department). Do not
record the address of professors but keep track of their ages.
Maintain records of courses also. Like what classroom is assigned
to a course, what is the current enrollment, and which department
offers it. At most one professor teaches each course. Each student
evaluates the professor teaching the course. Note that all course
offerings in the semester are unique, i.e. course names and
numbers do not overlap. A course can have ≥ 0 pre-requisites,
excluding itself. A student enrolled in a course must have enrolled in
all its pre-requisites. Each student receives a grade in each course.
The departments are also unique, and can have at most one
chairperson (or dept. head). A chairperson is not allowed to head
two or more departments.
Example
Create a database for managing class enrollments in a single
semester. You should keep track of all students (their names, Ids,
and addresses) and professors (name, Id, department). Do not
record the address of professors but keep track of their ages.
Maintain records of courses also. Like what classroom is assigned
to a course, what is the current enrollment, and which department
offers it. At most one professor teaches each course. Each student
evaluates the professor teaching the course. Note that all course
offerings in the semester are unique, i.e. course names and
numbers do not overlap. A course can have ≥ 0 pre-requisites,
excluding itself. A student enrolled in a course must have enrolled in
all its pre-requisites. Each student receives a grade in each course.
The departments are also unique, and can have at most one
chairperson (or dept. head). A chairperson is not allowed to head
two or more departments.