Normalizing with SQL

Normalizing with SQL

As we discussed in Chapter 3, a table is in BCNF if all determinants are candidate keys. If any determinant is not a candidate key, we must break the table into two or more tables. Consider an example. Suppose you are given the EQUIPMENT_REPAIR table in Figure 4-3 (the same table shown in Figure 3-10). In Chapter 3, we found that ItemNumber is a determinant, but not

a candidate key. Consequently, we created the EQUIPMENT_ITEM and REPAIR tables shown in Figure 4-4. In these tables, ItemNumber is a determinant and a candidate key of EQUIPMENT_ITEM, and RepairNumber is a determinant and primary key of REPAIR; thus both tables are in BCNF.

Now, as a practical matter, how do we transform the data in the format in Figure 4-3 to that in Figure 4-4? To answer that question, we need to use the SQL INSERT statement. You will learn the particulars of the INSERT statement in Chapter 7. For now, we will jump ahead and use one version of it to illustrate the practical side of normalization.

First, we need to create the structure for the two new tables in Figure 4-4. If you are using Microsoft Access, you can follow the procedure in Appendix A to create the tables. Later, in Chapter 7, you will learn how to create tables using SQL, a process that works for all DBMS products.

Once the tables are created, you can fill them using the SQL INSERT command. To fill the ITEM table, we use:

/* *** SQL-INSERT-CH04-01 *** */ INSERT INTO EQUIPMENT_ITEM

SELECT

DISTINCT ItemNumber, EquipmentType, AcquisitionCost

FROM

EQUIPMENT_REPAIR;

Notice that we must use the DISTINCT keyword because the combination (ItemNumber, EquipmentType, AcquisitionCost) is not unique in the EQUIPMENT_REPAIR table. Once we

Figure 4-4

EQUIPMENT_ITEM

The Normalized EQUIPMENT_ITEM and REPAIR Relations

REPAIR

Chapter 4 Database Design Using Normalization

have created the rows in EQUIPMENT_ITEM, we can then use the following INSERT com- mand to fill the rows of REPAIR:

/* *** SQL-INSERT-CH04-02 *** */ INSERT INTO REPAIR

SELECT RepairNumber, ItemNumber, RepairDate, RepairCost FROM

EQUIPMENT_REPAIR;

As you can see, the SQL statements for normalizing tables are relatively simple. After this transformation, we should probably remove the EQUIPMENT_REPAIR table. For now, you can do this using the graphical tools in Microsoft Access, SQL Server, Oracle Database, or MySQL. In Chapter 7, you will learn how to remove tables using the SQL DROP TABLE statement. You will also learn how to use SQL to create the referential integrity constraint:

REPAIR.ItemNumber must exist in ITEM.ItemNumber If you want to try out this example, download the Microsoft Access 2010 database

Equipment-Repair-Database.accdb from the text’s Web site at www.pearsonhighered.com/ kroenke . This database has the EQUIPMENT_REPAIR table with data. Create the new tables (see Appendix A) and then do the normalization by executing the SQL INSERT statements illustrated.

This process can be extended to any number of tables. We will consider richer examples of it in Chapter 7. For now, however, you should have the gist of the process.