Database Design: Wednesday, January 25, 2006
Database Design: Wednesday, January 25, 2006
Database Design
Wednesday, January 25, 2006
B
s.t. A11, , Ann B
Example:
name
name
color
color
category
category department
department
color,
color,category
category
price
price
Closures:
name+ = {name, color}
{name, category}+ = {name, category, color, department, price}
color+ = {color}
2
Closure Algorithm
Start
Startwith
withX={A1,
X={A1,,
,An}.
An}.
Example:
Repeat
Repeatuntil
untilXXdoesnt
doesntchange
change do:
do:
name
name
color
color
category
category
department
department
color,
category
color, category
price
price
BB11,,,
,BBnn
CC isisaaFD
FDand
and
BB1,,,
B
are
all
in
X
,
B
n
are
all
in
X
1
n
then
then add
addCCto
toX.
X.
ifif
{name, category}+ =
{ name, category, color, department, price }
Hence:
name,
name,category
category
color,
color,department,
department,price
price
Example
In class:
R(A,B,C,D,E,F)
Compute {A,B}+
A,
A,BB
CC
A,
D
A, D EE
BB
DD
A,
F
A, F BB
X = {A, B,
}
4
A,
A,BB
CC
A,
A,DD
BB
BB
DD
Another Example
Enrollment(student, major, course, room, time)
student major
major, course room
course time
Normal Forms
First Normal Form = all attributes are atomic
Second Normal Form (2NF) = old and obsolete
Third Normal Form (3NF) = will discuss
Boyce Codd Normal Form (BCNF) = will discuss
Others...
9
Keys
A superkey is a set of attributes A1, ..., An s.t. for
any other attribute B, we have A1, ..., An B
A key is a minimal superkey
I.e. set of attributes which is a superkey and for which
no subset is a superkey
10
Computing (Super)Keys
Compute X+ for all sets X
If X+ = all attributes, then X is a key
List only the minimal Xs
11
Example
Product(name, price, category, color)
name,
name, category
category
price
price
category
category
color
color
What is the key ?
12
Example
Product(name, price, category, color)
name,
name, category
category
price
price
category
category
color
color
What is the key ?
(name, category) + = name, category, price, color
Hence (name, category) is a key
13
Examples of Keys
Enrollment(student, address, course, room, time)
student
student
address
address
room,
room,time
time
course
course
student,
student,course
course
room,
room,time
time
14
Eliminating Anomalies
Main idea:
X A is OK if X is a (super)key
X A is not OK otherwise
15
Example
Name
Fred
Fred
Joe
Joe
SSN
123-45-6789
123-45-6789
987-65-4321
987-65-4321
PhoneNumber
206-555-1234
206-555-6543
908-555-2121
908-555-1234
City
Seattle
Seattle
Westfield
Westfield
SSN
SSN
Name,
Name,City
City
What the key?
{SSN, PhoneNumber}
Key or Keys ?
Can we have more than one key ?
Given R(A,B,C) define FDs s.t. there are two
or more keys
17
Key or Keys ?
Can we have more than one key ?
Given R(A,B,C) define FDs s.t. there are two
or more keys
ABC
ABC
BCA
BCA
or
ABC
ABC
BAC
BAC
18
Bs
R1
As
Others
R2
Is there a
2-attribute
relation that is
not in BCNF ?
In practice, we have
20
a better algorithm (coming up)
10
Example
Name
Fred
Fred
Joe
Joe
SSN
123-45-6789
123-45-6789
987-65-4321
987-65-4321
PhoneNumber
206-555-1234
206-555-6543
908-555-2121
908-555-1234
City
Seattle
Seattle
Westfield
Westfield
SSN
SSN
Name,
Name,City
City
What the key?
{SSN, PhoneNumber}
21
Example
Name
SSN
City
Fred
123-45-6789 Seattle
Joe
987-65-4321 Westfield
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
22
11
Example Decomposition
Person(name, SSN, age, hairColor, phoneNumber)
SSN name, age
age hairColor
Decompose in BCNF (in class):
23
ifif (not
(not found)
found) then
then R
R isis in
in BCNF
BCNF
+
let
let Y
Y == X
X+ -- X
X
+
let
Z
=
[all
attributes]
let Z = [all attributes]-- X
X+
decompose
decompose RR into
into R1(X
R1(X
Y)
Y) and
and R2(X
R2(X
Z)
Z)
continue
continue to
to decompose
decompose recursively
recursively R1
R1 and
and R2
R2
24
12
R(A,B,C,D)
Example
A+
25
A
A
BB
BB
CC
R(A,B,C,D)
= ABC ABCD
R1(A,B,C)
= BC ABC
B+
R11(B,C)
What are
the keys ?
R12(A,B)
R2(A,D)
What are
the keys ?
13
Decompositions in General
R(A
R(A11,,...,
...,AAnn,,BB11,,...,
...,BBmm,,CC11,,...,
...,CCpp))
RR11(A
(A11,,...,
...,AAnn,,BB11,,...,
...,BBmm))
RR22(A
(A11,,...,
...,AAnn,,CC11,,...,
...,CCpp))
Theory of Decomposition
Sometimes it is correct:
Name
Price
Category
Gizmo
19.99
Gadget
OneClick
24.99
Camera
Gizmo
19.99
Camera
Name
Price
Name
Category
Gizmo
19.99
Gizmo
Gadget
OneClick
24.99
OneClick
Camera
Gizmo
19.99
Gizmo
Camera
Lossless decomposition
28
14
Incorrect Decomposition
Sometimes it is not:
Name
Price
Category
Gizmo
19.99
Gadget
OneClick
24.99
Camera
Gizmo
19.99
Camera
Whats
incorrect ??
Name
Category
Price
Gizmo
Gadget
19.99
Gadget
OneClick
Camera
24.99
Camera
Gizmo
Camera
19.99
Camera
Category
Lossy decomposition
29
Decompositions in General
R(A
R(A11,,...,
...,AAnn,,BB11,,...,
...,BBmm,,CC11,,...,
...,CCpp))
RR11(A
(A11,,...,
...,AAnn,,BB11,,...,
...,BBmm))
RR22(A
(A11,,...,
...,AAnn,,CC11,,...,
...,CCpp))
30
15
Company
Product
Unit
Unit
Company
Company
Company,
Company,Product
Product
Unit
Unit
Unit+ = Unit, Company
Unit
Company
Unit
Product
Unit
Unit
Company
Company
We loose the FD: Company, Product Unit !!
31
Company
Galaga99 UW
Bingo
UW
Unit
Product
Galaga99
Bingo
Databases
Databases
Unit
Unit
Company
Company
No problem so far. All local FDs are satisfied.
Lets put all the data back into a single table again:
Unit
Company
Product
Galaga99
Bingo
UW
UW
Databases
Databases
Company,
Company,Product
Product
Unit
Unit
32
16
The Problem
We started with a table R and FD
We decomposed R into BCNF tables R1, R2,
with their own FD1, FD2,
We can reconstruct R from R1, R2,
But we cannot reconstruct FD from FD1, FD2,
33
34
17
ifif (not
(not found)
found) then
then R
R isis already
already in
in 3NF
3NF
+
let
let Y
Y == X
X+ -- X
X -- K
K
Z
=
[all
attributes]
let
(X
Y)
Y)
let Z = [all attributes]-- (X
decompose
into
R1(X
Y)
and
decompose into R1(X Y) and R2(X
R2(X
Z)
Z)
decompose,
decompose, recursively,
recursively, R1
R1 and
and R2
R2
35
Pick X = C
C+ = BCDE
C BDE is a BCNF violation
For 3NF: remove B, D (part of K):
C E is a 3NF violation
Decompose: R1(C, E), R2(A,B,C,D)
R1 is in 3NF
R2 is in 3NF (because its keys: AB, AC, AD)
36
18
BCNF
37
19
Person
buys
price
name
ssn
date
39
Product
name
card-no
Purchase
CreditCard
Person
Store
ssn
40
20
sname
Product
name
card-no
Purchase
CreditCard
Person
Store
ssn
Incomplete
(what does
it say ?)
42
21