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

Rules and Defaults —<br />

Cousins of Constraints<br />

Rules and defaults have been around much longer than CHECK and DEFAULT constraints have been. They<br />

are something of an old <strong>SQL</strong> <strong>Server</strong> standby, and are definitely not without their advantages.<br />

That being said, I’m going to digress from explaining them long enough to recommend that you look<br />

them over for backward compatibility and legacy-code familiarity only. Rules and defaults are not ANSI<br />

compliant (which creates portability issues), and they do not perform as well as constraints do. <strong>Microsoft</strong><br />

has listed rules and defaults as there only for backward compatibility since version 7.0 — that’s four versions<br />

and roughly ten years — not an encouraging thing if you’re asking yourself whether this feature is<br />

going to continue to be supported in the future. I wouldn’t go so far as to suggest that you start sifting<br />

through and replacing any old code that you may come across, but you should use constraints for any<br />

new code you generate.<br />

The primary thing that sets rules and defaults apart from constraints is in their very nature; constraints<br />

are features of a table — they have no existence on their own — while rules and defaults are actual<br />

objects in and of themselves. Whereas a constraint is defined in the table definition, rules and defaults<br />

are defined independently and are then “bound” to the table after the fact.<br />

The independent-object nature of rules and defaults gives them the ability to be reused without being<br />

redefined. Indeed, rules and defaults are not limited to being bound to just tables; they can also be<br />

bound to data types, vastly improving your ability to make highly functional user-defined data types.<br />

Rules<br />

180<br />

A rule is incredibly similar to a CHECK constraint. The only difference beyond those I’ve already<br />

described is that rules are limited to working with just one column at a time. You can bind the same rule<br />

separately to multiple columns in a table, but the rule will work independently with each column and<br />

will not be aware of the other columns at all. A constraint defined as (QtyShipped 0;<br />

Notice that what we are comparing is shown as a variable. Whatever the value is of the column being<br />

checked, that is the value that will be used in the place of @Salary. Thus, in this example, we’re saying<br />

that any column our rule is bound to would have to have a value greater than zero.<br />

If you want to go back and see what your rule looks like, you can make use of sp_helptext:<br />

EXEC sp_helptext SalaryRule;

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

Saved successfully!

Ooh no, something went wrong!