EXAMPLE 2: UNIVERSAL QUANTIFICATION

Now, I was practicing a tiny deception in Example 1, inasmuch as I was pretending that the specific part to which the constraint applied was understood. But that’s effectively just what happens with base table constraints in SQL; they’re tacitly understood to apply to each and every row of the base table whose definition they’re part of. However, suppose we wanted to be more explicit—i.e., suppose we wanted to state explicitly that the constraint applies to every part that happens to be represented in table P. In other words, for all such parts PX, if the color of part PX is red, then the city for part PX is London:

     FORALL PX ( IF PX.COLOR = 'Red' THEN PX.CITY = 'London' )

Note: The name PX and others like it in this chapter are deliberately chosen to be reminiscent of the range variables used in examples in the previous chapter. In fact, I’m going to assume from this point forward that names of the form PX, PY, etc., denote variables that range over the current value of table P; names of the form SX, SY, etc., denote variables that range over the current value of table S; and so on.[156] Details of how such variables are defined—in logic, I mean, not in SQL—aren’t important for present purposes and are therefore omitted. In SQL, they’re defined by means of AS clauses, which I’ll show when we get to the SQL formulations as such.

Now, SQL doesn’t support FORALL, but the quantification law tells us that the foregoing expression can be transformed ...

Get SQL and Relational Theory, 2nd Edition 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.