Sub topic 9
Tuple Relational Calculus
This chapter assumes basic set theory, first-order logic, and the idea that a database table contains rows and columns. Tuple relational calculus (TRC) describes which tuples a query should return. It states conditions that result tuples must satisfy rather than listing the steps used to compute them.
Core notation
A tuple variable represents one row of a relation. If s represents a row of Student, then s.sid, s.sname, and s.age refer to its attributes.
A TRC query commonly has this form:
\[ \{t \mid P(t)\} \]
Here, t is the result tuple and P(t) is a logical formula that must be true.
| Notation | Meaning |
|---|---|
| `s ∈ Student` | `s` is a tuple from `Student` |
| `s.age > 20` | The age attribute of `s` is greater than 20 |
| `P ∧ Q` | Both `P` and `Q` are true |
| `∃s` | There exists a tuple `s` |
| `∀c` | Every tuple `c` satisfies a condition |
For example, the names of students older than 20 can be written as:
{ s.sname | s ∈ Student ∧ s.age > 20 }The variable s is restricted to tuples in Student. The condition selects older students, and s.sname specifies the attribute returned.
Existential and universal quantifiers
The existential quantifier, ∃, expresses “there is at least one.” To find students enrolled in a course named DBMS:
{ s.sname |
s ∈ Student ∧
∃e ∈ Enrolled ∃c ∈ Course
(s.sid = e.sid ∧ e.cid = c.cid ∧ c.cname = 'DBMS')
}The variable e connects a student to a course through Enrolled.
The universal quantifier, ∀, expresses “for every.” To find students enrolled in every course:
{ s.sname |
s ∈ Student ∧
∀c ∈ Course
(∃e ∈ Enrolled (e.sid = s.sid ∧ e.cid = c.cid))
}This says that for each course c, an enrollment tuple e must exist for the current student.
Free and bound variables
A variable is bound when a quantifier controls it. In the previous query, c and e are bound by ∀c and ∃e. The outer variable s is free because it identifies the tuple being tested.
A query should also be safe: its result must be limited to values drawn from the database or fixed constants. An expression that can generate arbitrary values, such as all integers not appearing in a table, is unsafe and does not describe a finite database result reliably.
Exercise
Write a TRC query that returns the names of students enrolled in at least one course.
Show answer
{ s.sname | s ∈ Student ∧ ∃e ∈ Enrolled (e.sid = s.sid) }