Sub topic 10
Tuple relational calculus
This chapter assumes you know basic set operations, first-order logic (AND, OR, NOT, for all, there exists), and the relational algebra operators selection, projection, product and difference from earlier chapters. Tuple relational calculus (TRC) is a declarative query language. You describe the tuples you want, and you do not say how to compute them.
From procedures to descriptions
Relational algebra is procedural: an expression says which operators to apply and in what order. TRC is declarative: an expression states a condition, and the answer is every tuple that satisfies it.
A TRC query has the form:
{ t | P(t) }Read it as "the set of all tuples t such that the formula P is true for t". The variable t is a tuple variable. It stands for one row of some relation, and it is the only free variable in the formula.
We use the schema Student(sid, sname, age), Course(cid, cname), Enrolled(sid, cid) throughout.
Building blocks
Tuple variables and range
A tuple variable takes its values from a relation. Two notations for this are common:
Student(s)says thatsis a tuple of the relation Student.s ∈ Studentsays the same thing.
Attributes are accessed with dot notation: s.age, s.sname.
Atoms
An atom is the smallest formula. There are three kinds:
s ∈ R, a membership test.s.A θ u.B, a comparison of two attributes, where θ is one of=, ≠, <, ≤, >, ≥.s.A θ c, a comparison of an attribute with a constant.
Combining formulas
Formulas are built from atoms with ∧ (and), ∨ (or), ¬ (not), and the implication P ⇒ Q, which is the same as ¬P ∨ Q. Quantifiers bind a tuple variable:
∃s ∈ R (P): at least one tuple of R makes P true.∀s ∈ R (P): every tuple of R makes P true.
A variable bound by a quantifier is a bound variable. A variable not bound by one is free. The free variable in { t | P(t) } is the one whose values form the answer.
Selection and projection in TRC
The query "names of students older than 20" is:
{ t | ∃s ∈ Student (s.age > 20 ∧ t.sname = s.sname) }Here t is a new tuple with the single attribute sname. The existential quantifier finds a witness s in Student, and the equation t.sname = s.sname copies the attribute. This one formula does both selection (s.age > 20) and projection (only sname appears in t).
If you want whole Student rows instead, the shorter form is enough:
{ s | s ∈ Student ∧ s.age > 20 }Joins and set operations in TRC
A join becomes an existential quantifier over each relation, plus equality conditions between them. To find the names of students enrolled in course 10:
{ t | ∃s ∈ Student ∃e ∈ Enrolled
(s.sid = e.sid ∧ e.cid = 10 ∧ t.sname = s.sname) }The set operations map to logical connectives, assuming the two relations R and S are union compatible:
| Algebra | Calculus | Column |
|---|---|---|
| `R ∪ S` | `{ t | t ∈ R ∨ t ∈ S }` |
| `R ∩ S` | `{ t | t ∈ R ∧ t ∈ S }` |
| `R − S` | `{ t | t ∈ R ∧ ¬(t ∈ S) }` |
Difference is where negation first appears. The same pattern gives "students not enrolled in anything":
{ s | s ∈ Student ∧ ¬∃e ∈ Enrolled (e.sid = s.sid) }Universal quantification
The universal quantifier is what lets TRC state "for all" queries directly. Students enrolled in every course:
{ s | s ∈ Student ∧ ∀c ∈ Course ∃e ∈ Enrolled (e.sid = s.sid ∧ e.cid = c.cid) }Logic gives the equivalence ∀x P ≡ ¬∃x ¬P. Substituting it above gives:
{ s | s ∈ Student ∧ ¬∃c ∈ Course ¬∃e ∈ Enrolled (e.sid = s.sid ∧ e.cid = c.cid) }This is the form that maps directly to the double NOT EXISTS pattern in SQL, which a later chapter covers.
Safe expressions
Not every formula describes a sensible query. Consider:
{ t | ¬(t ∈ Student) }The answer is every possible tuple that is not in Student. That set is infinite, because the attribute domains (such as the integers) are unbounded. An expression whose answer can be infinite, or depends on values outside the database, is unsafe.
A safe expression always produces a finite answer, and every value in the answer comes from the database or the query itself. In practice, a formula is safe when each free and quantified variable is tied to a relation by a positive membership atom. Negation, a bare ∨, and ∀ must not be the only thing that limits a variable.
The next chapters build on this idea. For now remember the rule used in exams: if a variable is not restricted to a relation by ∈ in a non-negated way, suspect an unsafe expression.
Reading TRC in exam questions
GATE questions often show a TRC expression and ask which English description or which algebra expression matches. A reliable method has four steps.
- Identify the free variable and the relation it ranges over.
- List every bound variable and its relation.
- Read the connectives.
∧between a variable and a condition is a selection.∃with equalities is a join or projection.¬∃is a difference or anti-join. - Check for a quantifier trap.
∀s (s ∈ R ⇒ ...)is correct, but∀s (s ∈ R ∧ ...)demands that every possible tuple belongs to R, which is almost always false.
Translating to relational algebra and SQL
The three forms of the first query line up like this:
| Formalism | Query | Column |
|---|---|---|
| Relational algebra | `π_sname (σ_age > 20 (Student))` | |
| Tuple calculus | `{ t | ∃s ∈ Student (s.age > 20 ∧ t.sname = s.sname) }` |
| SQL | `SELECT DISTINCT sname FROM Student WHERE age > 20;` |
The pattern is consistent. Each ∃s ∈ R becomes a relation in FROM, conditions on s become WHERE predicates, and the attributes copied into t become the SELECT list. TRC works on sets, so SQL needs DISTINCT to match it.
Exercises
Exercise
Write a TRC expression for the sids of students who are enrolled in at least one course, and then one for the sids of students who are enrolled in no course.
Show answer
Enrolled in at least one: { t | ∃e ∈ Enrolled (t.sid = e.sid) }. Enrolled in none: { s | s ∈ Student ∧ ¬∃e ∈ Enrolled (e.sid = s.sid) }. The second keeps s tied to Student by a positive membership atom, so it is safe.
Exercise
Is { t | t ∉ Course } safe? Give a reason.
Show answer
No. The answer is every tuple not in Course. The attribute domains are unbounded, so the result is infinite. The variable t is never restricted to a relation by a positive membership atom.