200602-08 | Database Normalization | © MySQL AB 2006 | 1 An Introduction to Database Normalization Mike Hillyer – MySQL AB
200602-08 | Database Normalization | © MySQL AB 2006 | 2 About Me Member of the MySQL AB documentation team MySQL Core and Pro Certified MySQL expert at Resident MySQL expert at SearchOpenSource.com Mike Hillyer, BSc
200602-08 | Database Normalization | © MySQL AB 2006 | 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…
200602-08 | Database Normalization | © MySQL AB 2006 | 4 About This Session 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
200602-08 | Database Normalization | © MySQL AB 2006 | 5 What Is Database Normalization? Cures the ‘SpreadSheet Syndrome’ Store only the minimal amount of information. Remove redundancies. Remove anomalies. Restructure data.
200602-08 | Database Normalization | © MySQL AB 2006 | 6 What are the Benefits of Database Normalization? Decreased storage requirements! 1 VARCHAR(20) converted to 1 TINYINT UNSIGNED in a table of1 million rows is a savings of~20 MB Faster search performance! –Smaller file for table scans. –More directed searching. Improved data integrity!
200602-08 | Database Normalization | © MySQL AB 2006 | 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) Sixth Normal Form (6NF)
200602-08 | Database Normalization | © MySQL AB 2006 | 8 Our Table TitleAuthorBioISBNSubjectPagesPublisher Beginning MySQL Database Design and Optimization Chad Russell Jon Stephens Chad Russell is a programmer and network administrator who owns his own Internet hosting company. Jon Stephens is a member of the MySQL AB documentation team MySQL Database Design 520Apress
200602-08 | Database Normalization | © MySQL AB 2006 | 9 First Normal Form All values must be atomic 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
200602-08 | Database Normalization | © MySQL AB 2006 | 10 Satisfying 1NF ISBNTitlePages Beginning MySQL Database Design and Optimization 520 Book Author_I D First_NameLast_name 1ChadRussell 2JonStephens 3MikeHillyer Author Subject_IDName 1MySQL 2Database Design Subject Publisher_IDNameAddressCityStateZip 1Apress 2560 Ninth Street, Station 219 BerkeleyCalifornia94710 Publisher
200602-08 | Database Normalization | © MySQL AB 2006 | 11 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
200602-08 | Database Normalization | © MySQL AB 2006 | 12 Many-to-Many (Joining Tables) ISBNAuthor_ID ISBNSubject_ID Book_Author Book_Subject
200602-08 | Database Normalization | © MySQL AB 2006 | 13 One-To-Many ISBNTitlePagesPublisher_ID Beginning MySQL Database Design and Optimization 5201 Book
200602-08 | Database Normalization | © MySQL AB 2006 | 14 Second Normal Form Table must be in First Normal Form Composite keys –All columns in a row must refer to the entire key Benefits –Increased storage efficiency –Less data repetition ISBNAuthor_IDSummaryAuthor_URL A great book! Review
200602-08 | Database Normalization | © MySQL AB 2006 | 15 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 depend directly on the primary key “The key, the whole key, and nothing but the key” Benefits –No extraneous data
200602-08 | Database Normalization | © MySQL AB 2006 | 16 Satisfying Third Normal Form Publisher_IDNameAddressZip 1Sams Publishing800 East 96th Street46240 Publisher ZipCityState 46240IndianapolisIndiana Zip
200602-08 | Database Normalization | © MySQL AB 2006 | 17 Finding Balance
200602-08 | Database Normalization | © MySQL AB 2006 | 18 Joining Tables Two Basic Joins –Inner-Join –Outer Join LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN
200602-08 | Database Normalization | © MySQL AB 2006 | 19 Inner Join mysql> SELECT First_Name, Last_Name, ISBN -> FROM Author INNER JOIN Book_Author ON Author.Author_ID = Book_Author.Author_ID; | First_Name | Last_Name | ISBN | | Chad | Russell | | | Jon | Stephens | | rows in set (0.05 sec)
200602-08 | Database Normalization | © MySQL AB 2006 | 20 LEFT OUTER JOIN mysql> SELECT First_Name, Last_Name, ISBN -> FROM Author LEFT OUTER JOIN Book_Author ON Author.Author_ID = Book_Author.Author_ID; | First_Name | Last_Name | ISBN | | Chad | Russell | | | Jon | Stephens | | | Mike | Hillyer | NULL | rows in set (0.00 sec)
200602-08 | Database Normalization | © MySQL AB 2006 | 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
200602-08 | Database Normalization | © MySQL AB 2006 | 22 Conclusion normalization.html MySQL Database Design and Optimization –Jon Stephens & Chad Russell –Chapter 3 –ISBN –
200602-08 | Database Normalization | © MySQL AB 2006 | 23 QUESTIONS? Feel free to ask now or find me after this session!