Anda di halaman 1dari 6

Normalize your data easily

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

Subject Pages Publishe


r
Beginning
Chad Russell is 90593324 MySQL 520 Apress
MySQL
a programmer
Database
Chad
Database
Design
Russell, and system
Design and
administrator
Optimizatio Jon
who owns his
n
Stephen own internet
hosting
s
company. Jon
Stephens is a
member of the
MySQL AB
documentation
team.
In the example shown above, a lot of storage space will be wasted if any one criterion (author or
publisher) is considered as the identification key. Database normalization, thus, is essential. It is
a step by step process that cannot be carried out haphazardly. The following steps will help in
attaining database normalization in MySQL.
<< Back to Index

Step 1: Create first normal form (1NF)


The database normalization process involves getting data to conform to progressive normal
forms, and a higher level of database normalization cannot be achieved unless the previous levels
have been satisfied. First normal form is the basic level of database normalization.
For 1NF, ensure that the values in each column of a table are atomic; which means they are
unique, containing no sets of values. In our case, Author and Subject do not comply.
One method for bringing a table into 1NF is to separate the entities contained in the table into
separate tables. In our case this would result in Book, Author, Subject, and Publisher tables.
Books table:

ISBN

Title

Pages

1590593324

Beginning MySQL
Database Design and
Optimization

520

Authors table:

<< Back to Index

Author_ID

First
Name

Last Name

Chad

Russell

Jon

Stephens

Mike

Hilyer

<< Back to Index


Subjects table:

Subject _ID Last_name

Russell

Stephens

Publishers table:

Name

Address

City

State

Zip

Publisher_ID

Apress

2 580, Ninth Berkeley California


street, station 219

94710

<< Back to Index

Step 2: Define relationships


Three types of relations can be established:

One-to-(Zero or)-one (Example: marriage)

One-to-(Zero or)-many (Example: kids)

Many-to-many (Example: facebook)

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

<< Back to Index

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

1590593324 Beginning MySQL


Database Design and
Optimization

Pages Publisher_ID

520

<< Back to Index

Step 3: Make second normal form (2NF)


Second normal form (2NF) cuts down the tautological/superfluous data in a table by
selecting it, putting it in new tables and by establishing relations amongst them. In
database normalization, 2NF is about the relations between the composite key columns

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

A great book! http://www.openwin.org

<< Back to Index

Step 4: Third Normal Form (3NF)


This requires that all columns depend directly on the primary key. Tables violate the 3NF
when one column depends on another column which in turn depends on the primary key.
(A transitive dependency)
In the publisher table, the City and State are actually dependent on the zip code not the
Publisher_ID

Publisher_ID Name

Apress

Address

City

State

Zip

2580, Ninth Berkeley California 94710


street, station 219

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.

Anda mungkin juga menyukai