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

At a more specific level, we’ll be looking at the methods of implementing each of these types of constraints,<br />

including:<br />

❑ Primary key constraints<br />

❑ Foreign key constraints<br />

❑ Unique constraints (also known as alternate keys)<br />

❑ Check constraints<br />

❑ Default constraints<br />

❑ Rules<br />

❑ Defaults (similar to default constraints)<br />

We’ll also take a very cursory look at triggers and stored procedures (there will be much more on these<br />

later) as methods of implementing data integrity rules.<br />

Types of Constraints<br />

There are a number of different ways to implement constraints, but each of them falls into one of three<br />

categories — domain, entity, or referential integrity constraints, as illustrated in Figure 6-1.<br />

Domain Constraints<br />

Domain constraints deal with one or more columns. What we’re talking about here is ensuring that a particular<br />

column or set of columns meets particular criteria. When you insert or update a row, the constraint<br />

is applied without respect to any other row in the table — it’s the column’s data you’re interested in.<br />

For example, if we want to confine the UnitPrice column to include only values that are greater than or<br />

equal to zero, that would be a domain constraint. Although any row that had a UnitPrice that didn’t<br />

meet the constraint would be rejected, we’re actually enforcing integrity to make sure that the entire column<br />

(no matter how many rows) meets the constraint. The domain is the column, and our constraint is a<br />

domain constraint.<br />

We’ll see this kind of constraint when dealing with check constraints, rules, defaults, and default constraints.<br />

Entity Constraints<br />

154<br />

Entity constraints are all about individual rows. This form of constraint doesn’t really care about a column<br />

as a whole; it’s interested in a particular row, and would best be exemplified by a constraint that<br />

requires every row to have a unique value for a column or combination of columns.<br />

“What,” you say, “a unique column? Doesn’t that mean it’s a domain constraint?” No, it doesn’t. We’re<br />

not saying that a column has to meet any particular format, or that the value has to be greater or less than<br />

anything. What we’re saying is that for this row, the same value can’t already exist in some other row.<br />

We’ll see this kind of constraint when dealing with primary key and unique constraints.

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

Saved successfully!

Ooh no, something went wrong!