06a - Normalization
06a - Normalization
Basic Concepts
Study following relation
10 Main
10 Main
20 State
20 State
30 Elm
Basic Concepts
Apple
Orange
Grape
Apple
Mango
Price )
$2.00
2.50
1.25
2.25
6.00
John
John
Jane
Jane
Frank
10 Main
10 Main
20 State
20 State
30 Elm
Apple
Orange
Grape
Apple
Mango
Price )
$2.00
2.50
1.25
2.25
6.00
Basic Concepts
What happens if John moves?
SP ( Supp-Name, Supp-Addr, Item,
John
John
Jane
Jane
Frank
10 Main
10 Main
20 State
20 State
30 Elm
Apple
Orange
Grape
Apple
Mango
Basic Concepts
$2.00
2.50
1.25
2.25
6.00
John
John
Jane
Jane
Frank
5
10 Main
10 Main
20 State
20 State
30 Elm
Apple
Orange
Grape
Apple
Mango
Price )
$2.00
2.50
1.25
2.25
6.00
6
06 - Normalization to 3NF
Basic Concepts
Basic Concepts
John
John
Jane
Jane
Frank
10 Main
10 Main
20 State
20 State
30 Elm
Apple
Orange
Grape
Apple
Mango
Price )
$2.00
2.50
1.25
2.25
6.00
Basic Concepts
What if we wish to keep track of a new
supplier and the address but do not
know what they will be supplying?
SP ( Supp-Name, Supp-Addr, Item,
John
John
Jane
Jane
Frank
10 Main
10 Main
20 State
20 State
30 Elm
Apple
Orange
Grape
Apple
Mango
10 Main
10 Main
20 State
20 State
30 Elm
Apple
Orange
Grape
Apple
Mango
John
John
Jane
Jane
Frank
$2.00
2.50
1.25
2.25
6.00
Basic Concepts
Price )
$2.00
2.50
1.25
2.25
6.00
John
John
Jane
Jane
Frank
Price )
10 Main
10 Main
20 State
20 State
30 Elm
Apple
Orange
Grape
Apple
Mango
Price )
$2.00
2.50
1.25
2.25
6.00
10
Redundancy
Anomalies
11
Update Anomaly
caused by redundant information
must find all copies of information in order
to prevent inconsistencies when updating
12
06 - Normalization to 3NF
Anomalies
Deletion Anomaly
Anomalies
Insertion Anomaly
13
Fixing Anomalies
Decompose the relation
SP ( Supp-Name, Supp-Addr, Item, Price )
Supp( Supp-Name, Supp-Addr)
John
10 Main
Jane
20 State
Frank
30 Elm
SP-Item ( Supp-Name, Item, Price )
John
John
Jane
Jane
Frank
Apple $2.00
Orange 2.50
Grape 1.25
Apple 2.25
15
Mango 6.00
Decomposition of Relations
14
Fixing Anomalies:
Decomposing Relations
Mango
6.00
16
Relational Design
Methodology
Disadvantages
How do we tell whether one relation is
better than another?
17
18
06 - Normalization to 3NF
Functional Dependency
Functional dependency
Functional Dependency
Functional dependency
20
Functional Dependency
Functional Dependency
Functional dependency
22
Functional Dependency
Functional Dependency
p1
p2
p3
p3
s1
s2
s1
s1
d1
d2
d1
d1
c1
c2
c3
c3
Supv, Dept
Dept }
24
06 - Normalization to 3NF
Insert Anomaly:
We cannot enter a new employee
say e4 who works for supervisor
s4 until e4 works on some project.
p1
p2
p3
p3
s1
s2
s1
s1
d1
d2
d1
d1
c1
c2
c3
c3
Deletion Anomaly:
If e2 does not work on project
p2 and we delete this tuple, we
will loose ALL information about
e2s supervisor, etc.
R1 (A, B, C, E, F)
FD = { A B, C, E, F }
FD2 = { A, d1 d2, d3 }
25
26
Normal Forms
e1
e2
e1
e3
p1
p2
p3
p3
s1
s2
s1
s1
d1
d2
d1
d1
c1
c2
c3
c3
Supv, Dept
Supv
Dept }
27
28
Definitions
Definitions
Super Key:
Candidate Key:
a minimum set of attributes that uniquely
identify a tuple (can be > 1 in a relation)
a minimal super key
Primary Key:
one and only one per relation.
a chosen candidate key
29
30
06 - Normalization to 3NF
Definitions
Definitions
Prime Attribute
Fully Dependent
Non-Prime Attribute
an attribute not appearing in a key of a
relation.
Normal Forms
32
e1
e2
e1
e3
p1
p2
p3
p3
FD = { Emp-ID, Project
Emp-ID
33
Supv
s1
s2
s1
s1
d1
d2
d1
d1
c1
c2
c3
c3
Supv, Dept
Dept }
Supv, Dept
Dept }
Case }
Case }
35
36
06 - Normalization to 3NF
Problems with
Second Normal Form
EP1 ( Emp-ID, Supv, Dept)
e1
s1
d1
e2
s2
d2
e3
s1
d1
p1
c1
e2
p2
c2
e1
p3
c3
e3
p3
c3
Insertion Anomaly:
We cannot insert the information that
supervisor s6 works for dept d6 unless
at least one employee works for s6.
EP1 ( Emp-ID, Supv, Dept)
e1
s1
d1
e2
s2
d2
e3
s1
d1
Update Anomaly:
If supervisor s1 is
moved to d4, we have
to make changes in
more than one tuple.
p1
c1
e2
p2
c2
e1
p3
c3
e3
p3
c3
Deletion Anomaly:
If e2 is fired,
information that s2
works for d2 is lost.
37
Definitions
38
Normal Forms
1NF (First Normal Form)
Transitively Dependent
A non-prime attribute is transitively
dependent upon the primary key of a
relation if there is also a non-prime (non
key) attribute that functionally determines
the attribute.
In EP1, Dept is transitively dependent upon
Emp-ID since
Emp-ID Supv
Supv Dept
39
40
Supv
FD1 = { Emp-ID
Supv, Dept
Dept }
Supv, Dept
EP2 ( Emp-ID, Project, Case)
Supv
Dept }
Case }
Final Result
EP1-1( Supv, Dept)
of
Normalization FD1-1 = { Supv Dept }
FD1-2 = { Emp-ID
Supv }
41
Supv }
42
06 - Normalization to 3NF
Supv
Supv, Dept
Dept }
FD1 = { Emp-ID
Supv
Supv, Dept
Dept }
Case }
Supv }
43