Sub topic 13
Relational algebra and safe calculus: equal expressive power
This chapter assumes you know the basic relational algebra operators (selection, projection, set operations, product, join, division) and the syntax of tuple relational calculus (TRC), including the quantifiers ∃ and ∀. It uses the schema Student(sid, sname, age), Course(cid, cname), Enrolled(sid, cid).
What "equal expressive power" means
Two query languages have equal expressive power if every query written in one can be rewritten in the other, and both give the same result on every database. Codd's theorem states this for relational algebra and safe tuple relational calculus.
The claim has a qualifier. Plain calculus is more powerful than algebra, because it can describe queries with infinite answers. Algebra cannot do that. Safety removes those queries, and the remaining ones match algebra exactly.
The same equivalence holds for safe domain relational calculus. That is why GATE questions can ask you to move between the three notations.
From algebra to calculus
Every algebra operator has a calculus formula. Here R and S are relations, and where an operator needs it they are union-compatible.
| Algebra | Tuple calculus | Column |
|---|---|---|
| σ_age>20(Student) | { t \ | t ∈ Student ∧ t.age > 20 } |
| π_sname(Student) | { t \ | ∃s ∈ Student (t.sname = s.sname) } |
| R ∪ S | { t \ | t ∈ R ∨ t ∈ S } |
| R − S | { t \ | t ∈ R ∧ ¬(t ∈ S) } |
| R ∩ S | { t \ | t ∈ R ∧ t ∈ S } |
| R × S | { t \ | ∃r ∈ R ∃s ∈ S (t is r followed by s) } |
Selection, projection, union, difference and product are the basic operators. Intersection, joins and division are built from them, so they need no separate proof. Because each operator has a formula, any algebra expression translates into calculus by induction on its structure.
Here is a worked translation. The query is: students enrolled in no course.
Algebra:
π_sid(Student) − π_sid(Enrolled)
Tuple calculus:
{ t | ∃s ∈ Student ( t.sid = s.sid ∧ ¬∃e ∈ Enrolled ( e.sid = s.sid ) ) }The algebra uses set difference. The calculus uses a negated ∃.
From calculus to algebra: why safety is needed
The reverse direction fails for unrestricted calculus. Consider these expressions:
{ t | ¬(t ∈ Student) }
{ t | t.age > 20 }
{ t | ∀x (x.age > 20) }The first returns every tuple that is not in Student. That is infinitely many tuples, and no algebra expression can produce an infinite relation.
The second does not say which relation t comes from, so it also gives an infinite result.
An expression is safe if it returns only values that come from the database or from constants in the query. The usual way to state this is that every output value must lie in the active domain. The active domain is the set of all values that appear in the relations, plus the constants written in the query.
A safe expression has a finite result on every database, and it always depends on the database contents.
The idea behind the proof
The proof works with the active domain. Algebra can compute it directly by projecting every column of every relation and taking the union.
Dom = π_sid(Student) ∪ π_age(Student) ∪ π_cid(Course) ∪ π_sid(Enrolled) ∪ ...The columns have different types, so in practice you build one such relation per domain. Once you have it, each calculus construct maps to an algebra operator:
- A free variable ranging over a relation becomes that relation.
- ∧ becomes join or intersection.
- ∨ becomes union.
- ¬ becomes difference from a product of active-domain relations.
- ∃ becomes projection.
- ∀ is rewritten as ¬∃¬, so it becomes a difference or division.
Safety guarantees that the result never needs values outside the active domain. So the translation produces a finite algebra expression that returns exactly the calculus answer.
Edge cases and common errors
Universal quantifiers still need a range. The query "students enrolled in every course" is safe only if t is tied to a relation:
{ t | t ∈ Student ∧ ∀c ∈ Course ∃e ∈ Enrolled ( e.sid = t.sid ∧ e.cid = c.cid ) }Without t ∈ Student the expression is unsafe. In algebra this query is the division from the earlier chapter, and ∀ turns into division or a double difference.
Empty Course. If Course is empty, the ∀ condition holds for every t, so every student is returned. Algebra division behaves the same way when the divisor is empty, as long as the dividend's attributes are taken from a relation like Student. This is a common trap in exam questions.
Duplicates. Algebra and calculus use set semantics, so neither has duplicates. SQL uses bags. When translating to SQL, add DISTINCT to keep the same result.
Extra features go beyond both languages. Neither basic algebra nor safe calculus can express aggregation such as COUNT, or transitive closure such as "all ancestors". Extended algebra adds grouping and aggregation, and SQL adds recursive queries. These are strictly more powerful than the equivalence class in this chapter. Do not claim "algebra equals SQL".
Safe is a syntactic or semantic condition. Deciding whether an arbitrary calculus expression is safe is undecidable. Textbooks therefore use a restricted syntactic form, such as requiring every variable to be bound to a relation, which guarantees safety.
Decision criteria for exam questions
- If a statement says "every safe calculus query has an algebra equivalent", it is true.
- If it says "every calculus query has an algebra equivalent", it is false.
- If it says algebra can express transitive closure, it is false.
- To test whether an expression is safe, ask whether a value in the result could come from nowhere. A bare negation or a bare comparison on a free variable is unsafe.
Exercise
Is { t | ¬(t ∈ Enrolled) } safe? Give the reason.
Show answer
No. It returns every tuple that is not in Enrolled. This set is infinite, so no relational algebra expression can produce it.
Exercise
Write a safe tuple calculus expression, and an algebra expression, for the sids of students aged over 20 who are enrolled in no course.
Show answer
Calculus: { t | ∃s ∈ Student ( t.sid = s.sid ∧ s.age > 20 ∧ ¬∃e ∈ Enrolled ( e.sid = s.sid ) ) }. Algebra: πsid(σage>20(Student)) − π_sid(Enrolled).