Sub topic 12
Domain Relational Calculus
Tuple relational calculus (TRC) uses variables that stand for whole rows. Domain relational calculus (DRC) uses variables that stand for single column values. This chapter assumes you know TRC, the ∃ and ∀ quantifiers, and the schema Student(sid, sname, age), Course(cid, cname), Enrolled(sid, cid).
The core idea
A DRC query has this form:
{ <x1, x2, ..., xn> | P(x1, x2, ..., xn) }Each xi is a domain variable. It holds one value, such as an integer or a string. The formula P decides which combinations of values belong in the answer.
A DRC formula is built from these pieces:
- Relation atom:
<x1, ..., xk> ∈ Ris true when the row with those values is in relationR. The values appear in column order, so the atom must list one variable or constant per column. - Comparison atom:
x θ yorx θ c, where θ is one of=, ≠, <, ≤, >, ≥andcis a constant. - Connectives:
∧,∨,¬, and→. - Quantifiers:
∃x (...)and∀x (...), which range over domain values.
TRC says "there is a student row s with s.age > 20". DRC says "there are values s, n, a such that the row (s, n, a) is in Student and a > 20". DRC states the column positions explicitly, so it needs more variables. It also needs no dot notation.
Worked queries
Use this sample data.
| Relation | Rows |
|---|---|
| Student | (1, Ana, 19), (2, Ben, 22), (3, Cy, 25) |
| Course | (10, DB), (20, OS) |
| Enrolled | (2, 10), (2, 20), (3, 10) |
Q1: names of students older than 20
{ <n> | ∃s ∃a ( <s, n, a> ∈ Student ∧ a > 20 ) }
Result: Ben, Cy
Q2: ids of students not enrolled in any course
{ <s> | ∃n ∃a ( <s, n, a> ∈ Student ) ∧ ¬∃c ( <s, c> ∈ Enrolled ) }
Result: 1
Q3: ids of students enrolled in every course
{ <s> | ∃n ∃a ( <s, n, a> ∈ Student )
∧ ∀c ( (∃cn <c, cn> ∈ Course) → <s, c> ∈ Enrolled ) }
Result: 2Q2 and Q3 start with the same positive atom. It binds s to an actual student id, and the rest of the formula only filters those values. Q3 is the DRC form of division. The ∀c (... → ...) reads "for every course c, the student is enrolled in c", which matches the double NOT EXISTS pattern in SQL.
How to build a DRC query
- List the output variables. Each one will be a column of the answer.
- For each relation you need, write an atom with one variable per column. Reuse a variable wherever two columns must be equal. That is how a join is written:
sappears in both<s, n, a> ∈ Studentand<s, c> ∈ Enrolled. - Quantify every variable that is not an output variable. Use
∃for ordinary joins and∀or¬∃for "every" and "none". - Add the comparison conditions.
Safety in DRC
A DRC expression is safe if its answer depends only on the values stored in the database and the constants in the query. The set of those values is the active domain. An unsafe expression can describe an infinite answer.
Unsafe: { <s, c> | ¬( <s, c> ∈ Enrolled ) }Every integer pair that is not an enrollment satisfies this formula. The result is infinite, so no database system can evaluate it.
A formula ∃x (x > 5) has the same problem. Its truth depends on which values the domain contains, not on the stored data. The usual fix is to tie every variable to a positive relation atom, as Q2 and Q3 do. A formula with only a negated atom for a variable is a warning sign.
The safety rules mirror TRC. Safe DRC, safe TRC and relational algebra all have the same expressive power. Any query in one can be written in the others.
DRC compared with TRC
| Aspect | TRC | DRC |
|---|---|---|
| Variable ranges over | Whole tuples of a relation | Single attribute values |
| Column access | `s.age` | A variable in the right position of an atom |
| Relation membership | `s ∈ Student` | `<s, n, a> ∈ Student` |
| Join | Equality of attributes, `s.sid = e.sid` | A shared variable |
| Typical size | Shorter | Longer, with more variables |
DRC is the basis of Query-by-Example (QBE), where you fill in a table skeleton with variables and constants. GATE questions about DRC are usually short, such as "what does this expression compute" or "is it safe".
Common errors
- Wrong arity.
<s, n> ∈ Studentis invalid because Student has three columns. Write<s, n, a>. - Free variable left unbound. If
aappears in a formula but is neither in the output nor quantified, the expression is malformed. - Misplaced negation.
¬∃c (<s, c> ∈ Enrolled)means "no enrollment".∃c ¬(<s, c> ∈ Enrolled)means "some course c where the student is not enrolled", and almost any student satisfies it. Mixing these up breaks queries. - Missing guard on ∀.
∀c (<s, c> ∈ Enrolled)ranges over all values of c, not only courses. It is false for every s. Guard it with(∃cn <c, cn> ∈ Course) →. - Implication versus conjunction. After
∀, use→. After∃, use∧. Writing∀c (Course(c) ∧ ...)demands that every domain value be a course.
Equivalent SQL for Q2
SELECT s.sid
FROM Student s
WHERE NOT EXISTS (SELECT 1 FROM Enrolled e WHERE e.sid = s.sid);The positive atom <s, n, a> ∈ Student becomes the FROM clause. The ¬∃c becomes NOT EXISTS. The quantified variable c disappears because SQL checks the row as a whole.
Exercise
Write a DRC expression for the names of students enrolled in at least one course.
Show answer
{ <n> | ∃s ∃a ∃c ( <s, n, a> ∈ Student ∧ <s, c> ∈ Enrolled ) }. The variable s is shared by both atoms, which performs the join.
Exercise
Is { <s, c> | ¬( <s, c> ∈ Enrolled ) } a safe expression? Explain.
Show answer
No. The formula holds for every pair (s, c) that is not an enrollment, including values that appear nowhere in the database, so the answer is infinite. It is not tied to any positive relation atom. Adding the atoms <s, n, a> ∈ Student and <c, cn> ∈ Course (with s, c as output variables and n, a, cn existentially quantified) makes it safe.