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 />

A second example — one that used to apply frequently to <strong>SQL</strong> <strong>Server</strong> prior to version 7.0 — is when you<br />

have too much information to fit in one row. Remember that the maximum row size for <strong>SQL</strong> <strong>Server</strong> is<br />

8060 bytes of non-BLOB data. That’s a lot harder to fill than version 6.5’s 1962 bytes, but you can still<br />

have situations that require you to store a very large number of columns or even fewer very wide<br />

columns. One way to get around this problem was to actually create two different tables and split our<br />

rows between the tables. We could then impose a one-to-one relationship. The combination of the matching<br />

rows in the two tables then meets our larger row size requirement.<br />

Zero or One-to-One<br />

228<br />

<strong>SQL</strong> <strong>Server</strong> has no inherent method of enforcing a true one-to-one relationship. You<br />

can say that table A requires a matching record in table B, but when you then add<br />

that table B must have a matching record in table A, you create a paradox — which<br />

table gets the record first? If you need to enforce this kind of relationship in <strong>SQL</strong><br />

<strong>Server</strong>, the best you can do is force all inserts to be done via a stored procedure.<br />

The stored procedure can have the logic to insert into both tables or neither table.<br />

Neither foreign key constraints nor triggers can handle this circular relationship.<br />

<strong>SQL</strong> <strong>Server</strong> can handle the instance of zero or one-to-one relationships. This is essentially the same as a<br />

one-to-one, with the difference that one side of the relationship has the option of either having a record<br />

or not.<br />

Going back to our parent company vs. subsidiary example, you might prefer to create a relationship<br />

where the parent company needs to have a matching record for each subsidiary’s records, but the subsidiary<br />

doesn’t need the information from the parent. You could, for example, have subsidiaries that<br />

have very different customers (such as a railroad and a construction company). The parent company<br />

wants to know about all the customers regardless of what business they came from, but your construction<br />

company probably doesn’t care about your railroad customers. In such a case, you would have zero or<br />

one construction customers to one parent company customer record.<br />

Zero or one-to-one relationships can be enforced in <strong>SQL</strong> <strong>Server</strong> through:<br />

❑ A combination of a unique or primary key with a foreign key constraint. A foreign key constraint<br />

can enforce that at least one record must exist in the “one” (or parent company in our<br />

example) table, but it can’t ensure that only one exists (there could be more than one). Using a<br />

primary key or unique constraint would ensure that one was indeed the limit.<br />

❑ Triggers. Note that triggers would be required in both tables.<br />

The reason <strong>SQL</strong> <strong>Server</strong> can handle a zero or one-to-one, but not a one-to-one relationship<br />

is due to the “which goes first” problem. In a true one-to-one relationship,<br />

you can’t insert into either table because the record in the other table isn’t there<br />

yet — it’s a paradox. However, with a zero or one-to-one, you can insert into the<br />

required table first (the “one”), and the optional table (the zero or one), if desired,<br />

second. This same problem will hold true for the “one-to-one or many” and the “one<br />

to zero, one, or many” relationships also.

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

Saved successfully!

Ooh no, something went wrong!