Chapter 10. SQL and Logic

AS I MENTIONED IN Chapter 1, THERE’S AN ALTERNATIVE TO THE RELATIONAL ALGEBRA CALLED THE RELATIONAL CALCULUS. What this means is that queries, constraints, view definitions, and so forth can all be formulated in calculus terms as well as algebraic ones; sometimes, in fact, it’s easier to come up with a calculus formulation, though the opposite can also be true.

What is the relational calculus? Essentially, it’s an applied form of predicate calculus (also known as predicate logic), tailored to the needs of relational databases. So the aims of this chapter are to introduce the relevant features of predicate logic (hereinafter abbreviated to just logic); to show how those features are realized in concrete form in the relational calculus; and, finally, to consider the corresponding features of SQL as we go.

Incidentally, it follows from the above that a relational language can be based on either the algebra or the calculus. For example, Tutorial D is explicitly based on the algebra (which is why there aren’t many references to Tutorial D in this chapter), and Query-By-Example (see Appendix D) is explicitly based on the calculus. So which is SQL based on? The answer, regrettably, is partly both and partly neither… When it was first designed, SQL was specifically intended to be different from both the algebra and the calculus; indeed, such a goal was the prime motivation for the introduction of the SQL “IN subquery” construct.[70] As time went on, however, it turned ...

Get SQL and Relational Theory 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.