200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 1
Database Normalization
PHPQuebec2005
MikeHillyer–MySQLAB
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 2
About Me
• MemberoftheMySQLABdocumentationteam
• MySQLCoreandProCertified
• TopMySQLexpertatwww.experts-exchange.com
• ResidentMySQLexpertatSearchDatabase.com
• http://www.openwin.org/mike/aboutme.php
Mike Hillyer, BSc
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 3
About You
• CurrentlyuseMySQL?
• AnotherRDBMS?
• Areresponsiblefordatabasedesign?
• Willbeinthefuture?
• Knowaboutdatabasenormalization?
How many of you…
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 4
About This Session
• 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 5
What Is Database Normalization?
• Curesthe‘SpreadSheetSyndrome’
• Storeonlytheminimalamountofinformation.
• Removeredundancies.
• Restructuredata.
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 6
What are the Benefitsof Database Normalization?
• Decreasedstoragerequirements!
1VARCHAR(20)
convertedto 1TINYINTUNSIGNED
inatableof 1millionrows
isasavingsof ~20MB
• Fastersearchperformance!– Smallerfilefortablescans.– Moredirectedsearching.
• Improveddataintegrity!
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 7
What are the Normal Forms?
• FirstNormalForm(1NF)
• SecondNormalForm(2NF)
• ThirdNormalForm(3NF)
• Boyce-CoddNormalForm(BCNF)
• FourthNormalForm(4NF)
• FifthNormalForm(5NF)
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 8
Our Table
name phone1 phone2 email1 email2
MikeHillyer 403-555-1717 403-555-1919 [email protected] [email protected]
TomJensen 403-555-1919 403-555-1313 [email protected] [email protected]
RaySmith 403-555-1919 403-555-1111 [email protected]
user
namenicknamephone1phone2phone3cellpageraddresscityprovincepostal_codecountryemail1email2web_urlcompanydepartmentpicturenotesemail_format
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 9
First Normal Form
• Removehorizontalredundancies– Notwocolumnsholdthesameinformation– Nosinglecolumnholdsmorethanasingleitem
• Eachrowmustbeunique– Useaprimarykey
• Benefits– Easiertoquery/sortthedata– Morescalable– Eachrowcanbeidentifiedforupdating
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 10
One Solution
first_name last_name phone email
Mike Hillyer 403-555-1717 [email protected]
Mike Hillyer 403-555-1919 [email protected]
Tom Jensen 403-555-1919 [email protected]
Tom Jensen 403-555-1313 [email protected]
Ray Smith 403-555-1919 [email protected]
Ray Smith 403-555-1111
• Multiplerowsperuser
• Emailsareassociatedwithonlyoneotherphone
• HardtoSearch
user
first_namelast_namenicknamephonecellpageraddresscityprovincepostal_codecountryweb_urldepartmentpicturenotes
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 11
Satisfying 1NF
user
PK user_id
first_namelast_namenicknameaddresscityprovincepostal_codecountryweb_urlcompanydepartmentpicturenotes
phone
PK phone_id
country_codenumberextension
PK email_id
address
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 12
Forming Relationships
• ThreeForms– Oneto(zeroor)One– Oneto(zeroor)Many– ManytoMany
• OnetoOne– SameTable?
• OnetoMany– PlacePKoftheOneintheMany
• ManytoMany– Createajoiningtable
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 13
Joining Tables
user
PK user_id
first_namelast_namenicknameaddresscityprovincepostal_codecountryweb_urlpicturenotesemail_format
PK address
FK1 user_id
user_phone
PK,FK1 phone_idPK user_id
type
phone
PK phone_id
country_codenumberextension
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 14
user
PK user_id
first_namelast_namenicknameaddresscityprovincepostal_codecountryweb_urlpicturenotesemail_format
PK address
FK1 user_id
user_phone
PK,FK1 phone_idPK user_id
type
phone
PK phone_id
country_codenumberextension
Our User Table
first_name last_name company department
Mike Hillyer MySQL Documentation
Tom Jensen CPNS Finance
Ray Smith CPNS Documentation
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 15
Second Normal Form
• TablemustbeinFirstNormalForm
• Removeverticalredundancy– Thesamevalueshouldnotrepeatacrossrows
• Compositekeys– AllcolumnsinarowmustrefertoBOTHpartsofthekey
• Benefits– Increasedstorageefficiency– Lessdatarepetition
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 16
Satisfying 2NF
PK address
typeFK1 user_id
phone
PK phone_id
country_codenumberextensiontype
user
PK user_id
first_namelast_namenicknameaddresscityprovincepostal_codecountryweb_urlpicturenotes
user_phone
PK,FK1 user_idPK,FK2 phone_id
company
PK company_id
name
user_company
PK,FK1 user_idPK,FK2 company_id
department
PK address
FK1 user_id
user
PK user_id
first_namelast_namenicknameaddresscityprovincepostal_codecountryweb_urlpicturenotesemail_format
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 17
Third Normal Form
• TablemustbeinSecondNormalForm– Ifyourtableis2NF,thereisagoodchanceitis3NF
• Allcolumnsmustrelatedirectlytotheprimarykey
• Benefits– Noextraneousdata
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 18
Satisfying 3NF
PK address
FK1 user_idformat
phone
PK phone_id
country_codenumbertype
user
PK user_id
first_namelast_namenicknameaddresscityprovincepostal_codecountryweb_urlpicturenotes
user_phone
PK,FK1 user_idPK,FK2 phone_id
extension
company
PK company_id
name
user_company
PK,FK1 user_idPK,FK2 company_id
department
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 19
Finding Balance
PK address
FK1 user_idformat
phone
PK phone_id
FK1 type_idarea_codeNXXNCX
FK2 country_id
user
PK user_id
first_namelast_namenicknameunitstreet_numberstreet_namestreet_typequadrantweb_urlpicturenotes
FK1 postal_code
user_phone
PK,FK1 user_idPK,FK2 phone_id
extension
company
PK company_id
name
user_department
PK,FK1 user_idPK,FK2 department_id
type
PK type_id
type
country
PK country_id
Namephone_code
department
PK department_id
nameFK1 company_id
postal_code
PK postal_code
FK1 city_id
province
PK province_id
NameAbbreviation
FK1 country_id
city
PK city_id
nameFK1 province_id
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 20
Joining Tables
• TwoBasicJoins– Equi-Join– OuterJoin(LEFTJOIN)
• Equi-Join– SELECTuser.first_name,user.last_name,email.address
FROMuser,emailWHEREuser.user_id=email.user_id
• LEFTJOIN– SELECTuser.first_name,user.last_name,email.address
FROMuserLEFTJOINemailONuser.user_id=email.user_id
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 21
De-Normalizing Tables
• Usewithcaution
• Normalizefirst,thende-normalize
• Useonlywhenyoucannotoptimize
• Trytemptables,UNIONs,VIEWs,subselectsfirst
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 22
Conclusion
• http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html
• MySQLDatabaseDesignandOptimization– JonStephens&ChadRussell– Chapter3– ISBN1-59059-332-4– http://www.openwin.org/mike/books
• http://www.openwin.org/mike/presentations
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 23
QUESTIONS?
Feelfreetoasknoworfindmeafterthissession!
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 24
Book Draw!
Stick around and win a book!
Top Related