Introduction
Tuple relational calculus (TRC) is a declarative query language: a query states the property that answer tuples must satisfy and says nothing about how to compute them. A TRC query has the form , "the set of tuples for which the formula is true", where is a formula of first-order logic over the database.
Relational algebra and safe TRC have exactly the same expressive power (Codd's theorem), so every algebra expression has a calculus equivalent and the other way round. The two basic tasks are to say what a given TRC expression returns, or which of several expressions expresses an English query. The points that need most care are the placement of quantifiers, writing where is needed under , and safety.
Prerequisites
- Relational Algebra: the operators whose meaning TRC must reproduce, especially division.
- First-Order Logic: quantifiers, free and bound variables, negating nested quantifiers.
Core concepts
Tuple variables range over tuples. A formula is built from atoms:
| Atom | Meaning |
|---|---|
| is a tuple of relation | |
| comparison of two attribute values, | |
| comparison with a constant |
Formulas combine atoms with , , , and the quantifiers and . The bounded forms abbreviate and .
Free and bound variables. A variable is bound inside the scope of a quantifier on it and free otherwise. In , must be the only free variable of .
Equivalences used constantly.
- and .
- .
- distributes over ; distributes over ; neither distributes over the other connective.
Safety
The formula describes every tuple that is not in , over all possible values, an infinite set. Here safe requires domain independence: for every database instance, evaluating the formula over any ambient domain containing its active domain gives the same answer, whose values lie in that active domain. The domain of the formula, , comprises values in its mentioned relations plus its constants. An output-value bound alone is insufficient: unbounded quantifiers can change truth when the ambient domain grows. Syntactically range-restricted formulas bound output attributes to relation values/constants and bound quantified variables correctly, providing a sufficient safe fragment. Only safe expressions are allowed; safe TRC is exactly as expressive as relational algebra.
How it works: reading an expression
Read a TRC expression from the outside in:
- The target tuple and what it must belong to (its schema comes from the attributes used on it).
- Each is "there is some row such that"; each is "for every row of ".
- Translate the inner comparison last.
Translation table.
| Relational algebra | Tuple relational calculus |
|---|---|
| projected on | |
Algorithms and pseudocode
For the syntactically range-restricted fragment taught here, with all quantified variables bounded to relations and output attributes range-bound, evaluate by brute force over the domain of the formula. Do not apply this algorithm to arbitrary formulas merely because their current output lies in the active domain:
EVAL-TRC({t | P(t)})
D <- dom(P) // values in mentioned relations plus constants
answer <- empty set
for each tuple t over D with the target attributes
if P(t) is true, evaluating
exists s in R (Q) as: some row s of R makes Q true
forall s in R (Q) as: every row s of R makes Q true
then add t to answer
return answerFor this bounded fragment, time is exponential in the nesting depth of quantifiers (each quantifier loops over a relation), polynomial in the data for a fixed query.
Mathematical explanation
Why needs . An unbounded asserts that every tuple whatever is in , which is false as soon as the domain has a tuple outside . The intended "every of satisfies " is , which is true for tuples outside vacuously. Dually, is almost always true (any outside satisfies it), so pairs with .
Why safety matters. over the domain of alone is the complement of within that domain; over any larger domain the answer grows. An expression whose answer depends on the domain chosen is not a well-defined query, and domain-independent safety rules it out. Even an active-domain-bounded output can be unsafe: for unary R={1}, the query {t in R | forall s (s in R)} returns {1} over domain {1} but nothing over {1,2}; the unbounded quantifier is the problem.
Why does not distribute over . needs one witness for both; allows two different witnesses. The second is weaker.
Worked examples
All examples use Enrolled(sid, cid) = and Req(cid) = from Relational Algebra.
Example 1: selection and projection. returns the students enrolled in : .
Example 2: division. returns the students enrolled in every required course: , the same as .
Example 3: the trap. Replace the bounded by with ranging over the course values of the domain, . The tuple with is not in Req, so the body is false for it and the answer is empty.
Example 4: negation. returns students not enrolled in : .
Example 5: an unsafe query. . Use the same global untyped active domain as EVAL-TRC and Example 7: its 4 student and 3 course values give , hence two-field tuples, of which 10 belong to Enrolled. The active-domain complement contains 39 tuples, including ill-typed pairs; it is not just the two missing student-course enrolments. Adding one new global value gives additional pairs. Over an unrestricted domain, the complement can be infinite: this illustrates why the query is unsafe.
Example 6: maximum. For , returns . Changing to returns the empty set, because every has with .
Example 7: division by double negation, and the empty divisor. Rewrite as :
Read it from the inside: "there is no required course for which there is no enrolment of in ".
- With
Req: fails (no enrolment in ); the answer is , the same as Example 2 and the algebra division. - With
Reqempty: is false for every , so the body is true and every enrolled student qualifies: . This matches . - Drop the range condition and keep
Reqempty: now the body is true for every value of the domain, including the course names, so the answer is all 7 values . The expression is unsafe; the range condition is what keeps it safe. WithReqthe same expression happens to return , which hides the problem.
The SQL form is the double NOT EXISTS of SQL Subqueries and Views, and it gives the same two answers on these tables.
Important properties and observations
- Codd's theorem. Safe TRC, safe domain relational calculus and relational algebra (the six basic operators) are equally expressive.
- TRC has set semantics like the algebra; SQL's duplicates and aggregates are outside it.
- Every query needs a "range" for . Without a conjunct such as or , is unrestricted and the query is usually unsafe.
- Negation turns quantifiers. : "not every required course" means "some required course missing".
- Strict versus non-strict comparisons change answers at the boundary, as in Example 6.
Related material. Domain relational calculus uses variables over attribute values instead of tuples, for example ; its safety condition and expressive power match TRC.
Common misconceptions.
- " means every in satisfies ." It means every tuple is in and satisfies , almost always false.
- "Any first-order formula is a valid query." Only safe ones.
- " and are the same." They are not: "no row satisfies" versus "some row fails".
GATE examination perspective
- What does the expression return? Evaluate on the given instance, quantifier by quantifier.
- Which expression means "for all"? Look for paired with (or a bounded ), or the double negation .
- Equivalence to algebra. Match a TRC expression with the algebra expression (division, difference, join and projection).
- Safety. Identify the unsafe expression among options: a negated membership with no positive range for .
Summary
Sources
- A. Silberschatz, H. F. Korth, S. Sudarshan, Database System Concepts, 7th ed., McGraw-Hill, 2019 (2020 copyright), Chapter 27 Formal Relational Query Languages (online chapter).
- R. Elmasri, S. B. Navathe, Fundamentals of Database Systems, 7th ed., Pearson, 8.6 The Tuple Relational Calculus; 8.7 The Domain Relational Calculus.