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 AlgebraRelational 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

  1. Strong mathematical foundation in predicate logic

  2. Foundation of SQL (especially SELECT–FROM–WHERE)

  3. Provides a formal basis for query correctness

  4. Helps understand quantifiers, joins, and subqueries


3. Basic Form of a TRC Expression

A tuple relational calculus query has the form:

{tCOND(t)}\{ t \mid COND(t) \}

Where:

  • t is a tuple variable

  • COND(t) is a Boolean condition

  • Result = all tuples t that satisfy COND(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

{ t | EMPLOYEE(t) AND t.Salary > 50000 }
  • EMPLOYEE(t) → restricts t to EMPLOYEE relation

  • t.Salary > 50000 → selection condition


5. Selecting Specific Attributes (Projection)

To retrieve only certain attributes:

{ t.Fname, t.Lname | EMPLOYEE(t) AND t.Salary > 50000 }

Equivalent to:

πFname,Lname(σSalary>50000(EMPLOYEE))

6. General Form of TRC Expression

{t1.A1,t2.A2,COND(t1,t2,)}\{ t_1.A_1, t_2.A_2, \ldots \mid COND(t_1, t_2, \ldots) \}

A TRC query consists of:

  1. Tuple variables

  2. Range relations

  3. Selection conditions

  4. Join conditions

  5. Attributes to retrieve


7. Atoms and Formulas

Types of Atoms

  1. Relation atom

R(t)

Means t is a tuple of relation R

  1. Tuple-to-tuple comparison

t1.A = t2.B
  1. Tuple-to-constant comparison

t.A > 50000

Building Formulas

Formulas are built using:

  • AND

  • OR

  • NOT

Example:

EMPLOYEE(t) AND t.Salary > 50000 AND t.Dno = 5

8. Free and Bound Variables

  • Free variables → appear in the output

  • Bound variables → used only for conditions

Example:

{ t.Fname | EMPLOYEE(t) AND (∃d)(DEPARTMENT(d) AND t.Dno = d.Dnumber) }
  • 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

{ t.Fname, t.Lname | EMPLOYEE(t) AND (∃d)(DEPARTMENT(d) AND d.Dname='Research' AND t.Dno=d.Dnumber) }

This corresponds to a join + selection.


10. Universal Quantifier (∀)

Meaning

“For every tuple…”

Example (Conceptual)

Employees who work on all projects of department 5:

(∀x)(PROJECT(x) AND x.Dnum=5 → employee works on x)

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

(∀x)(NOT Relevant(x) OR Condition(x))

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:

(∀x)(P(x)) ≡ NOT (∃x)(NOT P(x))

This allows replacing ALL queries with NOT EXISTS patterns — commonly used in SQL.


14. Sample TRC Queries

Employees with No Dependents

{ e.Fname, e.Lname | EMPLOYEE(e) AND NOT (∃d)(DEPENDENT(d) AND d.Essn = e.Ssn) }

Managers with At Least One Dependent

{ e.Fname, e.Lname | EMPLOYEE(e) AND (∃d)(DEPENDENT(d) AND d.Essn = e.Ssn) }

15. Safe vs Unsafe Expressions

Unsafe Expression ❌

{ t | NOT EMPLOYEE(t) }
  • 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

Popular posts from this blog

Database Management Systems DBMS PCCST402 Semester 4 KTU CS 2024 Scheme

Data Models, Schemas and Instances

Introduction to Database Management System -DBMS