Relational calculus
This article includes a list of references, related reading, or external links, but its sources remain unclear because it lacks inline citations. (November 2021) |
The relational calculus consists of two calculi, the tuple relational calculus and the domain relational calculus, that is part of the relational model for databases and provide a declarative way to specify database queries. The raison d'être of relational calculus is the formalization of query optimization, which is finding more efficient manners to execute the same query in a database.
The relational calculus is similar to the relational algebra, which is also part of the relational model: While the relational calculus is meant as a declarative language that prescribes no execution order on the subexpressions of a relational calculus expression, the relational algebra is meant as an imperative language: the sub-expressions of a relational algebraic expression are meant to be executed from left-to-right and inside-out following their nesting.
Per Codd's theorem, the relational algebra and the domain-independent relational calculus are logically equivalent.
Example
[edit]A relational algebra expression might prescribe the following steps to retrieve the phone numbers and names of book stores that supply Some Sample Book:
- Join book stores and titles over the BookstoreID.
- Restrict the result of that join to tuples for the book Some Sample Book.
- Project the result of that restriction over StoreName and StorePhone.
A relational calculus expression would formulate this query in the following descriptive or declarative manner:
- Get StoreName and StorePhone for book stores such that there exists a title BK with the same BookstoreID value and with a BookTitle value of Some Sample Book.
Equivalence with relational algebra
[edit]This section needs expansion. You can help by adding missing information. (November 2021) |
The relational algebra and the domain-independent relational calculus are logically equivalent: for any algebraic expression, there is an equivalent expression in the calculus, and vice versa. This result is known as Codd's theorem.
Let the relations be:
- Bookstores () with attributes (BookstoreID, StoreName, StorePhone, …), and
- Titles () with attributes (BookstoreID, BookTitle, …).
Relational algebra expression
[edit]The query can be written as:
A more explicit equivalent form is:
The second form is often preferred because it pushes the selection down before the join.
Relational calculus expression
[edit]In tuple relational calculus, the same query is:
This says: return the store name and phone number of every bookstore for which there exists a title tuple with matching BookstoreID and the book title equal to "Some Sample Book".
Mathematical properties that prove the equivalence
[edit]The proof rests on the standard equivalence between join, selection, projection, and existential quantification.
Property A: Join + selection corresponds to conjunction with equality
[edit]
means exactly the set of pairs (b, t) such that:
Then applying selection:
adds:
So the algebraic expression becomes the set of all joined pairs satisfying:
Property B: Projection corresponds to existential quantification
[edit]Projection over (StoreName,StorePhone) removes the title tuple and any other unneeded attributes. In logic, removing attributes corresponds to existentially quantifying them away.
So the projected algebra result corresponds to:
which is exactly the calculus expression.
Property C: Selection pushdown is valid
[edit]Because selection only references , we can rewrite:
This is the relational algebra selection-pushdown law. It preserves meaning and often improves efficiency.
Final equivalence statement
[edit]Thus the relational algebra query and the relational calculus query are equivalent because:
denotes the same set as
Purpose
[edit]The raison d'être of the relational calculus is the formalization of query optimization. Query optimization consists in determining from a query the most efficient manner (or manners) to execute it. Query optimization can be formalized as translating a relational calculus expression delivering an answer A into efficient relational algebraic expressions delivering the same answer A.
See also
[edit]References
[edit]- Date, Christopher J. (2004). An Introduction to Database Systems (8th ed.). Addison Wesley. ISBN 0-321-19784-4.