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.
Parts
» This page intentionally left blank
» Reporting and Data Mining Database Applications
» Database Applications and SQL
» Reading Specified Columns and Rows from a Single Table
» “Does Not Work with Microsoft Access ANSI-89 SQL”
» Processing SQL Statements in Microsoft Access 2010
» Using SQL in Microsoft SQL Server 2008 R2
» Using SQL in Oracle Database 11g
» Using SQL in Oracle MySQL 5.5
» Wildcards in SQL WHERE Clauses
» Using SQL Built-in Functions
» SQL Expressions in SQL SELECT Statements
» Querying Multiple Tables with Subqueries
» Querying Multiple Tables with Joins
» Comparing Subqueries and Joins
» Finding Functional Dependencies
» Eliminating Anomalies from Multivalued Dependencies
» The Multivalue, Multicolumn Problem
» The General-Purpose Remarks Column
» R Diagrams Using the IE Crow’s Foot Model
» The Multivalued Attribute Pattern
» The Archetype/Instance Pattern
» The Student Acceptance Letter
» X This is a warning, no further action is required.
» 1:1 Relationships Between Strong Entities
» M Relationships Between Strong Entities
» Relationships in Mixed Entity Designs
» Representing Ternary and Higher-Order Relationships
» Relational Representation of the Highline University Data Model
» Surrogate Key Database Design
» Column Properties for the View Ridge Database Design Tables
» Variations in SQL Data Types
» Implementing Data Constraints
» Populating the View Ridge Database Tables
» Using Triggers to Provide Default Values
» The WORK_AddWorkTransaction Stored Procedure
» • If a PROJECT row is deleted, then the project has been canceled, and it is unneces-
» Reducing Cardinalities (with Data Loss)
» Optimistic Versus Pessimistic Locking
» Declaring Lock Characteristics
» Processing Rights and Responsibilities
» Recovery via Rollback/Rollforward
» Maintaining the Data Repository
» Types of Distributed Databases
» • Express Edition. This free, feature-limited version is available for download. It
» SQL Server 2008 R2 SQL Statements and SQL Scripts
» Creating the View Ridge Database Table Structure
» Populating the VRG Tables with Data
» The Stored Procedure InsertCustomerAndInterests
» The Stored Procedure InsertCustomerWithTransaction
» A Trigger for Setting Default Values
» A Trigger for Enforcing a Data Constraint
» A Trigger for Enforcing a Required Child Constraint
» Creating an ODBC Data Source Name
» Materializing XML Documents with XSLT
» Using the SQL SELECT . . . FOR XML Statement
» Multitable SELECT with FOR XML
» A Schema with Two Multivalued Paths
» Problems with Operational Data
» Using SQL for Market Basket Analysis
Show more