Top Banner
200502-02 | Database Normalization | © MySQL AB 2005 | www.mysql.com 1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB
24

200502-02 | Database Normalization | © MySQL AB 2005 | 1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

Dec 31, 2015

Download

Documents

Melvyn Bradley
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 1

Database Normalization

PHP­Quebec­2005

Mike­Hillyer­–­MySQL­AB

Page 2: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 2

About Me

• Member­of­the­MySQL­AB­documentation­team

• MySQL­Core­and­Pro­Certified

• Top­MySQL­expert­at­www.experts-exchange.com

• Resident­MySQL­expert­at­SearchDatabase.com

• http://www.openwin.org/mike/aboutme.php

Mike Hillyer, BSc

Page 3: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 3

About You

• Currently­use­MySQL?

• Another­RDBMS?

• Are­responsible­for­database­design?

• Will­be­in­the­future?

• Know­about­database­normalization?

How many of you…

Page 4: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­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• What­Is­Database­Normalization?• What­are­the­Benefits­of­Database­Normalization?• What­are­the­Normal­Forms?• First­Normal­Form• Second­Normal­Form• Forming­Relationships• Third­Normal­Form• Joining­Tables• De-Normalization• Conclusion

Page 5: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 5

What Is Database Normalization?

• Cures­the­‘SpreadSheet­Syndrome’

• Store­only­the­minimal­amount­of­information.

• Remove­redundancies.

• Restructure­data.

Page 6: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 6

What are the Benefitsof Database Normalization?

• Decreased­storage­requirements!

1­VARCHAR(20)

­­­­­converted­to­­­­­­­­­­ 1­TINYINT­UNSIGNED

­­­­­in­a­table­of 1­million­rows

­­­­­is­a­savings­of ~20­MB

• Faster­search­performance!– Smaller­file­for­table­scans.– More­directed­searching.

• Improved­data­integrity!

Page 7: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 7

What are the Normal Forms?

• First­Normal­Form­(1NF)

• Second­Normal­Form­(2NF)

• Third­Normal­Form­(3NF)

• Boyce-Codd­Normal­Form­(BCNF)

• Fourth­Normal­Form­(4NF)

• Fifth­Normal­Form­(5NF)

Page 8: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 8

Our Table

name phone1 phone2 email1 email2

Mike­Hillyer 403-555-1717 403-555-1919 [email protected] [email protected]

Tom­Jensen 403-555-1919 403-555-1313 [email protected] [email protected]

Ray­Smith 403-555-1919 403-555-1111 [email protected]

user

namenicknamephone1phone2phone3cellpageraddresscityprovincepostal_codecountryemail1email2web_urlcompanydepartmentpicturenotesemail_format

Page 9: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 9

First Normal Form

• Remove­horizontal­redundancies– No­two­columns­hold­the­same­information– No­single­column­holds­more­than­a­single­item

• Each­row­must­be­unique– Use­a­primary­key

• Benefits– Easier­to­query/sort­the­data– More­scalable– Each­row­can­be­identified­for­updating

Page 10: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­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

• Multiple­rows­per­user

• Emails­are­associated­with­only­one­other­phone

• Hard­to­Search

user

first_namelast_namenicknamephonecellpageraddresscityprovincepostal_codecountryweb_urldepartmentpicturenotes

Page 11: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 11

Satisfying 1NF

user

PK user_id

first_namelast_namenicknameaddresscityprovincepostal_codecountryweb_urlcompanydepartmentpicturenotes

phone

PK phone_id

country_codenumberextension

email

PK email_id

address

Page 12: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 12

Forming Relationships

• Three­Forms– One­to­(zero­or)­One– One­to­(zero­or)­Many– Many­to­Many

• One­to­One– Same­Table?

• One­to­Many– Place­PK­of­the­One­in­the­Many

• Many­to­Many– Create­a­joining­table

Page 13: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 13

Joining Tables

user

PK user_id

first_namelast_namenicknameaddresscityprovincepostal_codecountryweb_urlpicturenotesemail_format

email

PK address

FK1 user_id

user_phone

PK,FK1 phone_idPK user_id

type

phone

PK phone_id

country_codenumberextension

Page 14: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 14

user

PK user_id

first_namelast_namenicknameaddresscityprovincepostal_codecountryweb_urlpicturenotesemail_format

email

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

Page 15: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 15

Second Normal Form

• Table­must­be­in­First­Normal­Form

• Remove­vertical­redundancy– The­same­value­should­not­repeat­across­rows

• Composite­keys– All­columns­in­a­row­must­refer­to­BOTH­parts­of­the­key

• Benefits– Increased­storage­efficiency– Less­data­repetition

Page 16: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 16

Satisfying 2NF

email

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

email

PK address

FK1 user_id

user

PK user_id

first_namelast_namenicknameaddresscityprovincepostal_codecountryweb_urlpicturenotesemail_format

Page 17: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 17

Third Normal Form

• Table­must­be­in­Second­Normal­Form– If­your­table­is­2NF,­there­is­a­good­chance­it­is­3NF

• All­columns­must­relate­directly­to­the­primary­key

• Benefits– No­extraneous­data

Page 18: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 18

Satisfying 3NF

email

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: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 19

Finding Balance

email

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: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 20

Joining Tables

• Two­Basic­Joins– Equi-Join– Outer­Join­(LEFT­JOIN)

• Equi-Join– SELECT­user.first_name,­user.last_name,­email.address

FROM­user,­emailWHERE­user.user_id­=­email.user_id

• LEFT­JOIN– SELECT­user.first_name,­user.last_name,­email.address

FROM­user­LEFT­JOIN­email­­­­­­­ON­user.user_id­=­email.user_id

Page 21: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 21

De-Normalizing Tables

• Use­with­caution

• Normalize­first,­then­de-normalize

• Use­only­when­you­cannot­optimize

• Try­temp­tables,­UNIONs,­VIEWs,­subselects­first

Page 22: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 22

Conclusion

• http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html

• MySQL­Database­Design­and­Optimization– Jon­Stephens­&­Chad­Russell– Chapter­3– ISBN­1-59059-332-4– http://www.openwin.org/mike/books

• http://www.openwin.org/mike/presentations

Page 23: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 23

QUESTIONS?

Feel­free­to­ask­now­or­find­me­after­this­session!

Page 24: 200502-02 | Database Normalization | © MySQL AB 2005 |  1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.

2005­02-02­|­Database­Normalization­|­©­MySQL­AB­2005­|­www.mysql.com 24

Book Draw!

Stick around and win a book!