SQL and Constraints
A foolish consistency is the hobgoblin of little minds.
—Ralph Waldo Emerson: “Self Reliance” (1841)
I’ve touched on the topic of integrity constraints here and there in previous chapters, but it’s time to get more specific. Here’s a rough definition, repeated from Chapter 1: An integrity constraint (constraint for short) is basically just a boolean expression that must evaluate to TRUE. Constraints in general are so called because they constrain the values that can legally appear as values of some variable; but the ones we’re interested in here are the ones that apply to variables in the database (i.e., relvars) specifically.1 Such constraints fall into two broad categories, type constraints and database constraints. In essence:
A type constraint defines the values that constitute a given type.
A database constraint further constrains the values that can appear in a given database (where by “further” I mean over and above the constraints already imposed by the pertinent type constraints).
As usual, in what follows I’ll discuss these ideas in both relational and SQL terms.
By the way, it’s worth noting that constraints in general can be regarded as a formal version of what some people call business rules. Now, this latter term doesn’t really have a precise definition (at least, not one that’s universally accepted); in general, however, a business rule is a declarative statement—emphasis on declarative—of some aspect of the enterprise the database ...