Tuple Relational Calculus (TRC)
Tuple Relational Calculus (TRC)
1. What is Relational Calculus?
Relational calculus is a formal, declarative query language for the relational model.
-
It specifies what data is required, not how to retrieve it
-
There is no sequence of operations
-
It is based on mathematical logic (predicate calculus)
Key Contrast with Relational Algebra
| Relational Algebra | Relational Calculus |
|---|---|
| Procedural | Non-procedural |
| Specifies how to get data | Specifies what data is needed |
| Uses operators (σ, π, ⨝, etc.) | Uses logical formulas |
| Execution order matters | Execution order does not matter |
Both have equal expressive power, leading to the idea of relational completeness.
2. Why Tuple Relational Calculus is Important
-
Strong mathematical foundation in predicate logic
-
Foundation of SQL (especially SELECT–FROM–WHERE)
-
Provides a formal basis for query correctness
-
Helps understand quantifiers, joins, and subqueries
3. Basic Form of a TRC Expression
A tuple relational calculus query has the form:
Where:
-
tis a tuple variable -
COND(t)is a Boolean condition -
Result = all tuples
tthat satisfyCOND(t)
4. Tuple Variables and Range Relations
Tuple Variables
-
Represent individual tuples
-
Range over relations using predicates like
EMPLOYEE(t)
Example
Query: Find employees with salary > 50,000
-
EMPLOYEE(t)→ restrictstto EMPLOYEE relation -
t.Salary > 50000→ selection condition
5. Selecting Specific Attributes (Projection)
To retrieve only certain attributes:
Equivalent to:
6. General Form of TRC Expression
A TRC query consists of:
-
Tuple variables
-
Range relations
-
Selection conditions
-
Join conditions
-
Attributes to retrieve
7. Atoms and Formulas
Types of Atoms
-
Relation atom
Means t is a tuple of relation R
-
Tuple-to-tuple comparison
-
Tuple-to-constant comparison
Building Formulas
Formulas are built using:
-
AND
-
OR
-
NOT
Example:
8. Free and Bound Variables
-
Free variables → appear in the output
-
Bound variables → used only for conditions
Example:
-
t→ free -
d→ bound by ∃
Only free variables appear in the result.
9. Existential Quantifier (∃)
Meaning
“There exists at least one tuple such that…”
Example
Employees who work in Research department
This corresponds to a join + selection.
10. Universal Quantifier (∀)
Meaning
“For every tuple…”
Example (Conceptual)
Employees who work on all projects of department 5:
Universal quantifiers are used to express ALL conditions, such as:
-
Works on all projects
-
Has all required skills
-
Satisfies every condition
11. Using Universal Quantifiers Safely
Universal quantifiers apply to all tuples in the universe, so we must:
-
Exclude irrelevant tuples
-
Use OR conditions to make formula TRUE for unwanted cases
Pattern
12. Example: Works on All Projects of Dept 5
English Meaning
An employee must work on every project controlled by department 5.
Logical Structure
-
Exclude non-projects
-
Exclude projects not in dept 5
-
Enforce work condition on remaining projects
13. Existential vs Universal Transformation
Key equivalence:
This allows replacing ALL queries with NOT EXISTS patterns — commonly used in SQL.
14. Sample TRC Queries
Employees with No Dependents
Managers with At Least One Dependent
15. Safe vs Unsafe Expressions
Unsafe Expression ❌
-
Refers to infinite universe
-
Produces infinite results
Safe Expression ✅
-
All values come from:
-
Relations mentioned
-
Constants in the query
-
Rule:
A TRC expression is safe if its result is finite and domain-restricted.
16. Relationship to SQL
| TRC Concept | SQL Equivalent |
|---|---|
| Tuple variable | Table alias |
| Existential quantifier | EXISTS |
| Universal quantifier | NOT EXISTS |
| Formula | WHERE clause |
| Declarative | SQL is declarative |
SQL is essentially a restricted, safe form of tuple relational calculus.
17. Summary
-
Tuple Relational Calculus is non-procedural
-
Uses logic and predicates
-
Expresses selection, projection, joins, and quantification
-
Foundation for SQL semantics
-
Must ensure safety to avoid infinite results
Comments
Post a Comment