Chapter 11. Using Logic to Formulate SQL Expressions

IN Chapter 6, I DESCRIBED THE PROCESS OF EXPRESSION TRANSFORMATION AS IT APPLIED TO relational algebra expressions specifically; I showed how one such expression could be transformed into another logically equivalent one, using various transformation laws. The laws I considered included such things as:

  1. Restrict distributes over union, intersect, and difference

  2. Project distributes over union but not over intersect and difference

and several others. (As you might expect, analogous laws apply to expressions of the relational calculus also, though I didn’t say much about such laws in Chapter 10.)

Now, the purpose of such transformations, as I discussed them earlier, was essentially optimization; the aim was to come up with an expression with the same semantics as the original one but better performance characteristics. However, the concept of expression transformation—or query rewrite, as it’s sometimes (not very appropriately) known—has application in other areas, too. In particular, and very importantly, it can be used to transform precise logical expressions representing queries and the like into SQL equivalents. And that’s what this chapter is all about: It shows how to take the logical or relational calculus formulation of some query or constraint (for example) and map it systematically into an SQL equivalent. And while the SQL formulation so obtained can sometimes be hard to understand, we know it’s correct, because of the systematic ...

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.