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 6: Constraints<br />

170<br />

Let’s illustrate this “You can’t delete or update a referenced record” idea.<br />

We just defined a couple of foreign keys for the Orders table. One of those references the EmployeeID<br />

columns of the Employees table. Let’s say, for instance, that we have an employee with an EmployeeID of<br />

10 who takes many orders for us for a year or two, and then decides to quit and move on to another job.<br />

Our tendency would probably be to delete the record in the Employees table for that employee, but that<br />

would create a rather large problem — we would get what are called orphaned records in the Orders<br />

table. Our Orders table would have a large number of records that still have an EmployeeID of 10. If we<br />

are allowed to delete EmployeeID 10 from the Employees table, then we will no longer be able to tell<br />

which employee entered in all those orders. The value for the EmployeeID column of the Orders table<br />

will become worthless!<br />

Now let’s take this example one step further. Let’s say that the employee did not quit. Instead, for some<br />

unknown reason, we wanted to change that employee’s ID number. If we made the change (via an UPDATE<br />

statement) to the Employees table, but did not make the corresponding update to the Orders table, then<br />

we would again have orphaned records. We would have records with a value of 10 in the EmployeeID<br />

column of the Orders table with no matching employee.<br />

Now, let’s take it one more step further! Imagine that someone comes along and inserts a new record<br />

with an EmployeeID of 10. We now have a number of records in our Orders table that will be related to<br />

an employee who didn’t take those orders. We would have bad data (yuck!).<br />

Instead of allowing orphaned records, <strong>SQL</strong> <strong>Server</strong>, by default, restricts us from deleting or updating<br />

records from the referenced table (in this case, the Employees table) unless any dependent records have<br />

already been deleted from or updated in the referencing (in this case, Orders) table.<br />

This is actually not a bad segue into a brief discussion on or about when a CASCADE<br />

action makes sense and when it doesn’t. Data-integrity-wise, we probably wouldn’t<br />

want to allow the deletion of an employee if there are dependent rows in the Orders<br />

table. Not being able to trace back to the employee would degrade the value of our<br />

data. On the other hand, it may be perfectly valid (for some very strange reason) to<br />

change an employee’s ID. We could CASCADE that update to the Orders table with<br />

little ill effect. Another moral to the story here is that you don’t need the same<br />

CASCADE decision for both UPDATE and DELETE — think about each separately (and<br />

carefully).<br />

As you can see, although the foreign key is defined on one table, it actually placed restrictions on both<br />

tables (if the foreign key is self-referenced, then both sets of restrictions are on the one table).<br />

UNIQUE Constraints<br />

These are relatively easy. UNIQUE constraints are essentially the younger sibling of primary keys in that<br />

they require a unique value throughout the named column (or combination of columns) in the table. You<br />

will often hear UNIQUE constraints referred to as alternate keys. The major differences are that they are not<br />

considered to be the unique identifier of a record in a table (even though you could effectively use it that<br />

way) and that you can have more than one UNIQUE constraint (remember that you can only have one primary<br />

key per table).

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

Saved successfully!

Ooh no, something went wrong!