Sub topic 10
Safe Expressions and Domain Relational Calculus
This chapter assumes that you can read relational algebra, use basic first-order logic, and write simple tuple relational calculus (TRC) expressions.
Safe expressions
A calculus expression is safe when its result is guaranteed to be finite and to use only values found in the database. This property matters because database relations are finite, but the underlying domains may be infinite. For example, the domain of integers has infinitely many values.
Consider the schema:
Student(sid, sname, age)
A safe TRC query for names of students older than 20 is:
{ t.sname | Student(t) ∧ t.age > 20 }The tuple variable t is restricted by Student(t). Therefore, only stored student tuples can produce results. The output names come from the active domain, which is the set of values appearing in the database.
An unsafe expression is:
{ t | ¬Student(t) }This asks for every tuple that is not a student tuple. If the tuple domain is unlimited, infinitely many tuples may satisfy the condition. The database system cannot materialize this result as a finite relation.
Common safety rules are:
- Every result value must be connected to a value in a database relation.
- Every free tuple variable must be restricted by a relation atom such as
Student(t). - Negated conditions must be bounded by related positive conditions.
- Comparisons must involve values whose possible values are limited by the database.
A safe expression can still return an empty relation. Safety only guarantees that the result is finite and database-bounded.
Domain relational calculus
Domain relational calculus (DRC) uses variables for individual attribute values rather than whole tuples. A DRC query has the general form:
{ <x1, x2, ..., xn> | formula }The variables x1 through xn are free variables and form the result. Variables introduced with ∃ or ∀ are bound variables.
For example, the DRC version of “names of students older than 20” is:
{ <name> |
∃sid ∃age (
Student(sid, name, age) ∧ age > 20
)
}The relation atom Student(sid, name, age) binds all three domain variables to values from stored student tuples. The variable name is free, so it appears in the result. The variables sid and age are existentially quantified because they are needed only to test the condition.
TRC and DRC express the same safe database queries. They differ in what their variables represent:
| Formalism | Variable represents | Example |
|---|---|---|
| TRC | A complete tuple | `Student(t)` |
| DRC | One attribute value | `Student(sid, name, age)` |
Exercise
Is { <x> | x > 0 } a safe DRC expression if the database contains no integer relation?
Show answer
No. The expression can range over infinitely many positive integers, and x is not restricted by a database relation.