7-rel-db-design-07-norm2-slides
7-rel-db-design-07-norm2-slides
Normalization
Functional dependencies
Basic normal forms
First Normal Form (1NF)
Second Normal Form (2NF)
Third Normal Form (3NF)
Finding functional dependencies
Second Normal Form
E xample
AB BE DF {B, C} A
A
AC {B, C} D
B (AK1.1) AD {B, C} F
C (AK1.2)
D AE
E AF
F
Second Normal Form
E xample
AB BE DF {B, C} A
A
AC {B, C} D
B (AK1.1) AD {B, C} F
C (AK1.2)
D AE
E AF
F
Second Normal Form
E xample
AB BE DF {B, C} A
A
AC {B, C} D
B (AK1.1) AD {B, C} F
C (AK1.2)
D AE
E AF
F
Second Normal Form
E xample
AB BE DF {B, C} A
A
AC {B, C} D
B (AK1.1) AD {B, C} F
C (AK1.2)
D AE
E AF
F
Second Normal Form
E xample
Example
Fix2NF
AB BE DF {B, C} A
A
B
AC {B, C} D
B (AK1.1) AD {B, C} F
C (AK1.2) B
E
D E
AE
E AF
F
Determinant = key
Second Normal Form
Currently checking
Currently checking
p lays in /
• Player 1 League
p lays in /
is played by is played by
Candidate key #2
Co mp e tition Match
League
Date (AK1.1)
Candidate key #1 Player 1 (FK) is played for / League code
P has
Player 2 (FK) (AK1.2)
League (FK)
Frames won player 1
Frames won player 2
Non-key attributes Highest break player 1 Co mp e tition Frame
Highest break player 2
Competition points player 1 c ontains / Date (FK)
Competition points player 2 is p art of P Player 1 (FK)
Frame #
Player 1 score
Player 2 score
p lays in /
• Player 1 League
p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) is played for / League code
P has
Player 2 (FK) (AK1.2)
League (FK)
Frames won player 1
Frames won player 2
Highest break player 1 Co mp e tition Frame
Highest break player 2
Competition points player 1 c ontains / Date (FK)
Competition points player 2 is p art of P Player 1 (FK)
Frame #
Player 1 score
Identical values Player 2 score
Different values
Currently checking Different values
Date Player 1
?
Player 2
?
League Frames 1 Frames 2 HB 1
?
HB 2 Points 1 Points 2
p lays in /
• Player 1 League
p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) is played for / League code
P has
Player 2 (FK) (AK1.2)
League (FK)
Frames won player 1
Frames won player 2
Highest break player 1 Co mp e tition Frame
Highest break player 2
Competition points player 1 c ontains / Date (FK)
Competition points player 2 is p art of P Player 1 (FK)
Frame #
Player 1 score
Identical values Player 2 score
Different values
Currently checking Different values
Date Player 1
?
Player 2
?
League Frames 1 Frames 2 HB 1
?
HB 2 Points 1 Points 2
p lays in /
• Player 1 League
p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) is played for / League code
P has
Player 2 (FK) (AK1.2)
League (FK)
Frames won player 1
Frames won player 2
Highest break player 1 Co mp e tition Frame
Highest break player 2
Competition points player 1 c ontains / Date (FK)
Competition points player 2 is p art of P Player 1 (FK)
Frame #
Player 1 score
Identical values Player 2 score
Different values
Currently checking Different values
p lays in /
• Player 1 League
p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) is played for / League code
P has
Player 2 (FK) (AK1.2)
League (FK)
Frames won player 1
Frames won player 2
Highest break player 1 Co mp e tition Frame
Highest break player 2
Competition points player 1 c ontains / Date (FK)
Competition points player 2 is p art of P Player 1 (FK)
Frame #
Player 1 score
Identical values Player 2 score
Different values
Currently checking Different values
p lays in /
• Player 1 League
p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) is played for / League code
P has
Player 2 (FK) (AK1.2)
League (FK)
Frames won player 1
Frames won player 2
Highest break player 1 Co mp e tition Frame
Highest break player 2
Competition points player 1 c ontains / Date (FK)
Competition points player 2 is p art of P Player 1 (FK)
Frame #
Player 1 score
Different values Player 2 score
Identical values
Currently checking Different values
p lays in /
• Player 1 League
p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) is played for / League code
P has
Player 2 (FK) (AK1.2)
League (FK)
Frames won player 1
Frames won player 2
Highest break player 1 Co mp e tition Frame
Highest break player 2
Competition points player 1 c ontains / Date (FK)
Competition points player 2 is p art of P Player 1 (FK)
Frame #
Player 1 score
Different values Player 2 score
Identical values
Currently checking Different values
p lays in /
• Player 1 League
p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) is played for / League code
P has
Player 2 (FK) (AK1.2)
League (FK)
Frames won player 1
Frames won player 2
Highest break player 1 Co mp e tition Frame
Highest break player 2
Competition points player 1 c ontains / Date (FK)
Competition points player 2 is p art of P Player 1 (FK)
Frame #
Player 1 score
Identical values Player 2 score
Different values
Currently checking
Date
?
Player 1 Player 2
?
League Frames 1 Frames 2 HB 1
?
HB 2 Points 1 Points 2
p lays in /
• Player 1 League
p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) is played for / League code
P has
Player 2 (FK) (AK1.2)
League (FK)
Frames won player 1
Frames won player 2
Highest break player 1 Co mp e tition Frame
Highest break player 2
Competition points player 1 c ontains / Date (FK)
Competition points player 2 is p art of P Player 1 (FK)
Frame #
Player 1 score
Identical values Player 2 score
Different values
Currently checking
Date
?
Player 1 Player 2
?
League Frames 1 Frames 2 HB 1
?
HB 2 Points 1 Points 2
2012-10-03 Katie
? Jim C
? ? ? ? 12
? ? ?
2013-02-07 Dave
? Jim B
? ? ? ? 17
? ? ?
Demo: dependencies on subsets of candidate keys Player
Player name
Functional dependencies found:
Date of birth
p lays in /
• Player 1 League
p lays in /
is played by Player
•is played by 2 League
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) is played for / League code
P has
Player 2 (FK) (AK1.2)
League (FK)
Frames won player 1
Frames won player 2
Highest break player 1 Co mp e tition Frame
Highest break player 2
Competition points player 1 c ontains / Date (FK)
Competition points player 2 is p art of P Player 1 (FK)
Frame #
Player 1 score
Identical values Player 2 score
Different values
Currently checking
Date
?
Player 1 Player 2
?
League Frames 1 Frames 2 HB 1
?
HB 2 Points 1 Points 2
p lays in /
• Player 1 League
p lays in /
is played by Player
•is played by 2 League
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) is played for / League code
P has
Player 2 (FK) (AK1.2)
League (FK)
Frames won player 1
Frames won player 2
Highest break player 1 Co mp e tition Frame
Highest break player 2
Competition points player 1 c ontains / Date (FK)
Competition points player 2 is p art of P Player 1 (FK)
Frame #
Player 1 score
Identical values Player 2 score
Different values
Currently checking
Date
?
Player 1 Player 2 League Frames 1 Frames 2 HB 1
?
HB 2 Points 1 Points 2
p lays in /
• Player 1 League
p lays in /
is played by Player
•is played by 2 League
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) is played for / League code
P has
Player 2 (FK) (AK1.2)
League (FK)
Frames won player 1
Frames won player 2
Highest break player 1 Co mp e tition Frame
Highest break player 2
Competition points player 1 c ontains / Date (FK)
Competition points player 2 is p art of P Player 1 (FK)
Frame #
Player 1 score
Different values Player 2 score
Identical values
Currently checking
p lays in /
• Player 1 League
p lays in /
is played by Player
•is played by 2 League
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) is played for / League code
P has
Player 2 (FK) (AK1.2)
League (FK)
Frames won player 1
Frames won player 2
Highest break player 1 Co mp e tition Frame
Highest break player 2
Competition points player 1 c ontains / Date (FK)
Competition points player 2 is p art of P Player 1 (FK)
Frame #
Player 1 score
Different values Player 2 score
Identical values
Currently checking
League code
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Functional dependencies found: Date (AK1.1)
is played for / League code
Player 1 (FK)
• Player 1 League Player 2 (FK) (AK1.2)
P has
Player 1 score
• Player League Player 2 score
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Functional dependencies found: Date (AK1.1)
League code
Player 1 (FK)
• Player 1 League Player 2 (FK) (AK1.2)
• Player 2 League Frames won player 1
Frames won player 2
Highest break player 1
Highest break player 2 Co mp e tition Frame
Competition points player 1 c ontains /
Competition points player 2 is p art of P Date (FK)
Player 1 (FK)
Actual functional dependency: Frame #
Player 1 score
• Player League Player 2 score
3NF example
Attr 1
Attr 2
Examples of violations:
Attr 3 (AK1.1)
Attr 4 (AK1.2)
• Attr5 Attr7
Attr 5 • Attr7 Attr6
Attr 6
Attr 7
Third Normal Form
3NF example
Attr 1
Attr 2
Examples of violations:
Attr 3 (AK1.1)
Attr 4 (AK1.2)
• {Attr5, Attr6} Attr7
Attr 5 • {Attr5, Attr7} Attr6
Attr 6
Attr 7
Third Normal Form
3NF example
Attr 1
Attr 2
Examples of violations:
Attr 3 (AK1.1)
Attr 4 (AK1.2)
• {Attr1, Attr5} Attr7
Attr 5 • {Attr3, Attr5, Attr7} Attr6
Attr 6
Attr 7
Third Normal Form
3NF example
Attr 1
Attr 2
Examples of violations:
Attr 3 (AK1.1)
Attr 4 (AK1.2)
• {Attr1, Attr4} Attr6
Attr 5 • {Attr2, Attr3} Attr6
Attr 6 • {Attr2, Attr3, Attr5, Attr6} Attr7
Attr 7
Third Normal Form
Example
Fix2NF
AB DF {B, C} A
A
B
AC {B, C} D
B (FK) (AK1.1) AD {B, C} F
C (AK1.2) E
D AF
F
Third Normal Form
Example
Fix2NF
AB DF {B, C} A
A
B
AC {B, C} D
B (FK) (AK1.1) AD {B, C} F
C (AK1.2) E
D AF
F
Fix3NF
D
F
Third Normal Form
Testing for:
Dependency of any attribute on a non-key attribute
Can be skipped for tables with no non-key attributes
Dependency of any attribute on a combination of attributes
Can be skipped for tables with:
No composite keys and ≤1 non-key attribute
One composite key and no non-key attributes
Finding functional dependencies, part 4
C (AK1.2)
D
E
Functional dependencies found: DE F (AK2.1)
G
DG
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Player 1 score
Player 2 score
Currently checking
a1 b1 c1 d1 e1 f1 g1 h1 j1
a2 b2 c2 d1 e2 f2 g2 h2 j2
Demo: dependencies on non-key attributes Player
Player name is of /
P p lays in
Date of birth
League code (FK)
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Player 1 score
Player 2 score
Currently checking
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Player 1 score
Player 2 score
Currently checking
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Player 1 score
Player 2 score
Currently checking
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Player 1 score
Player 2 score
Currently checking
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Player 1 score
Player 2 score
Currently checking
Co mp e tition Match
League
Date (AK1.1)
Functional dependency Player 1 (FK) League code
Player 1 score
Player 2 score
Currently checking
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Functional dependency!
Player 2 (FK) (AK1.2)
Frames won player 1
Frames won player 2
Violates 3NF!
Highest break player 1
Highest break player 2 Co mp e tition Frame
Competition points player 1 c ontains /
Competition points player 2 is p art of P Date (FK)
Player 1 (FK)
Frame #
Player 1 score
Player 2 score
Currently checking
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Functional dependency!
Player 2 (FK) (AK1.2)
Frames won player 1 (FK)
Frames won player 2 Co mp e tition Frame
Violates 3NF!
Highest break player 1 c ontains / Date (FK)
Highest break player 2 is p art of P Co mp e tition
Player 1 (FK)Frame
2
Competition points player 1 c ontains / Frame #
Competition points player 2 is p art of P Date (FK)
Player
Player 11 (FK)
score
is s cored in /
Frame
Player #2 score
scores
Player 1 score
Co mp e tition points player 1
Player 2 score
Frames won player 1
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Functional dependency!
Player 2 (FK) (AK1.2)
Frames won player 1 (FK)
Frames won player 2 Co mp e tition Frame
Highest break player 1 c ontains / Date (FK)
Highest break player 2 is p art of P Player 1 (FK)
Competition points player 2 Frame #
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Currently checking
Competition points player 1
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Functional dependency!
Player 2 (FK) (AK1.2)
Frames won player 1 (FK)
Frames won player 2 Co mp e tition Frame
Violates 3NF!
Highest break player 1 c ontains / Date (FK)
Highest break player 2 is p art of P Player 1 (FK)
Competition points player 2 Frame #
Currently checking
Competition points player 1
Co mp e tition Match
League
Date (AK1.1)
League code
Functional dependency! Player 1 (FK)
Currently checking
Competition points player 1
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Co mp e tition points
Frames won
?
Date
?
Player 1
?
Player 2
?
Frames 1 Frames 2
?
HB 1 HB 2
?
2012-10-03 Katie Jim 2 1 40 12
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Co mp e tition points
Frames won
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Co mp e tition points
Frames won
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Co mp e tition points
Frames won
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Co mp e tition points
Frames won
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Co mp e tition points
Frames won
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Co mp e tition points
Frames won
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Co mp e tition points
Frames won
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Co mp e tition points
Frames won
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Co mp e tition points
Frames won
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Co mp e tition points
Frames won
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Co mp e tition points
Frames won
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Co mp e tition points
Frames won
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Co mp e tition points
Frames won
Co mp e tition Match
League
Date (AK1.1)
Actual underlying dependencies: Player 1 (FK) League code
Co mp e tition points
Frames won
Co mp e tition Match
League
Date (AK1.1)
Functional dependency Player 1 (FK) League code
Co mp e tition points
Frames won
Co mp e tition Match
League
Date (AK1.1)
League code
Derivation rule! Player 1 (FK)
Player 2 (FK) (AK1.2)
Frames won player 1 (FK)
Frames won player 2 (FK) Co mp e tition Frame
Highest break player 1 c ontains / Date (FK)
Highest break player 2 is p art of P
Player 1 (FK)
Frame #
Co mp e tition points
Frames won
Co mp e tition Match
League
Date (AK1.1)
League code
Derivation rule! Player 1 (FK)
Player 2 (FK) (AK1.2)
Frames won player 1 (FK)
Frames won player 2 (FK) Co mp e tition Frame
Highest break player 1 c ontains / Date (FK)
Highest break player 2 is p art of P
Player 1 (FK)
Frame #
Co mp e tition points
Frames won
p lays in / p lays in /
is played by is played by
Co mp e tition Match
League
Date (AK1.1)
Player 1 (FK) League code
Co mp e tition points
Frames won
Competition points
Derivation rule:
Frames won player 2
Date Player 1 Player 2 Frames 1 Frames 2 HB 1 HB 2 is computed from
Frames won player 1
and
Player 1 League code
(see specification in design docs)
Summary
Problems
Redundancy
Modification anomalies
Fixed by normalization
Functional dependencies
The key, the
… whole key, and
… nothing but the key; so
… help me Codd
Normal forms
First Normal Form
Second Normal Form
Third Normal Form
Further reading:
Database normalization on Wikipedia:
http://en.wikipedia.org/wiki/Database_normalization
or
http://tinyurl.com/Norm-DB