FREE BOOK

Chapter 4 - Normalizing a Data Model

Posted by Addison Wesley Free Book | C# Language September 01, 2009
Data normalization is probably one of the most talked-about aspects of database modeling. Before building your data model, you must answer a few questions about normalization. These questions include whether or not to use the formal normalization forms, which of these forms to use, and when to denormalize.

First Normal Form (1NF)

In first normal form, every entity in the database has a primary key attribute (or set of attributes). Each attribute must have only one value, and not a set of values. For a database to be in 1NF it must not have any repeating groups. A repeating group is data in which a single instance may have multiple values for a given attribute.

For example, consider a recording studio that stores data about all its artists and their albums. Table 4.1 outlines an entity that stores some basic data about the artists signed to the recording studio.

Table 4.1 Artists and Albums: Repeating Groups of Data

Artist Name Genre Album Name Release Date
The Awkward Stage Rock Home 10/01/2006
Girth Metal On the Sea 5/25/1997
Wasabi Peanuts Adult Contemporary Rock Spicy Legumes 11/12/2005
The Bobby R&B Live! 7/27/1985
Jenkins Band   Running the Game 10/30/1988
Juices of Brazil Latin Jazz
White
Long Road 1/01/2003
6/10/2005

Notice that for the first artist, there is only one album and therefore one release date. However, for the fourth and fifth artists, there are two albums and two release dates. In practice, we cannot guarantee which release date belongs to which album. Sure, it'd be easy to assume that the first release date belongs to the first album name, but how can we be sure that album names and dates are always entered in order and not changed afterward?

There are two ways to eliminate the problem of the repeating group. First, we could add new attributes to handle the additional albums, as in Table 4.2.

Table 4.2 Artists and Albums: Eliminate the Repeating Group, but at What Cost?

Artist Name Genre Album Name 1 Release Date 1 Album Name 2 Release Date 2
The Awkward Stage Rock Home 10/01/2006 Null Null
Girth Metal On the Sea 5/25/1997 Null Null
Wasabi
Peanuts
Adult
Contemporary Rock
Spicy Legumes 11/12/2005 Null Null
The Bobby
Jenkins Band
R&B Running
the Game
7/27/1985 Live! 10/30/1988
Juices of Brazil Latin Jazz Long Road 1/01/2003 White 6/10/2005

We've solved the problem of the repeating group, and because no attribute contains more than one value, this table is in 1NF. However, we've introduced a much bigger problem: what if an artist has more than two albums? Do we keep adding two attributes for each album that any artist releases? In addition to the obvious problem of adding attributes to the entity, in the physical implementation we are wasting a great deal of space for each artist who has only one album. Also, querying the resultant table for album names would require searching every album name column, something that is very inefficient.

If this is the wrong way, what's the right way? Take a look at Tables 4.3 and 4.4.

Table 4.3 The Artists

ArtistName Genre
The Awkward Stage Rock
Girth Metal
Wasabi Peanuts Adult Contemporary Rock
The Bobby Jenkins Band R&B
Juices of Brazil Latin Jazz

Table 4.4 The Albums

AlbumName ReleaseDate ArtistName
White 6/10/2005 Juices of Brazil
Home 10/01/2006 The Awkward Stage
On the Sea 5/25/1997 Girth
Spicy Legumes 11/12/2005 Wasabi Peanuts
Running the Game 7/27/1985 The Bobby Jenkins Band
Live 10/30/1988 The Bobby Jenkins Band
Long Road 1/01/2003 Juices of Brazil

We've solved the problem by adding another entity that stores album names as well the attribute that represents the relationship to the artist entity. Neither of these entities has a repeating group, each attribute in both entities holds a single value, and all of the previously mentioned query problems have been eliminated. This database is now in 1NF and ready to be deployed, right? Considering there are several other normal forms, we think you know the answer.

Total Pages : 7 12345

comments