database normalisation
TRANSCRIPT
![Page 1: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/1.jpg)
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 1
Database Normalization
PHPQuebec2005
MikeHillyer–MySQLAB
![Page 2: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/2.jpg)
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
![Page 3: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/3.jpg)
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 3
About You
• CurrentlyuseMySQL?• AnotherRDBMS?• Areresponsiblefordatabasedesign?• Willbeinthefuture?• Knowaboutdatabasenormalization?
How many of you…
![Page 4: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/4.jpg)
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
![Page 5: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/5.jpg)
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 5
What Is Database Normalization?
• Curesthe‘SpreadSheetSyndrome’• Storeonlytheminimalamountofinformation.• Removeredundancies.• Restructuredata.
![Page 6: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/6.jpg)
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 6
What are the Benefitsof Database Normalization?
• Decreasedstoragerequirements!1VARCHAR(20)
convertedto 1TINYINTUNSIGNEDinatableof 1millionrowsisasavingsof ~20MB• Fastersearchperformance!
– Smallerfilefortablescans.– Moredirectedsearching.
• Improveddataintegrity!
![Page 7: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/7.jpg)
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)
![Page 8: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/8.jpg)
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 8
Our Table
name phone1 phone2 email1 email2MikeHillyer 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
![Page 9: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/9.jpg)
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 9
First Normal Form
• Removehorizontalredundancies– Notwocolumnsholdthesameinformation– Nosinglecolumnholdsmorethanasingleitem
• Eachrowmustbeunique– Useaprimarykey
• Benefits– Easiertoquery/sortthedata– Morescalable– Eachrowcanbeidentifiedforupdating
![Page 10: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/10.jpg)
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 10
One Solution
first_name last_name phone emailMike 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
![Page 11: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/11.jpg)
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
![Page 12: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/12.jpg)
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 12
Forming Relationships
• ThreeForms– Oneto(zeroor)One– Oneto(zeroor)Many– ManytoMany
• OnetoOne– SameTable?
• OnetoMany– PlacePKoftheOneintheMany
• ManytoMany– Createajoiningtable
![Page 13: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/13.jpg)
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
![Page 14: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/14.jpg)
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 Tablefirst_name last_name company departmentMike Hillyer MySQL Documentation
Tom Jensen CPNS Finance
Ray Smith CPNS Documentation
![Page 15: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/15.jpg)
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 15
Second Normal Form
• TablemustbeinFirstNormalForm• Removeverticalredundancy
– Thesamevalueshouldnotrepeatacrossrows• Compositekeys
– AllcolumnsinarowmustrefertoBOTHpartsofthekey• Benefits
– Increasedstorageefficiency– Lessdatarepetition
![Page 16: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/16.jpg)
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
![Page 17: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/17.jpg)
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 17
Third Normal Form
• TablemustbeinSecondNormalForm– Ifyourtableis2NF,thereisagoodchanceitis3NF
• Allcolumnsmustrelatedirectlytotheprimarykey• Benefits
– Noextraneousdata
![Page 18: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/18.jpg)
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
![Page 19: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/19.jpg)
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
![Page 20: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/20.jpg)
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
![Page 21: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/21.jpg)
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 21
De-Normalizing Tables
• Usewithcaution• Normalizefirst,thende-normalize• Useonlywhenyoucannotoptimize• Trytemptables,UNIONs,VIEWs,subselectsfirst
![Page 22: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/22.jpg)
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
![Page 23: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/23.jpg)
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 23
QUESTIONS?
Feelfreetoasknoworfindmeafterthissession!
![Page 24: Database normalisation](https://reader035.vdocument.in/reader035/viewer/2022062412/58f32ae81a28ab223d8b45a5/html5/thumbnails/24.jpg)
200502-02|DatabaseNormalization|©MySQLAB2005|www.mysql.com 24
Book Draw!
Stick around and win a book!