Database Normalisation
Database Normalisation
com
Database Normalization
PHPQuebec2005
MikeHillyerMySQLAB
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
About Me
Mike Hillyer, BSc
MemberoftheMySQLABdocumentationteam
MySQLCoreandProCertified
TopMySQLexpertatwww.experts-exchange.com
ResidentMySQLexpertatSearchDatabase.com
http://www.openwin.org/mike/aboutme.php
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
About You
How many of you
CurrentlyuseMySQL?
AnotherRDBMS?
Areresponsiblefordatabasedesign?
Willbeinthefuture?
Knowaboutdatabasenormalization?
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
http://www.openwin.org/mike/presentations/
http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html
Introduction
WhatIsDatabaseNormalization?
WhataretheBenefitsofDatabaseNormalization?
WhataretheNormalForms?
FirstNormalForm
SecondNormalForm
FormingRelationships
ThirdNormalForm
JoiningTables
De-Normalization
Conclusion
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
CurestheSpreadSheetSyndrome
Storeonlytheminimalamountofinformation.
Removeredundancies.
Restructuredata.
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
Decreasedstoragerequirements!
convertedto
inatableof
isasavingsof
1VARCHAR(20)
1TINYINTUNSIGNED
1millionrows
~20MB
Fastersearchperformance!
Smallerfilefortablescans.
Moredirectedsearching.
Improveddataintegrity!
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
FirstNormalForm(1NF)
SecondNormalForm(2NF)
ThirdNormalForm(3NF)
Boyce-CoddNormalForm(BCNF)
FourthNormalForm(4NF)
FifthNormalForm(5NF)
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
Our Table
user
name
nickname
phone1
phone2
phone3
cell
pager
address
city
province
postal_code
country
email1
email2
web_url
company
department
picture
notes
email_format
name
phone1
phone2
email1
email2
MikeHillyer
403-555-1717
403-555-1919
mike@hoppen.com
mhillyer@mysite.com
TomJensen
403-555-1919
403-555-1313
tom@openwin.org
tom@supersite.org
RaySmith
403-555-1919
403-555-1111
ray@cpma.com
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
Removehorizontalredundancies
Notwocolumnsholdthesameinformation
Nosinglecolumnholdsmorethanasingleitem
Eachrowmustbeunique
Useaprimarykey
Benefits
Easiertoquery/sortthedata
Morescalable
Eachrowcanbeidentifiedforupdating
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
One Solution
user
first_name
last_name
nickname
phone
cell
pager
address
city
province
postal_code
country
web_url
department
picture
notes
first_name last_name
phone
Mike
Hillyer
403-555-1717
mike@hoppen.com
Mike
Hillyer
403-555-1919
mhillyer@mysite.com
Tom
Jensen
403-555-1919
tom@openwin.org
Tom
Jensen
403-555-1313
tom@supersite.org
Ray
Smith
403-555-1919
ray@cpma.com
Ray
Smith
403-555-1111
Multiplerowsperuser
Emailsareassociatedwithonlyoneotherphone
HardtoSearch
10
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
Satisfying 1NF
user
PK
user_id
first_name
last_name
nickname
address
city
province
postal_code
country
web_url
company
department
picture
notes
email
PK
email_id
address
phone
PK
phone_id
country_code
number
extension
11
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
Forming Relationships
ThreeForms
Oneto(zeroor)One
Oneto(zeroor)Many
ManytoMany
OnetoOne
SameTable?
OnetoMany
PlacePKoftheOneintheMany
ManytoMany
Createajoiningtable
12
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
Joining Tables
user
PK
user_id
first_name
last_name
nickname
address
city
province
postal_code
country
web_url
picture
notes
email_format
phone
user_phone
PK,FK1 phone_id
PK
user_id
type
email
PK
address
FK1
user_id
PK
phone_id
country_code
number
extension
13
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
company
department
Mike
Hillyer
MySQL
Documentation
Tom
Jensen
CPNS
Finance
Ray
Smith
CPNS
Documentation
user
PK
user_id
first_name
last_name
nickname
address
city
province
postal_code
country
web_url
picture
notes
email_format
phone
user_phone
PK,FK1 phone_id
PK
user_id
type
email
PK
address
FK1
user_id
PK
phone_id
country_code
number
extension
14
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
TablemustbeinFirstNormalForm
Removeverticalredundancy
Thesamevalueshouldnotrepeatacrossrows
Compositekeys
AllcolumnsinarowmustrefertoBOTHpartsofthekey
Benefits
Increasedstorageefficiency
Lessdatarepetition
15
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
Satisfying 2NF
email
email
PK address
PK address
type
FK1 user_id
FK1 user_id
user
user
PK user_id
PK user_id
first_name
last_name
first_name
nickname
last_name
address
nickname
city
address
province
city
postal_code
province
country
postal_code
web_url
country
picture
web_url
notes
picture
email_format
notes
phone
user_phone
PK,FK1 user_id
PK,FK2 phone_id
PK
phone_id
country_code
number
extension
type
user_company
PK,FK1 user_id
PK,FK2 company_id
department
company
PK
company_id
name
16
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
TablemustbeinSecondNormalForm
Ifyourtableis2NF,thereisagoodchanceitis3NF
Allcolumnsmustrelatedirectlytotheprimarykey
Benefits
Noextraneousdata
17
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
Satisfying 3NF
user_phone
user
PK
email
PK
address
FK1
user_id
format
user_id
first_name
last_name
nickname
address
city
province
postal_code
country
web_url
picture
notes
PK,FK1 user_id
PK,FK2 phone_id
phone
PK
phone_id
country_code
number
type
extension
user_company
PK,FK1 user_id
PK,FK2 company_id
department
company
PK
company_id
name
18
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
Finding Balance
user
user_phone
PK
user_id
FK1
first_name
last_name
nickname
unit
street_number
street_name
street_type
quadrant
web_url
picture
notes
postal_code
PK,FK1 user_id
PK,FK2 phone_id
extension
postal_code
PK
postal_code
FK1
city_id
PK
address
FK1
user_id
format
phone
PK
phone_id
FK1
type_id
area_code
NXX
NCX
country_id
FK2
PK
city_id
FK1
name
province_id
PK
type_id
PK,FK1 user_id
PK,FK2 department_id
province_id
FK1
Name
Abbreviation
country_id
country_id
Name
phone_code
department
PK
department_id
FK1
name
company_id
province
PK
PK
type
user_department
city
country
type
company
PK
company_id
name
19
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
Joining Tables
TwoBasicJoins
Equi-Join
OuterJoin(LEFTJOIN)
Equi-Join
SELECTuser.first_name,user.last_name,email.address
FROMuser,email
WHEREuser.user_id=email.user_id
LEFTJOIN
SELECTuser.first_name,user.last_name,email.address
FROMuserLEFTJOINemail
ONuser.user_id=email.user_id
20
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
De-Normalizing Tables
Usewithcaution
Normalizefirst,thende-normalize
Useonlywhenyoucannotoptimize
Trytemptables,UNIONs,VIEWs,subselectsfirst
21
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
Conclusion
http://dev.mysql.com/tech-resources/articles/intro-tonormalization.html
MySQLDatabaseDesignandOptimization
JonStephens&ChadRussell
Chapter3
ISBN1-59059-332-4
http://www.openwin.org/mike/books
http://www.openwin.org/mike/presentations
22
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
QUESTIONS?
Feelfreetoasknoworfindmeafterthissession!
23
200502-02|DatabaseNormalization|MySQLAB2005|www.mysql.com
Book Draw!
Stick around and win a book!
24