Professional Documents
Culture Documents
CSE 303 Lec 10 DesignTheory
CSE 303 Lec 10 DesignTheory
Lecture 10
Telephone
Customer ID First Name Surname
Number
123 Robert Ingram 555-861-2025
555-403-1659
456 Jane Wright
555-776-4100
789 Maria Fernandez 555-808-9633
Telephone
Customer ID First Name Surname
Number
123 Robert Ingram 555-861-2025
456 Jane Wright 555-403-1659
456 Jane Wright 555-776-4100
789 Maria Fernandez 555-808-9633
Telephone
Customer ID First Name Surname
Number
123 Robert Ingram 555-861-2025
456 Jane Wright 555-403-1659
456 Jane Wright 555-776-4100
789 Maria Fernandez 555-808-9633
Normalization:
Eliminates anomalies
CSE 303: Ashikur Rahman 15
Data Anomalies
When a database is poorly designed we get anomalies:
One person may have multiple phones, but lives in only one city
Anomalies:
• Redundancy = repeated data
• Update anomalies = Fred moves to “Bellevue”
• Deletion anomalies = Joe deletes his phone number:
what is his city ?
17
CSE 303: Ashikur Rahman
Relation Decomposition
Break the relation into two:
Name SSN PhoneNumber City
Fred 123-45-6789 206-555-1234 Seattle
Fred 123-45-6789 206-555-6543 Seattle
Joe 987-65-4321 908-555-2121 Westfield
R
A1 ... Am B1 ... Bm
t’
How about?
title, year starName
CSE 303: Ashikur Rahman 24
Examples
An FD holds, or does not hold on an instance:
Position Phone
name color
category department name, category price
color, category price
CSE 303: Ashikur Rahman 28
Armstrong’s Rules (1/5)
A1 ... An B1 ... Bm
A1, A2, …, An B1
A1, A2, …, An B2
.....
A1, A2, …, An Bm
CSE 303: Ashikur Rahman 29
Armstrong’s Rules (2/5)
where i = 1, 2, ..., n
A1 … An
Why ?
30
CSE 303: Ashikur Rahman
Armstrong’s Rules (3/5)
A1 … An
Why ?
33
CSE 303: Ashikur Rahman
Armstrong’s Rules (5/5)
If
A1 … An B1 … Bm C1 ... Ck
name color
category department name, category price
color, category price
35
X={A1, …, An}.
if B1, …, Bn C is a FD and
B1, …, Bn are all in X
then add C to X.
39
Closure Algorithm
X={A1, …, An}.
Repeat until X doesn’t change do: {name, category}+ =
if B1, …, Bn C is a FD and {name, category}
B1, …, Bn are all in X
then add C to X.
{name} {color}
{category} {dept}
{name} {color}
{category} {dept}
{name, category}+ =
{name} {color}
{name, category, color, dept}
{category} {dept}
{name, category}+ =
{name} {color}
{name, category, color, dept}
{category} {dept}
{name, category}+ =
{name, category, color, dept, price}
{color, category} {price}
43
More Examples
In class:
R(A,B,C,D,E,F) AB C
AD E
B D
AF B
R(A,B,C,D,E,F) AB C
AD E
B D
AF B
R(A,B,C,D,E,F) AB C
AD E
B D
AF B
R(A,B,C,D,E,F) AB C
BC AD
D E
CF B
R(A,B,C,D) AB C
BC D
AD C
CD A
AD B
1. Compute {A,D}+
R(A,B,C,D) AB C
BC D BD A
CD A
AD B
A+ = A, B+ = B, C+ = C, D+ = D
AB+ =ABCD, AC+=AC, AD+=ABCD,
What are all the keys?
BC+=ABCD, BD+=BD, CD+=ABCD
ABC+ = ABD+ =What + = ABCD (no need to compute– why ?)
ACDare all the superkeys that are not keys?
BCD+ = ABCD, ABCD+ = ABCD
A+ = A, B+ = B, C+ = C, D+ = D
AB+ =ABCD, AC+=AC, AD+=ABCD,
What are all the keys?
BC+=ABCD, BD+=BD, CD+=ABCD
ABC+ = ABD+ =What + = ABCD (no need to compute– why ?)
ACDare all the superkeys that are not keys?
BCD+ = ABCD, ABCD+ = ABCD
AB C
Keys:
AD B
AB , AD
B D
A+ = A, B+ = BD, C+ = C, D+ = D
AB+ =ABCD, AC+=AC, AD+=ABCD,
What are all the keys?
BC+=BCD, BD+=BD, CD+=CD
ABC+ = ABD+ What
= ACD + = ABCD (no need to compute– why ?)
are all the superkeys that are not keys?
BCD+ = BCD, ABCD+ = ABCD
AB C
Superkeys that are not Keys:
AD B
ABC , ABD, ACD, ABCD
B D
A+ = A, B+ = BD, C+ = C, D+ = D
AB+ =ABCD, AC+=AC, AD+=ABCD,
What are all the keys?
BC+=BCD, BD+=BD, CD+=CD
ABC+ = ABD+ What
= ACD + = ABCD (no need to compute– why ?)
are all the superkeys that are not keys?
BCD+ = BCD, ABCD+ = ABCD
student address
room, time course
student, course room, time
• X A is OK if X is a (super)key
• X A is not OK otherwise
In other words: there are no “bad” FDs, that is the left side of
every nontrivial FD must be a super key.
Equivalently:
" X, either (X+ = X) or (X+ = all attributes)
CSE 303: Ashikur Rahman 69
All two-attribute relations are in BCNF
R(A,B)
Case 1: Suppose there is no dependency
Nothing is being violated, so fine
In practice, we have
R1 R2 71
a better algorithm (coming up)
Example
Name SSN PhoneNumber City
Fred 123-45-6789 206-555-1234 Seattle
Fred 123-45-6789 206-555-6543 Seattle
Joe 987-65-4321 908-555-2121 Westfield
Joe 987-65-4321 908-555-1234 Westfield
let Y = X+ - X
let Z = [all attributes] - X+
decompose R into R1(X Y) and R2(X Z)
continue to decompose recursively R1 and R2
CSE 303: Ashikur Rahman 74
R(A,B,C,D) Find X s.t.: X ≠X+ ≠ [all attributes]
A B
Example BC
R(A,B,C,D)
A+ = ABC ≠ ABCD
R1(A,B,C) R2(A,D)
B+ = BC ≠ ABC
What are
R11(B,C) R12(A,B) the keys ?
Iteration 1: Person
SSN+ = SSN, name, age, hairColor
Decompose into: P(SSN, name, age, hairColor)
Phone(SSN, phoneNumber)
Iteration 2: P
age+ = age, hairColor What are
Decompose: People(SSN, name, age) the keys ?
Hair(age, hairColor)
Phone(SSN, phoneNumber) 76
Decompositions in General
R(A1, ..., An, B1, ..., Bm, C1, ..., Cp)
R1(A1, ..., An, B1, ..., Bm) R2(A1, ..., An, C1, ..., Cp)