The following example will illustrate how database normalization helps achieve a good design in
MySQL. The table below presents data that needs to be captured in the database.
Title
Author
Bio
ISBN
ISBN
Title
Pages
1590593324
Beginning MySQL
Database Design and
Optimization
520
Authors table:
Author_ID
First
Name
Last Name
Chad
Russell
Jon
Stephens
Mike
Hilyer
Russell
Stephens
Publishers table:
Name
Address
City
State
Zip
Publisher_ID
Apress
94710
The books table may have many to many relations with the Authors table.
Authors table may have many books and a book may have more than one author.
The Books table may have many to many relations with the Subject table.
The books may fit in many subjects and the subjects may have many books.
Many-to-many relations have to be presented by link tables
Book Author table:
ISBN
Subject_ID
1590593324
1590593324
Book_Subject table:
ISBN
Subject_ID
1590593324
1590593324
One-to-many in our example will be Books to Publisher. Each book has only one
Publisher but one Publisher may have many books.
We can achieve one-to-many relationships with a foreign key. A foreign key is a
mechanism in database management systems (DBMS) that defines relations and creates
constraints between data segments. It is not possible to review what is not related to the
specific book. It is not possible to have a book without an author or publisher.
When deleting a publisher, all the related books may need to be deleted along with the
reviews of those books. The authors would not be needed to be deleted.
The foreign key is introduced in the table that represents the many, pointing to the
primary key on the one table. Since the Book table represents the many portion of the
one-to-many relationship, the primary key value of the Publisher as in a Publisher_ID
column as a foreign key is added.
ISBN
Title
Pages Publisher_ID
520
and non-key columns. That means the non-key columns have to depend on the whole
composite key.
Here, the primary key is composite to eliminate the possibility of the same person writing
more than one review of the book. Reviewer URL is dependent only on the Reviewer ID
which is only part of the whole composite key.
This table does not comply with the 2NF:
ISBN
Reviewer ID
1590593324
Summary
Reviewer_URL
Publisher_ID Name
Apress
Address
City
State
Zip
To comply with 3NF we have to move these outside the publishers table:
Zip
City
State
94710
Berkeley
California
Through the process of database normalization we bring our schema's tables into
conformance with progressive normal forms. As a result the tables each represent a single
entity (a book, an author, a subject, etc) and we benefit from decreased redundancy, fewer
anomalies and improved efficiency.