17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8: Being Normal: Normalization and Other Basic Design Issues<br />

220<br />

We have a number of issues to deal with in this table if we’re going to normalize it. While we have a<br />

functional primary key (yes, these existed long before relational systems), we have problems with both<br />

of the main areas of the first normal form:<br />

❑ I have repeating groups of data (customer information). I need to break that out into a different<br />

table.<br />

❑ The ItemsOrdered column does not contain data that is atomic in nature.<br />

We can start by moving several columns out of the table:<br />

OrderNo<br />

(PK)<br />

OrderDate CustomerNo ItemsOrdered<br />

100 1/1/1999 54545 1A4536, Flange, 7lbs, $75;4-OR2400, Injector, .5lbs,<br />

$108;4-OR2403, Injector, .5lbs, $116;1-4I5436, Head,<br />

63lbs, $750<br />

101 1/1/1999 12000 1-3X9567, Pump, 5lbs, $62.50<br />

102 1/1/1999 66651 7-8G9200; Fan, 3lbs, $84;1-8G5437, Fan, 3lbs, $15;1-<br />

3H6250, Control, 5lbs, $32<br />

103 1/2/1999 54545 40-8G9200, Fan, 3lbs, $480;1-2P5523, Housing, 1lbs,<br />

$165;1-3X9567, Pump, 5lbs, $42<br />

And putting them into their own table:<br />

CustomerNo (PK) CustomerName CustomerAddress<br />

54545 ACME Co 1234 1st St.<br />

12000 Sneed Corp. 555 Main Ave.<br />

66651 ZZZ & Co. 4242 SW 2nd<br />

There are several things to notice about the old and new tables:<br />

❑ We must have a primary key for our new table to ensure that each row is unique. For our<br />

Customers table, there are two candidate keys — CustomerNo and CustomerName. CustomerNo<br />

was actually created just to serve this purpose and seems the logical choice — after all, it’s entirely<br />

conceivable that you could have more than one customer with the same name. (For example,<br />

there have to be hundreds of companies named ACME in the U.S.)<br />

❑ Although we’ve moved the data out of the Orders table, we still need to maintain a reference to<br />

the data in the new Customers table. This is why you still see the CustomerNo (the primary<br />

key) column in the Orders table. Later on, when we build our references, we’ll create a foreign<br />

key constraint to force all orders to have valid customer numbers.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!