Sub topic 11
Safe expressions
This chapter assumes you can read tuple relational calculus (TRC) expressions of the form { t | P(t) }. It also assumes you know the quantifiers ∃ and ∀, and that you know the relational algebra operators from earlier chapters. The next chapter uses safety to compare calculus with algebra.
Why safety matters
A TRC expression { t | P(t) } means "all tuples t for which the predicate P is true". The definition says nothing about where t comes from. If P is true for infinitely many tuples, the answer is infinite. A database system cannot compute or print an infinite relation.
An expression is unsafe if it can return an infinite result for some database. It is safe if, on every database, it returns a finite result built only from values that the database or the expression itself mentions.
Unsafe expressions
Three common patterns produce unsafe expressions. Take the schema Student(sid, sname, age):
1. { t | ¬(t ∈ Student) }
2. { t | t.age > 20 }
3. { t | t ∈ Student ∨ t.age = 5 }- Expression 1 returns every possible tuple that is not in Student. There are infinitely many such tuples.
- Expression 2 never says that t comes from a relation. Any tuple with an age above 20 qualifies, including tuples with arbitrary other attributes.
- Expression 3 has a disjunction. One branch restricts t to Student, but the other branch does not restrict t at all.
In each case the predicate says what t must satisfy but does not tie t to a finite source.
The domain of an expression
The formal definition uses the domain of an expression P, written dom(P). It is the set of all values that either appear as constants in P or appear in any tuple of any relation that P mentions.
For example, in { t | t ∈ Student ∧ t.age > 20 }, dom is every value stored in Student plus the constant 20. That set is finite for any database.
An expression { t | P(t) } is safe if every tuple in its result consists only of values from dom(P). Safety is defined by this guarantee about the result. It does not require a particular syntax.
The following table applies the definition to the three unsafe expressions. They fail because their answers can contain values that lie outside dom(P).
| Expression | Why the result escapes dom(P) |
|---|---|
| `¬(t ∈ Student)` | Any value, from any domain, can appear in a tuple outside Student. |
| `t.age > 20` | Every integer above 20 qualifies, and no relation limits the other attributes. |
| `t ∈ Student ∨ t.age = 5` | The second branch accepts tuples with any other attribute values. |
A practical rule for writing safe expressions
Testing the dom(P) condition directly is not practical. In GATE-style questions you check a syntactic rule instead: every tuple variable must be range-restricted, meaning a positive membership atom such as t ∈ R binds it to a relation.
- Free variables must be bound by a positive atom
t ∈ Rthat is part of a conjunction. - A negation must appear only inside a conjunction with a positive atom that already binds its variables.
- A disjunction is safe only if both branches restrict the same free variables.
- Quantifiers must be range-restricted. Write
∃e ∈ Enrolled (...)and∀c ∈ Course (...), so the quantified variable also comes from a relation.
These rules are sufficient for safety. They are not strictly necessary, because some safe expressions do not follow them exactly. In practice, rewriting an expression into this form is how you show it is safe.
Making unsafe expressions safe
To repair expression 1, bind t to a relation first and then negate a condition:
Students who are not enrolled in any course:
{ t | t ∈ Student ∧ ¬∃e ∈ Enrolled (e.sid = t.sid) }Here t is bound by t ∈ Student, so every answer is a Student tuple. The negation filters that finite set and cannot enlarge it. In relational algebra this is a set difference:
Student − (Student ⋈ Enrolled projected onto Student's attributes)The same approach works for "for all" queries. A universal quantifier is safe when its variable ranges over a relation:
Students enrolled in every course:
{ t | t ∈ Student ∧ ∀c ∈ Course ∃e ∈ Enrolled (e.sid = t.sid ∧ e.cid = c.cid) }The quantifier ∀c ranges over Course, which is finite. Without the ∈ Course restriction, ∀c would range over all possible tuples, and the expression would be unsafe. A universal quantifier can be rewritten as a negated existential, because ∀c P(c) is equivalent to ¬∃c ¬P(c). That is why this query leads to the double NOT EXISTS form in SQL.
A checking procedure
Safety and the other query languages
Relational algebra and SQL have no counterpart to unsafe expressions. Every algebra operator takes finite relations and produces a finite relation. There is no operator that returns "everything not in R". Complement exists only as a difference against another finite relation, such as Student − Enrolled_students.
This is why safety is the condition that connects TRC to relational algebra. Unrestricted calculus is more powerful than algebra, but only because it can express infinite queries. The safe subset matches algebra in power, and the later chapter on expressive power builds on that fact.
Common errors
- Treating a negation as a source of tuples.
¬(t ∈ R)selects nothing finite. It needs a positive binding beside it. - Using a bare comparison to bind a variable.
t.age > 20constrains t but does not bind it. - Forgetting that OR needs both branches bound. One restricted branch is not enough.
- Leaving a quantifier unrestricted. Write
∀c ∈ Course, not just∀c. - Confusing safe with correct. A safe expression is finite but may still compute the wrong query.
Exercise
Which of these TRC expressions are safe, using the schema Student(sid, sname, age) and Enrolled(sid, cid)? (a) { t | t ∈ Student ∧ ¬∃e ∈ Enrolled (e.sid = t.sid) }, (b) { t | ¬∃e ∈ Enrolled (e.sid = t.sid) }, (c) { t | t ∈ Student ∨ t ∈ Enrolled }. For each unsafe one, give the reason.
Show answer
(a) is safe. t is bound by the positive atom t ∈ Student, and the negated existential only removes tuples from that finite set. (b) is unsafe. No positive atom binds t, so every possible tuple that has no matching Enrolled row qualifies, and there are infinitely many. (c) is safe in the sense that both branches restrict t to a finite relation, so the result is finite. It is not a meaningful query in practice, because Student and Enrolled have different attributes and are not union-compatible.