Professional Documents
Culture Documents
Relational Database Design
Relational Database Design
2
1. Normal forms & functional
dependencies
3
What you will learn about in this section
1. Overview of design theory & normal forms
3. Functional dependencies
4
Design Theory
• Design theory is about how to represent your data to avoid
anomalies.
DB designs based on
• Boyce-Codd Normal Form (BCNF) functional Our focus in
dependencies, this lecture
intended to prevent + next one
• 3rd Normal Form (3NF) data anomalies
If t1,t2 agree here.. …they also agree here! then ti[B1] = tj[B1] AND ti[B2]=tj[B2]
AND … AND ti[Bn] = tj[Bn]
FDs for Relational Schema Design
• High-level idea: why do we care about FDs?
23
More Examples
{Position} {Phone}
24
More Examples
25
ACTIVITY
Find at least three FDs which
A B C D E
hold on this instance:
1 2 4 3 6
3 2 5 1 8 { } { }
{ } { }
1 4 4 5 7 { } { }
1 2 4 3 6
3 2 5 1 8
26
2. Finding functional
dependencies
27
What you will learn about in this section
1. “Good” vs. “Bad” FDs: Intuition
2. Finding FDs
3. Closures
28
“Good” vs. “Bad” FDs
We can start to develop a notion of good vs. bad FDs:
29
“Good” vs. “Bad” FDs
We can start to develop a notion of good vs. bad FDs:
2. Find out its functional dependencies (FDs) This part can be tricky!
Given the provided FDs, we can see that {Name, Category} {Price}
must also hold on any instance…
38
Closure Algorithm
{name, category}+ =
{name, category}
F=
{name} {color}
{category} {dept}
{name, category}+ =
{name, category, color}
F=
{name} {color}
{category} {dept}
{name, category}+ =
{name, category, color}
F= {name, category}+ =
{name} {color} {name, category, color, dept}
{category} {dept}
{name, category}+ =
{name, category, color}
F= {name, category}+ =
{name} {color} {name, category, color, dept}
{category} {dept}
{name, category}+ =
{name, category, color, dept, price}
{color, category} {price}
42
Example
R(A,B,C,D,E,F) {A,B} {C}
{A,D} {E}
{B} {D}
{A,F} {B}
46
Using Closure to Infer ALL FDs
Example: {A,B} C
Given F =
Step 1: Compute X+, for every set of attributes X: {A,D} B
{B} D
{A}+ = {A}
{B}+ = {B,D}
{C}+ = {C}
{D}+ = {D}
{A,B}+ = {A,B,C,D}
{A,C}+ = {A,C}
{A,D}+ = {A,B,C,D} No need to
{A,B,C}+ = {A,B,D}+ = {A,C,D}+ = {A,B,C,D} {B,C,D}+ = compute these-
{B,C,D} why?
{A,B,C,D}+ = {A,B,C,D}
47
Using Closure to Infer ALL FDs
Example: {A,B} C
Given F =
Step 1: Compute X+, for every set of attributes X: {A,D} B
{B} D
{A}+ = {A}, {B}+ = {B,D}, {C}+ = {C}, {D}+ = {D}, {A,B}+ =
{A,B,C,D}, {A,C}+ = {A,C}, {A,D}+ = {A,B,C,D}, {A,B,C}+ =
{A,B,D}+ = {A,C,D}+ = {A,B,C,D}, {B,C,D}+ = {B,C,D},
{A,B,C,D}+ = {A,B,C,D}
51
1. Boyce-Codd Normal Form
52
What you will learn about in this section
1. Conceptual Design
53
Conceptual Design
Back to Conceptual Design
Now that we know how to find FDs, it’s a straight-forward process:
59
Example
Name SSN City {SSN} {Name,City}
Fred 123-45-6789 Seattle
Joe 987-65-4321 Madison This FD is now
good because it is
SSN PhoneNumber the key
123-45-6789 206-555-1234
123-45-6789 206-555-6543 Let’s check
987-65-4321 908-555-2121 anomalies:
987-65-4321 908-555-1234 • Redundancy ?
• Update ?
Now in BCNF! • Delete ?
60
BCNF Decomposition Algorithm
BCNFDecomp(R):
Find X s.t.: X+ ≠ X and X+ ≠ [all attributes]
let Y = X+ - X, Z = (X+)C
decompose R into R1(X ∪ Y) and R2(X ∪ Z)
61
BCNF Decomposition Algorithm
BCNFDecomp(R):
Find a set of attributes X s.t.: X+ ≠ X and X+ ≠ Find a set of attributes X
[all attributes] which has non-trivial
“bad” FDs, i.e. is not a
if (not found) then Return R superkey, using closures
let Y = X+ - X, Z = (X+)C
decompose R into R1(X ∪ Y) and R2(X ∪ Z)
let Y = X+ - X, Z = (X+)C
decompose R into R1(X ∪ Y) and R2(X ∪ Z)
let Y = X+ - X, Z = (X+)C
decompose R into R1(X ∪ Y) and R2(X ∪ Z)
Proceed recursively until no
more “bad” FDs!
Return BCNFDecomp(R1), BCNFDecomp(R2)
65
Example
BCNFDecomp(R): R(A,B,C,D,E)
Find a set of attributes X s.t.: X+ ≠ X and X+ ≠
[all attributes]
{A} {B,C}
if (not found) then Return R {C} {D}
let Y = X+ - X, Z = (X+)C
decompose R into R1(X ∪ Y) and R2(X ∪ Z)
{A} {B,C}
R(A,B,C,D,E) {C} {D}
{A}+ = {A,B,C,D} ≠ {A,B,C,D,E}
R1(A,B,C,D)
{C}+ = {C,D} ≠ {A,B,C,D}
68
What you will learn about in this section
1. Lossy & Lossless Decompositions
69
Recap: Decompose to remove redundancies
1. We saw that redundancies in the data (“bad FDs”) can lead to data
anomalies
R(A1,...,An,B1,...,Bm,C1,...,Cp)
R1(A1,...,An,B1,...,Bm) R2(A1,...,An,C1,...,Cp)
73
A problem with BCNF
We do a BCNF decomposition
Unit Company Unit Product
on a “bad” FD:
… … … … {Unit}+ = {Unit, Company}
{Unit} {Company}
{Unit} {Company}