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.

Those Other CASCADE Actions …<br />

So, those were examples of cascading updates and deletes, but what about the other two types of cascade<br />

actions I mentioned? What of SET NULL and SET DEFAULT?<br />

These were added with <strong>SQL</strong> <strong>Server</strong> 2005, so avoid them if you want backward compatibility with <strong>SQL</strong><br />

<strong>Server</strong> 2000, but their operation is very simple: If you perform an update that changes the parent values<br />

for a row, then the child row will be set to either NULL or whatever the default value for that column is<br />

(whichever you chose — SET NULL or SET DEFAULT). It’s just that simple.<br />

Other Things to Think About with Foreign Keys<br />

There are some other things to think about before we’re done with foreign keys. We will be coming back<br />

to this subject over and over again throughout the book, but for now, I just want to get in a couple of<br />

finer points:<br />

❑ What makes values in foreign keys required versus optional<br />

❑ How foreign keys are bi-directional<br />

What Makes Values in Foreign Keys Required vs. Optional<br />

The nature of a foreign key provides us two possible choices regarding how to fill in a column or<br />

columns that have a foreign key defined for them:<br />

❑ Fill the column in with a value that matches the corresponding column in the referenced table.<br />

❑ Do not fill in a value at all and leave the value NULL.<br />

Chapter 6: Constraints<br />

You can make the foreign key completely required (limit your users to just the first option in the preceding<br />

list) by simply defining the referencing column as NOT NULL. Since a NULL value won’t be valid in the<br />

column and the foreign key requires any non-NULL value to have a match in the referenced table, you<br />

know that every row will have a match in your referenced table. In other words, the reference is<br />

required.<br />

Allowing the referencing column to have NULLs will create the same requirement, except that the user<br />

will also have the option of supplying no value — even if there is not a match for NULL in the referenced<br />

table, the insert will still be allowed.<br />

How Foreign Keys Are Bi-Directional<br />

We touched on this some when we discussed CASCADE actions, but when defining foreign keys, I can’t<br />

stress enough that they effectively place restrictions on both tables. Up to this point, we’ve been talking<br />

about things in terms of the referencing table; however, once the foreign key is defined, the referenced<br />

table must also live by a rule:<br />

By default, you cannot delete a record or update the referenced column in a referenced<br />

table if that record is referenced from the dependent table. If you want to be<br />

able to delete or update such a record, then you need to set up a CASCADE action for<br />

the delete and/or update.<br />

169

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

Saved successfully!

Ooh no, something went wrong!