5.1. Types of Constraints

There are several ways to implement constraints, but each of them falls into one of three categories — entity, domain, or referential integrity constraints — as illustrated in Figure 5-1.

Figure 5.1. Figure 5-1

5.1.1. Domain Constraints

Domain constraints deal with one or more columns. They ensure that a particular column or set of columns meets particular criteria. When you insert or update a row, the constraint is applied without respect to any other row in the table; it's the column's data you're interested in.

For example, if you want to confine the UnitPrice column to values that are greater than or equal to zero, you would use a domain constraint. Although any row that had a UnitPrice that didn't meet the constraint would be rejected, you're actually enforcing integrity to make sure that entire column (no matter how many rows) meets the constraint. The domain is the column, and the constraint is a domain constraint.

You'll use this kind of constraint when dealing with CHECK constraints, rules, defaults, and DEFAULT constraints.

5.1.2. Entity Constraints

Entity constraints are all about individual rows. This form of constraint doesn't really care about a column as a whole. It's interested in a particular row, and would best be exemplified by a constraint that requires every row to have a unique value for a column or combination of columns.

"What," ...

Get Professional SQL Server™ 2005 Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.