200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.
com
Database Normalization
PHP Quebec 2005 Mike Hillyer MySQL AB
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
About Me
Mike Hillyer, BSc 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
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
About You
How many of you
Currently use MySQL? Another RDBMS? Are responsible for database design? Will be in the future? Know about database normalization?
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
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
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
What Is Database Normalization?
Cures the SpreadSheet Syndrome Store only the minimal amount of information. Remove redundancies. Restructure data.
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
What are the Benefits of Database Normalization?
Decreased storage requirements!
converted to in a table of is a savings of 1 VARCHAR(20) 1 TINYINT UNSIGNED 1 million rows ~20 MB
Faster search performance!
Smaller file for table scans. More directed searching.
Improved data integrity!
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
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)
200502-02 | Database Normalization | MySQL AB 2005 | 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
Mike Hillyer
phone1
403-555-1717
phone2
403-555-1919
email1
[email protected]
email2
[email protected]
Tom Jensen
403-555-1919
403-555-1313
[email protected]
[email protected]
Ray Smith
403-555-1919
403-555-1111
[email protected]
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
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
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
10
One Solution
user
first_name last_name
first_name last_name nickname phone cell pager address city province postal_code country web_url department picture notes
phone
403-555-1717 403-555-1919 403-555-1919
email
[email protected] [email protected] [email protected]
Mike Mike Tom
Hillyer Hillyer Jensen
Tom
Ray Ray
Jensen
Smith Smith
403-555-1313
403-555-1919 403-555-1111
[email protected]
[email protected]
Multiple rows per user Emails are associated with only one other phone Hard to Search
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
11
Satisfying 1NF
user PK user_id first_name last_name nickname address city province postal_code country web_url company department picture notes
phone
email
PK
PK email_id address
phone_id country_code number extension
200502-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
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
13
Joining Tables
user PK user_id first_name last_name nickname address city province postal_code country web_url picture notes email_format
user_phone PK,FK1 phone_id PK user_id type PK
phone phone_id country_code number extension
email PK FK1 address user_id
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
14
Our User Table
first_name last_name
Mike Tom Ray Hillyer Jensen Smith
company
MySQL CPNS CPNS
department
Documentation Finance Documentation
user PK user_id first_name last_name nickname address city province postal_code country web_url picture notes email_format
user_phone PK,FK1 phone_id PK user_id type PK
phone phone_id country_code number extension
email PK FK1 address user_id
200502-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
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
16
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 first_name last_name last_name nickname address nickname city address province city postal_code province country postal_code web_url country picture web_url notes picture email_format notes
user_phone PK,FK1 user_id PK,FK2 phone_id PK
phone phone_id country_code number extension type
user_company PK,FK1 user_id PK,FK2 company_id department PK
company company_id name
200502-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
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
18
Satisfying 3NF
user_phone user PK 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 extension PK phone phone_id country_code number type
email PK FK1 address user_id format
user_company PK,FK1 user_id PK,FK2 company_id department company PK company_id name
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
19
Finding Balance
user PK user_id first_name last_name nickname unit street_number street_name street_type quadrant web_url picture notes postal_code user_phone PK,FK1 user_id PK,FK2 phone_id extension PK FK1 phone phone_id type_id area_code NXX NCX country_id PK type type_id type PK country country_id Name phone_code
email PK FK1 address user_id format
FK2
user_department PK,FK1 user_id PK,FK2 department_id PK
department department_id name company_id
FK1
FK1
postal_code PK FK1 postal_code city_id FK1 PK
city city_id name province_id FK1 PK
province province_id Name Abbreviation country_id PK
company company_id name
200502-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, email WHERE 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
200502-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
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
22
Conclusion
http://dev.mysql.com/tech-resources/articles/intro-tonormalization.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
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
23
QUESTIONS?
Feel free to ask now or find me after this session!
200502-02 | Database Normalization | MySQL AB 2005 | www.mysql.com
24
Book Draw!
Stick around and win a book!