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

184<br />

Restriction Pros Cons<br />

Constraints Fast.<br />

Can reference other columns.<br />

Happen before the command occurs.<br />

ANSI-compliant.<br />

Rules, Defaults Independent objects.<br />

Reusable.<br />

Can be bound to data types.<br />

Happen before the command occurs.<br />

Triggers Ultimate flexibility. Can reference<br />

other columns and other tables. Can<br />

even use .NET to reference information<br />

that is external to your <strong>SQL</strong> <strong>Server</strong>.<br />

Must be redefined for each table.<br />

Can’t reference other tables.<br />

Can’t be bound to data types.<br />

Slightly slower.<br />

Can’t reference across columns.<br />

Can’t reference other tables.<br />

Really meant for backward<br />

compatibility only!!!<br />

Happen after the command occurs.<br />

High overhead.<br />

The main time to use rules and defaults is if you are implementing a rather robust logical model and are<br />

making extensive use of user-defined data types. In this instance, rules and defaults can provide a lot of<br />

functionality and ease of management without much programmatic overhead. You just need to be aware<br />

that they may go away someday. Probably not soon, but someday.<br />

Triggers should only be used when a constraint is not an option. Like constraints, they are attached to<br />

the table and must be redefined with every table you create. On the bright side, they can do most things<br />

that you are likely to want to do data integrity–wise. Indeed, they used to be the common method of<br />

enforcing foreign keys (before FOREIGN KEY constraints were added). I will cover these in some detail<br />

later in the book.<br />

That leaves constraints, which should become your data integrity solution of choice. They are fast and<br />

not that difficult to create. Their downfall is that they can be limiting (they can’t reference other tables<br />

except for a FOREIGN KEY), and they can be tedious to redefine over and over again if you have a common<br />

constraint logic.<br />

Regardless of what kind of integrity mechanism you’re putting in place (keys, triggers,<br />

constraints, rules, defaults), the thing to remember can best be summed up in<br />

just one word — balance.<br />

Every new thing that you add to your database adds more overhead, so you need to<br />

make sure that whatever you’re adding honestly has value to it before you stick it in<br />

your database. Avoid things like redundant integrity implementations (for example, I<br />

can’t tell you how often I’ve come across a database that has both foreign keys defined<br />

for referential integrity and triggers to do the same thing). Make sure you know what<br />

constraints you have before you put the next one on, and make sure you know exactly<br />

what you hope to accomplish with it.

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

Saved successfully!

Ooh no, something went wrong!