View
218
Download
1
Embed Size (px)
Citation preview
Kirkwood Center for Kirkwood Center for Continuing EducationContinuing Education
By Fred McClurg, [email protected]
Introduction toIntroduction toPHP and MySQLPHP and MySQL
Copyright © 2010 All Rights Reserved.
Chapter NineChapter NineNormalizationNormalization
http://webcert.kirkwood.edu/http://webcert.kirkwood.edu/~fmcclurg/courses/php/slides/~fmcclurg/courses/php/slides/chapter09d.normalization.pptchapter09d.normalization.ppt
Normalization
Defined: The breaking apart of data into logical relationships to reduce duplication of data.
Why Normalize?1. Reduce duplication2. Conserve storage space3. Reduce maintenance
Normalization (cont.)Avoid Extremes:
1. Each data column in a separate tableA. Queries would be very largeB. Additional processing time required
2. All columns in one tableA. Data is repeated between rowsB. Requires additional storageC. Updates on multiple rows required
Forms of Normalization (1NF)First Normal Form (1NF or FNF)
Purpose: Reducing redundant data across a horizontal row
Rules:1. No multiple columns containing the same
data
2. Column can contain only one value
3. The primary key must uniquely define the row
22
11
First Normal Form Challenge
After Normalization:Rule One:Move multiple authors into a single tableRule Two:Break name into two separate columnsBreak address into four separate columnsRule Three:Add an unique id for a primary key
NameName AddressAddress PhonePhone TitleTitle Author1Author1 Author2Author2 OrderedOrdered
Fred McClurg
1640 Fawn Drive, North Liberty, IA 52317
319-123-4567 The Case for a Creator
Lee Strobel
Garry Poole
Jan 9, 2009
Fred McClurg
1640 Fawn Drive, North Liberty, IA 52317
319-123-4567 More than a Carpenter
Josh McDowell
Jan 9, 2009
Before Normalization:
NameName AddressAddress PhonePhone TitleTitle Author1Author1 Author2Author2 OrderedOrdered
Fred McClurg
1640 Fawn Drive, North Liberty, IA 52317
319-123-4567 The Case for a Creator
Lee Strobel Garry Poole
Jan 9, 2009
Fred McClurg
1640 Fawn Drive, North Liberty, IA 52317
319-123-4567 More than a Carpenter
Josh McDowell
Jan 9, 2009
First Normal Form ResultsAfter Normalization Example:
IDID FirstFirst LastLast AddressAddress CityCity StateState ZipZip PhonePhone TitleTitle OrderedOrdered
1 Fred McClurg 1640 Fawn Drive
North Liberty
IA 52317 319-123-4567 The Case for a Creator
Jan 9, 2009
2 Fred McClurg 1640 Fawn Drive
North Liberty
IA 52317 319-123-4567 More than a Carpenter
Jan 11, 2009
IDID TitleTitle AuthorAuthor
1 The Case for a Creator Lee Strobel
2 The Case for a Creator Garry Poole
3 More than a Carpenter Josh McDowell
Forms of Normalization (2NF)Second Normal Form (2NF)
Purpose: Reducing redundant data in vertical columns
Rules:1. Tables must be in First Normal Form
2. Place columns that repeat values across multiple rows into a separate table
3. Place columns that aren’t dependent on the primary key into a separate table
ID First Last Address City State Zip Phone Title Ordered
1 Fred McClurg 1640 Fawn Drive
North Liberty
IA 52317 319-123-4567 The Case for a Creator
Jan 9, 2009
2 Fred McClurg 1640 Fawn Drive
North Liberty
IA 52317 319-123-4567 More than a Carpenter
Jan 11, 2009
ID Title Author
1 The Case for a Creator Lee Strobel
2 The Case for a Creator Garry Poole
3 More than a Carpenter Josh McDowell
Second Normal Form ChallengeBefore Normalization:
After Normalization:Rule Two:Move book into separate tableRule Three:Move author into separate tableMove orders into separate table
OrderOrder
User_IDUser_ID Book_IDBook_ID PlacedPlaced
1 1 Jan 9, 2009
1 2 Jan 11, 2009
Second Normal Form Results
IDID FirstFirst LastLast AddressAddress CityCity StateState ZipZip PhonePhone TitleTitle OrderedOrdered
1 Fred McClurg 1640 Fawn Drive
North Liberty
IA 52317 319-123-4567 The Case for a Creator
Jan 9, 2009
2 Fred McClurg 1640 Fawn Drive
North Liberty
IA 52317 319-123-4567 More than a Carpenter
Jan 11, 2009
IDID TitleTitle AuthorAuthor
1 The Case for a Creator Lee Strobel
2 The Case for a Creator Garry Poole
3 More than a Carpenter Josh McDowell
BookBook
ID Title
1 The Case for a Creator
2 More than a Carpenter
AuthorAuthor
IDID NameName
1 Lee Strobel
2 Garry Poole
3 Josh McDowell
Book_to_AuthorBook_to_Author
Book_IDBook_ID Author_IDAuthor_ID
1 1
1 2
2 3
IDID FirstFirst LastLast AddressAddress CityCity StateState ZipZip PhonePhone
1 Fred McClurg 1640 Fawn Drive
North Liberty
IA 52317 319-123-4567
What is the relationship between address, city, state, and zip?
Address, City, State, Zip Relationship
addressaddress
idid streetstreet city_idcity_id
1 Kansas State University 1
2 Empire State Building 2
3 Grand Central Station 3
statestate
id namename zip_idzip_id
1 KS 1
2 NY 2
3 NY 3
citycity
id namename state_idstate_id
1 Manhattan 1
2 Manhattan 2
3 Manhattan 3
zipzip
id codecode
1 66506
2 10018
3 10017
addressaddress citycity statestate zipzip
What is the relationship between address, city, state, and zip?
Address, City, State, Zip Relationship
address
id street state_id
1 Kansas State University 1
2 Empire State Building 2
3 Grand Central Station 3
state
id name city_id
1 KS 1
2 NY 2
3 NY 3
city
id name zip_id
1 Manhattan 1
2 Manhattan 2
3 Manhattan 3
zip
id code
1 66506
2 10018
3 10017
addressaddress statestate citycity zipzip
zipzip
idid codecode state_idstate_id
1 66506 1
2 10018 2
3 10017 2
What is the relationship between address, city, state, and zip?
Address, City, State, Zip Relationship
addressaddress
idid streetstreet city_idcity_id
1 Kansas State University 1
2 Empire State Building 2
3 Grand Central Station 3
statestate
idid namename
1 KS
2 NY
citycity
idid namename zip_idzip_id
1 Manhattan 1
2 Manhattan 2
3 Manhattan 3
addressaddress citycity zipzip statestate
What is the relationship between address, city, state, and zip?
Address, City, State, Zip Relationship
addressaddress
idid streetstreet zip_idzip_id
1 Kansas State University 1
2 Empire State Building 2
Grand Central Station 3
statestate
idid namename
1 KS
2 NY
citycity
idid namename state_idstate_id
1 Manhattan 1
2 Manhattan 2
zipzip
idid codecode city_idcity_id
1 66506 1
2 10018 2
3 10017 2
addressaddress zipzip citycity statestate
What is the relationship between address, city, state, and zip?
Address, City, State, Zip Relationship
addressaddress
id streetstreet zip_idzip_id
1 Kansas State University 1
2 Empire State Building 2
3 Grand Central Station 3
state
id name
1 KS
2 NY
city
id name
1 Manhattan
zip
id code city_id state_id
1 66506 1 1
2 10018 1 2
3 10017 1 2
addressaddress zipzip citycity
statestate
Forms of Normalization (cont.)Third Normal Form (3NF)
Purpose: Reduce data that is not dependant on the primary key, but is dependant on other data in the table
Note: The Third Normal Form process may not be necessary if First and Second Normal Form process has been performed. (refinement of 1NF and 2NF)
ID First Last Address City State Zip Phone
1 Fred McClurg 1640 Fawn Drive
North Liberty
IA 52317 319-123-4567
Book_to_AuthorBook_to_Author
Book_IDBook_ID Author_IDAuthor_ID
1 1
1 2
2 3
OrderOrder
User_IDUser_ID Book_IDBook_ID PlacedPlaced
1 1 Jan 9, 2009
1 2 Jan 11, 2009
Third Normal Form Problem
UsersUsers
ID 1
First Fred
Last McClurg
Address 1640 Fawn Drive
Zip_ID 1
Phone 319-123-4567
ZipZip
ID 1
Code 52317
City_ID 1
State_ID 1
CityCity
ID 1
Name North Liberty
StateState
ID 1
Abbrev IA
Book
IDID TitleTitle
1 The Case for a Creator
2 More than a Carpenter
AuthorAuthor
IDID NameName
1 Lee Strobel
2 Garry Poole
3 Josh McDowell
Denormalization
Quote:“Normalize until it hurts. Denormalize until it helps.”
Description: Normalization often involves separating data into tables. Denormalization is the process of grouping data together.
Denormalization (cont.)
Book
ID Title ISBN_ID
1 Dinner with a Perfect Stranger
1
2 A Day with a Perfect Stranger
2
ISBN
ID Code
1 1578569052
2 1400072425
Book
ID Title ISBN
1 Dinner with a Perfect Stranger
1578569052
2 A Day with a Perfect Stranger
1400072425
to be to be continued ...continued ...
http://webcert.kirkwood.edu/~fmcclurg/courses/php/slides/chapter09e.performance.ppt