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.

Just remember that each constraint stands alone — you add, change, or delete each of them<br />

independently.<br />

Next, look at our foreign key declaration:<br />

FOREIGN KEY (OrderID)<br />

REFERENCES Orders(OrderID)<br />

We’ve declared our OrderID as being dependent on a “foreign” column. In this case, it’s for a column<br />

(also called OrderID) in a separate table (Orders), but as we saw earlier in the chapter, it could just as<br />

easily have been in the same table.<br />

Now, to get to the heart of our cascading issue, we need to look at our ON clauses:<br />

ON UPDATE NO ACTION<br />

ON DELETE CASCADE<br />

We’ve defined two different referential integrity actions. As you might guess, a referential integrity action<br />

is what you want to have happen whenever the referential integrity rule you’ve defined is invoked. For<br />

situations where the parent record (in the Orders table) is updated, we’ve said that we do not want that<br />

update to be cascaded to our child table (OrderDetails). For illustration purposes, however, I’ve chosen<br />

a CASCADE for deletes.<br />

Note that NO ACTION is the default, and so specifying this in our code is optional. While the “typical”<br />

way of coding this is to leave out the NO ACTION, I would encourage you to include the NO ACTION<br />

explicitly to make your intent clear.<br />

Let’s try an insert into our OrderDetails table:<br />

INSERT INTO OrderDetails<br />

VALUES<br />

(1, ‘4X4525’, ‘This is a part’, 25.00, 2);<br />

Unless you’ve been playing around with your data some, this generates an error:<br />

Chapter 6: Constraints<br />

There is something of a “gotcha” when creating foreign keys that reference the same<br />

table the foreign key is being defined on. Foreign keys of this nature are not allowed<br />

to have declarative CASCADE actions. The reason for this restriction is to avoid<br />

cyclical updates or deletes; that is, situations where the first update causes another,<br />

which in turn tries to update the first. The result could be a never-ending loop.<br />

Msg 547, Level 16, State 0, Line 1<br />

The INSERT statement conflicted with the FOREIGN KEY constraint<br />

“FKOrderContainsDetails”. The conflict occurred in database “Accounting”, table<br />

“dbo.Orders”, column ‘OrderID’.<br />

The statement has been terminated.<br />

Why? Well, we haven’t inserted anything into our Orders table yet, so how can we refer to a record in<br />

the Orders table if there isn’t anything there?<br />

165

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

Saved successfully!

Ooh no, something went wrong!