You are on page 1of 24

2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.

com
1
Database Normalization
!! Quebec 2005
Mike illyer MySQL AB
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
2
bout Me
Member of the MySQL AB documentation team
MySQL Core and !ro Certified
Top MySQL expert at www.experts-exchange.com
Resident MySQL expert at SearchDatabase.com
http://www.openwin.org/mike/aboutme.php
Mike Hillyer, B8c
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
3
bout You
Currently use MySQL?
Another RDBMS?
Are responsible for database design?
Will be in the future?
Know about database normalization?
How many of you.
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
1
bout This 8ession
http://www.openwin.org/mike/presentations/
http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html
ntroduction
What s 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
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
5
hat s Database Normalization?
Cures the 'SpreadSheet Syndrome'
Store only the minimal amount of information.
Remove redundancies.
Restructure data.
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com

hat are the Benefits
of Database Normalization?
Decreased storage requirements!
1 VARCAR(20)
converted to 1 TNYNT UNSGNED
in a table of 1 million rows
is a savings of ~20 MB
Faster search performance!
Smaller file for table scans.
More directed searching.
mproved data integrity!
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
Z
hat 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)
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
8
ur Table
name phone1 phone2 email1 email2
Mike illyer 403-555-1717 403-555-1919 mike@hoppen.com mhillyer@mysite.com
Tom Jensen 403-555-1919 403-555-1313 tom@openwin.org tom@supersite.org
Ray Smith 403-555-1919 403-555-1111 ray@cpma.com
user
name
nickname
phone1
phone2
phone3
cell
pager
address
city
province
postal_code
country
email1
email2
web_url
company
department
picture
notes
email_format
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
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
10
ne 8olution
first_name last_name phone email
Mike illyer 403-555-1717 mike@hoppen.com
Mike illyer 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
Multiple rows per user
Emails are associated with only one other phone
ard to Search
user
first_name
last_name
nickname
phone
cell
pager
address
city
province
postal_code
country
web_url
department
picture
notes
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
11
8atisfying 1NF
user
PK user_id
first_name
last_name
nickname
address
city
province
postal_code
country
web_url
company
department
picture
notes
phone
PK phone_id
country_code
number
extension
email
PK emaiI_id
address
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
!lace !K of the One in the Many
Many to Many
Create a joining table
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
13
oining Tables
user
PK user_id
first_name
last_name
nickname
address
city
province
postal_code
country
web_url
picture
notes
email_format
email
PK address
FK1 user_id
user_phone
PK,FK1 phone_id
PK user_id
type
phone
PK phone_id
country_code
number
extension
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
11
user
PK user_id
first_name
last_name
nickname
address
city
province
postal_code
country
web_url
picture
notes
email_format
email
PK address
FK1 user_id
user_phone
PK,FK1 phone_id
PK user_id
type
phone
PK phone_id
country_code
number
extension
ur User Table
first_name last_name company department
Mike illyer MySQL Documentation
Tom Jensen C!NS Finance
Ray Smith C!NS Documentation
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
15
8econd 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 BOT parts of the key
Benefits
ncreased storage efficiency
Less data repetition
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
1
8atisfying 2NF
email
PK address
type
FK1 user_id
phone
PK phone_id
country_code
number
extension
type
user
PK user_id
first_name
last_name
nickname
address
city
province
postal_code
country
web_url
picture
notes
user_phone
PK,FK1 user_id
PK,FK2 phone_id
company
PK company_id
name
user_company
PK,FK1 user_id
PK,FK2 company_id
department
email
PK address
FK1 user_id
user
PK user_id
first_name
last_name
nickname
address
city
province
postal_code
country
web_url
picture
notes
email_format
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
1Z
Third Normal Form
Table must be in Second Normal Form
f 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
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
18
8atisfying 3NF
email
PK address
FK1 user_id
format
phone
PK phone_id
country_code
number
type
user
PK user_id
first_name
last_name
nickname
address
city
province
postal_code
country
web_url
picture
notes
user_phone
PK,FK1 user_id
PK,FK2 phone_id
extension
company
PK company_id
name
user_company
PK,FK1 user_id
PK,FK2 company_id
department
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
19
Finding Balance
email
PK address
FK1 user_id
format
phone
PK phone_id
FK1 type_id
area_code
NXX
NCX
FK2 country_id
user
PK user_id
first_name
last_name
nickname
unit
street_number
street_name
street_type
quadrant
web_url
picture
notes
FK1 postal_code
user_phone
PK,FK1 user_id
PK,FK2 phone_id
extension
company
PK company_id
name
user_department
PK,FK1 user_id
PK,FK2 department_id
type
PK type_id
type
country
PK country_id
Name
phone_code
department
PK department_id
name
FK1 company_id
postal_code
PK postaI_code
FK1 city_id
province
PK province_id
Name
Abbreviation
FK1 country_id
city
PK city_id
name
FK1 province_id
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
20
oining Tables
Two Basic Joins
Equi-Join
Outer Join (LEFT JON)
Equi-Join
SELECT user.first_name, user.last_name, email.address
FROM user, email
WERE user.user_id = email.user_id
LEFT JON
SELECT user.first_name, user.last_name, email.address
FROM user LEFT JON email
ON user.user_id = email.user_id
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, UNONs, VEWs, subselects first
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
22
onclusion
http://dev.mysql.com/tech-resources/articles/intro-to-
normalization.html
MySQL Database Design and Optimization
Jon Stephens & Chad Russell
Chapter 3
SBN 1-59059-332-4
http://www.openwin.org/mike/books
http://www.openwin.org/mike/presentations
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
23
"UE8TN8?
Feel free to ask now or find me after this session!
2005-02-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
21
Book Drawl
SrIek around and wIn a book!