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

Cascading Actions<br />

164<br />

One important difference between foreign keys and other kinds of keys is that foreign keys are bidirectional;<br />

that is, they not only restrict the child table to values that exist in the parent, but they also check for child<br />

rows whenever we do something to the parent (thus preventing orphans). The default behavior is for<br />

<strong>SQL</strong> <strong>Server</strong> to “restrict” the parent row from being deleted if any child rows exist. Sometimes, however,<br />

we would rather automatically delete any dependent records rather than prevent the deletion of the<br />

referenced record. The same notion applies to updates to records where we would like the dependent<br />

record to automatically reference the newly updated record. Somewhat more rare is the instance where<br />

you want to alter the referencing row to some sort of known state. For this, you have the option to set<br />

the value in the dependent row to either NULL or whatever the default value is for that column.<br />

The process of making such automatic deletions and updates is known as cascading. This process, especially<br />

for deletes, can actually run through several layers of dependencies (where one record depends on<br />

another, which depends on another, and so on). So, how do we implement cascading actions in <strong>SQL</strong><br />

<strong>Server</strong>? All we need is a modification to the syntax we use when declaring our foreign key. We just add<br />

the ON clause that we skipped at the beginning of this section.<br />

Let’s check this out by adding a new table to our Accounting database. We’ll make this a table to store<br />

the individual line items in an order, and we’ll call it OrderDetails:<br />

CREATE TABLE OrderDetails<br />

(<br />

OrderID int NOT NULL,<br />

PartNo varchar(10) NOT NULL,<br />

Description varchar(25) NOT NULL,<br />

UnitPrice money NOT NULL,<br />

Qty int NOT NULL,<br />

CONSTRAINT PKOrderDetails<br />

PRIMARY KEY (OrderID, PartNo),<br />

CONSTRAINT FKOrderContainsDetails<br />

FOREIGN KEY (OrderID)<br />

REFERENCES Orders(OrderID)<br />

ON UPDATE NO ACTION<br />

ON DELETE CASCADE<br />

);<br />

This time we have a whole lot going on, so let’s take it apart piece by piece.<br />

Before we get too far into looking at the foreign key aspects of this, notice something<br />

about how the primary key was done here. Instead of placing the declaration<br />

immediately after the key, I decided to declare it as a separate constraint item. This<br />

helps facilitate the multicolumn primary key (which therefore could not be declared<br />

as a column constraint) and the clarity of the overall CREATE TABLE statement. Likewise,<br />

I could have declared the foreign key either immediately following the column<br />

or, as I did here, as a separate constraint item. I’ll touch on this a little bit later in the<br />

chapter.<br />

First, notice that our foreign key is also part of our primary key. This is not at all uncommon in<br />

child tables, and is actually almost always the case for associate tables (more on this next chapter).

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

Saved successfully!

Ooh no, something went wrong!