← 🔣 Relational Algebra & Calculus
Subtopic 4 of 4

2.4Tuple and domain relational calculus

The idea

Relational calculus says what you want, not how to get it. Tuple calculus uses tuple variables; domain calculus uses attribute variables.

Think of it like this: Ordering from a menu ("a pizza with no mushrooms") versus giving the chef a recipe.

Why it exists

You must read a calculus expression and match it to an algebra query, and know that unsafe expressions are not allowed.

How it works

  1. { t | P(t) } returns all tuples t that make P true.
  2. ∃ means "there is a tuple such that"; ∀ means "for every tuple".
  3. "For all" queries use ∀ or a double negation (division).
  4. An expression is unsafe if its result can be infinite, e.g. { t | ¬R(t) }.

Worked example: Which operator expresses { t | R(t) ∧ ¬S(t) }?

R − S.

Now play with it

  • →Read the table of equivalences row by row.
  • →Think of why { t | ¬R(t) } has no finite answer.
OperationRelational algebraTuple relational calculusDomain relational calculus
Selectionσ_A=5(R){ t | R(t) ∧ t.A = 5 }{ <a, b> | R(a, b) ∧ a = 5 }
Projectionπ_A(R){ t | ∃r (R(r) ∧ t.A = r.A) }{ <a> | ∃b R(a, b) }
UnionR ∪ S{ t | R(t) ∨ S(t) }{ <a, b> | R(a, b) ∨ S(a, b) }
DifferenceR − S{ t | R(t) ∧ ¬S(t) }{ <a, b> | R(a, b) ∧ ¬S(a, b) }
Division R ÷ Sπ_A(R) − π_A((π_A(R) × S) − R){ t | ∀s ∈ S ∃r ∈ R (r.A = t.A ∧ r.B = s.B) }"for all" = ∀ over the divisor

Unsafe: { t | ¬R(t) } returns every tuple not in R, an infinite set. Safe calculus is exactly as powerful as relational algebra.

Check yourself

Safe tuple relational calculus and relational algebra

Remember: Safe calculus = algebra. Unsafe expressions give infinite results.
Exam notes (the dense version)

Calculus is declarative: {t | P(t)} (tuple calculus) or {<x1, …, xk> | P} (domain calculus), with ∃ and ∀. Algebra and safe calculus have the same expressive power (Codd). An expression is unsafe if it can produce an infinite result, such as {t | ¬(t ∈ R)}. SQL is mostly based on tuple calculus.

Previous year questions: Relational Algebra & Calculus (12)

PYQs are grouped by topic, so this is the full set for Relational Algebra & Calculus; try the ones that match this subtopic. Open all 12 on the topic page.