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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

This kind of relationship usually sets up what is called a domain relationship. A<br />

domain is a limited list of values that the dependent table must choose from —<br />

nothing outside the domain list is considered a valid option. The table that holds the<br />

rows that make up the domain list is commonly referred to as a domain or lookup<br />

table. Nearly all databases you create are going to have at least one, and probably<br />

many, domain tables in them. Our Shippers table is a domain table — the purpose<br />

of having it isn’t just to store the information on the name and phone number of the<br />

shipper, but also to limit the list of possible shippers in the Orders table.<br />

In <strong>SQL</strong> <strong>Server</strong>, we can enforce this kind of relationship through two methods:<br />

❑ FOREIGN KEY constraint: You simply declare a FOREIGN KEY constraint on the table that serves<br />

as the “many” side of the relationship, and reference the table and column that is to be the “one”<br />

side of the relationship (you’ll be guaranteed only one in the referenced table since you must<br />

have a PRIMARY KEY or UNIQUE constraint on the column(s) referenced by a foreign key).<br />

❑ Triggers: Actually, for all the early versions of <strong>SQL</strong> <strong>Server</strong>, this was the only option for true referential<br />

integrity. You actually need to add two triggers — one for each side of the relationship.<br />

Add a trigger to the table that is the “many” side of the relationship and check that any row<br />

inserted or changed in that table has a match in the table it depends on (the “one” side of the<br />

relationship). Then, you add a delete trigger and an update trigger to the other table — this trigger<br />

checks records that are being deleted (or changed) from the referenced table to make sure that it<br />

isn’t going to orphan (make it so it doesn’t have a reference).<br />

We’ve previously discussed the performance ramifications of the choices between the two in Chapter 6.<br />

Using a FOREIGN KEY constraint is generally faster — particularly when there is a violation. That being<br />

said, triggers may still be the better option in situations where you’re going to have a trigger executing<br />

anyway (or some other special constraint need).<br />

Many-to-Many<br />

In this type of relationship, both sides of the relationship may have several records — not just one — that<br />

match. An example of this would be the relationship of products to orders. A given order may have many<br />

different products in the order. Likewise, any given product may be ordered many times. We still may,<br />

however, want to relate the tables in question — for example, to ensure that an order is for a product<br />

that we know about (it’s in our Products table).<br />

<strong>SQL</strong> <strong>Server</strong> has no way of physically establishing a direct many-to-many relationship, so we cheat by<br />

having an intermediate table to organize the relationship. Some tables create our many-to-many relationships<br />

almost by accident as a normal part of the normalization process — others are created entirely from<br />

scratch for the sole purpose of establishing this kind of relationship. This latter “middleman” kind of<br />

table is often called either a linking table, an associate table, or sometimes a merge table.<br />

First, let’s look at a many-to-many relationship that is created in the normal course of normalization. An<br />

example of this can be found in the Accounting database’s OrderDetails table (we created the Accounting<br />

database in Chapters 5 and 6), which creates a many-to-many relationship between our Orders and<br />

Products tables, shown in Figure 8-5.<br />

231

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

Saved successfully!

Ooh no, something went wrong!