Tuple and Domain Relational Calculus

Unit 2CLO04

While relational algebra is procedural (specifies how to compute), relational calculus is declarative (specifies what to compute). It's the theoretical basis for SQL's SELECT statements and provides an alternative way to express queries using mathematical logic.

The Basics

Relational calculus is a non-procedural query language based on mathematical logic (predicate calculus). It describes what data to retrieve without specifying how to retrieve it.

Two Variants:

1. Tuple Relational Calculus (TRC)
Variables range over tuples (rows)

2. Domain Relational Calculus (DRC)
Variables range over domain values (individual attributes)

General Form (TRC):

{ t | P(t) }

Where:

  • t is a tuple variable
  • P(t) is a predicate (logical formula)
  • Result contains all tuples t for which P(t) is true

Quantifiers:

Existential (∃): "There exists"
∃t ∈ R (P(t)) means: There exists a tuple t in relation R such that P(t) is true

Universal (∀): "For all"
∀t ∈ R (P(t)) means: For all tuples t in relation R, P(t) is true

Relationship to SQL:

TRC/DRC ≡ SQL SELECT
Relational Algebra ≡ Query execution plans

All three are relationally complete (express same queries)

Technical Details

TUPLE RELATIONAL CALCULUS (TRC)

Syntax:

{ t | P(t) }
{ t.A1, t.A2, ..., t.An | P(t) }

Components:

  1. Tuple Variables: t, s, u (range over tuples)

  2. Atoms (basic predicates):

    • t ∈ R: tuple t is in relation R
    • t.A op s.B: compare attributes
    • t.A op constant: compare with constant
      where op ∈ {=, ≠, <, >, ≤, ≥}
  3. Formulas (combine atoms):

    • Atoms are formulas
    • If P, Q are formulas: ¬P, P ∧ Q, P ∨ Q, P ⇒ Q are formulas
    • If P is a formula: ∃t ∈ R (P), ∀t ∈ R (P) are formulas

Operator Precedence:

  1. ¬ (NOT)
  2. ∧ (AND)
  3. ∨ (OR)
  4. ⇒ (IMPLIES)

Quantifier Scope:

  • ∃t ∈ R (P(t)) binds t in P
  • ∀t ∈ R (P(t)) binds t in P
  • Free variables: not bound by quantifier
  • Bound variables: bound by quantifier

Examples:

Simple Query:
{ t | t ∈ Employee ∧ t.Salary > 50000 }
"Find all employees with salary > 50000"

With Projection:
{ t.Name | t ∈ Employee ∧ t.Salary > 50000 }
"Find names of employees with salary > 50000"

With Existential Quantifier:
{ t | t ∈ Employee ∧ ∃d ∈ Department (t.DeptID = d.DeptID ∧ d.DeptName = 'IT') }
"Find employees in IT department"

With Universal Quantifier:
{ t | t ∈ Student ∧ ∀c ∈ Required_Courses (∃e ∈ Enrollment (e.StudentID = t.StudentID ∧ e.CourseID = c.CourseID)) }
"Find students enrolled in ALL required courses"

Quantifier Equivalences:

De Morgan's Laws:

  • ¬(∃t ∈ R (P(t))) ≡ ∀t ∈ R (¬P(t))
  • ¬(∀t ∈ R (P(t))) ≡ ∃t ∈ R (¬P(t))

Implication:

  • ∀t (P(t) ⇒ Q(t)) ≡ ∀t (¬P(t) ∨ Q(t))
  • ∃t (P(t) ∧ Q(t)) ≡ ¬∀t (P(t) ⇒ ¬Q(t))

DOMAIN RELATIONAL CALCULUS (DRC)

Syntax:

{ <x1, x2, ..., xn> | P(x1, x2, ..., xn) }

Components:

  1. Domain Variables: x, y, z (range over attribute values)

  2. Atoms:

    • <x1, x2, ..., xn> ∈ R: values form a tuple in R
    • x op y: compare domain variables
    • x op constant: compare with constant
  3. Formulas: Same as TRC

Examples:

Simple Query:
{ <n, s> | <i, n, d, s> ∈ Employee ∧ s > 50000 }
"Find names and salaries of employees earning > 50000"

With Existential Quantifier:
{ <n> | ∃i, d, s (<i, n, d, s> ∈ Employee ∧ ∃dn, l (<d, dn, l> ∈ Department ∧ dn = 'IT')) }
"Find names of IT department employees"

With Universal Quantifier:
{ <sid> | <sid, sn> ∈ Student ∧ ∀cid (∃cn (<cid, cn> ∈ Required_Courses ⇒ ∃g (<sid, cid, g> ∈ Enrollment))) }
"Find student IDs for students enrolled in all required courses"

SAFETY OF EXPRESSIONS

Problem: Some expressions can produce infinite results

Unsafe Expression:
{ t | ¬(t ∈ Employee) }
"All tuples NOT in Employee" → Infinite!

{ t | true }
"All possible tuples" → Infinite!

Safe Expression Definition:

An expression is safe if:

  1. All values in result are from domain of expression
  2. For every atom, variables range over finite sets
  3. Domain of expression: set of all values in relations mentioned in expression

Domain of Expression:

dom(expression) = ∪ all constants and attribute values in relations mentioned

Safe Expression Constraints:

  1. For ∃t ∈ R (P(t)): Result tuples must come from dom(R)
  2. For ∀t ∈ R (P(t)): Typically written as ∀t (t ∈ R ⇒ P(t))
  3. Negation carefully handled to avoid infinite results

Example Safe Expression:

{ t | t ∈ Employee ∧ ¬(t.Salary > 50000) }
Safe: Result limited to Employee tuples

Example Unsafe Expression:

{ t | ¬(t ∈ Employee) }
Unsafe: Infinite tuples not in Employee

Making Unsafe Expressions Safe:

Unsafe:
{ t | ¬∃d ∈ Department (d.DeptID = t.DeptID) }

Safe:
{ t | t ∈ Employee ∧ ¬∃d ∈ Department (d.DeptID = t.DeptID) }
(Restrict to Employee tuples)

CONVERSION RULES

TRC to SQL:

TRC: { t.Name, t.Salary | t ∈ Employee ∧ t.Salary > 50000 }

SQL:
SELECT Name, Salary
FROM Employee
WHERE Salary > 50000;

DRC to SQL:

DRC: { <n, s> | <i, n, d, s> ∈ Employee ∧ s > 50000 }

SQL:
SELECT Name, Salary
FROM Employee
WHERE Salary > 50000;

SQL to TRC:

SQL:
SELECT e.Name
FROM Employee e, Department d
WHERE e.DeptID = d.DeptID AND d.DeptName = 'IT';

TRC:
{ t.Name | t ∈ Employee ∧ ∃d ∈ Department (t.DeptID = d.DeptID ∧ d.DeptName = 'IT') }

EXPRESSIVE POWER

Theorem (Codd's Theorem):
Relational Algebra ≡ Safe TRC ≡ Safe DRC ≡ SQL (without aggregates)

All express same queries (relationally complete)

What they CANNOT express:

  • Transitive closure (e.g., find all ancestors)
  • Aggregates (SUM, AVG, COUNT) - need extensions
  • Recursive queries (need SQL recursive CTEs)

Examples

EXAMPLE SCHEMA:

Employee Table:

EmpIDNameDeptIDSalary
101Alice1060000
102Bob2055000
103Charlie1070000
104Diana3050000

Department Table:

DeptIDDeptNameLocation
10ITNYC
20HRLA
30SalesChicago

Project Table:

ProjIDProjNameDeptID
P1Alpha10
P2Beta20
P3Gamma10

WorksOn Table:

EmpIDProjIDHours
101P120
101P315
102P230
103P125

TRC EXAMPLES:

Example 1: Simple Selection

Query: Find all employees with salary > 55000

TRC:
{ t | t ∈ Employee ∧ t.Salary > 55000 }

Result:

EmpIDNameDeptIDSalary
101Alice1060000
103Charlie1070000

SQL:
SELECT * FROM Employee WHERE Salary > 55000;

Example 2: Projection

Query: Find names of employees earning > 55000

TRC:
{ t.Name | t ∈ Employee ∧ t.Salary > 55000 }

Result:

Name
Alice
Charlie

SQL:
SELECT Name FROM Employee WHERE Salary > 55000;

Example 3: Join with Existential Quantifier

Query: Find names of employees in IT department

TRC:
{ t.Name | t ∈ Employee ∧ ∃d ∈ Department (t.DeptID = d.DeptID ∧ d.DeptName = 'IT') }

Result:

Name
Alice
Charlie

SQL:
SELECT e.Name
FROM Employee e
WHERE EXISTS (
SELECT 1
FROM Department d
WHERE e.DeptID = d.DeptID AND d.DeptName = 'IT'
);

Example 4: Multiple Conditions

Query: Find employees in NYC earning > 55000

TRC:
{ t.Name, t.Salary | t ∈ Employee ∧ ∃d ∈ Department (t.DeptID = d.DeptID ∧ d.Location = 'NYC' ∧ t.Salary > 55000) }

Result:

NameSalary
Alice60000
Charlie70000

SQL:
SELECT e.Name, e.Salary
FROM Employee e
JOIN Department d ON e.DeptID = d.DeptID
WHERE d.Location = 'NYC' AND e.Salary > 55000;

Example 5: Universal Quantifier

Query: Find employees who work on ALL projects in their department

TRC:
{ t.Name | t ∈ Employee ∧ ∀p ∈ Project (p.DeptID ≠ t.DeptID ∨ ∃w ∈ WorksOn (w.EmpID = t.EmpID ∧ w.ProjID = p.ProjID)) }

Explanation:

  • For all projects p
  • Either p is not in employee's department OR employee works on p

Result:

Name
Alice
(Alice works on both IT projects P1 and P3)

SQL:
SELECT e.Name
FROM Employee e
WHERE NOT EXISTS (
SELECT p.ProjID
FROM Project p
WHERE p.DeptID = e.DeptID
AND p.ProjID NOT IN (
SELECT w.ProjID
FROM WorksOn w
WHERE w.EmpID = e.EmpID
)
);

Example 6: Negation

Query: Find employees NOT in IT department

TRC:
{ t.Name | t ∈ Employee ∧ ¬∃d ∈ Department (t.DeptID = d.DeptID ∧ d.DeptName = 'IT') }

Result:

Name
Bob
Diana

SQL:
SELECT Name
FROM Employee
WHERE DeptID NOT IN (
SELECT DeptID FROM Department WHERE DeptName = 'IT'
);

DRC EXAMPLES:

Example 7: Simple Selection (DRC)

Query: Find employee IDs and names with salary > 55000

DRC:
{ <i, n> | ∃d, s (<i, n, d, s> ∈ Employee ∧ s > 55000) }

Result:

EmpIDName
101Alice
103Charlie

SQL:
SELECT EmpID, Name FROM Employee WHERE Salary > 55000;

Example 8: Join (DRC)

Query: Find employee names and their department names

DRC:
{ <en, dn> | ∃ei, ed, es, did, dl (
<ei, en, ed, es> ∈ Employee ∧
<did, dn, dl> ∈ Department ∧
ed = did
) }

Result:

EmpNameDeptName
AliceIT
BobHR
CharlieIT
DianaSales

SQL:
SELECT e.Name AS EmpName, d.DeptName
FROM Employee e
JOIN Department d ON e.DeptID = d.DeptID;

Example 9: Universal Quantifier (DRC)

Query: Find students enrolled in all courses

Assume:
Student: <StudentID, Name>
Course: <CourseID, CourseName>
Enrollment: <StudentID, CourseID>

DRC:
{ <sid> | ∃sn (<sid, sn> ∈ Student ∧
∀cid (∃cn (<cid, cn> ∈ Course) ⇒
∃ (<sid, cid> ∈ Enrollment))
) }

SQL:
SELECT s.StudentID
FROM Student s
WHERE NOT EXISTS (
SELECT c.CourseID
FROM Course c
WHERE NOT EXISTS (
SELECT *
FROM Enrollment e
WHERE e.StudentID = s.StudentID
AND e.CourseID = c.CourseID
)
);

Example 10: Complex Query with Multiple Quantifiers

Query: Find departments where all employees earn > 50000

TRC:
{ d.DeptName | d ∈ Department ∧
∀t ∈ Employee (t.DeptID ≠ d.DeptID ∨ t.Salary > 50000)
}

Explanation:

  • For all employees t
  • Either t is not in this department OR t.Salary > 50000

DRC:
{ <dn> | ∃did, dl (<did, dn, dl> ∈ Department ∧
∀ei, en, ed, es (
<ei, en, ed, es> ∈ Employee ⇒
(ed ≠ did ∨ es > 50000)
))
}

SQL:
SELECT d.DeptName
FROM Department d
WHERE NOT EXISTS (
SELECT *
FROM Employee e
WHERE e.DeptID = d.DeptID AND e.Salary <= 50000
);

SAFETY EXAMPLES:

Example 11: Unsafe Expression

Unsafe:
{ t | ¬(t ∈ Employee) }

Why unsafe? Result includes infinite tuples not in Employee

Safe version:
Not possible to make this safe meaningfully

Example 12: Safe Expression with Negation

Query: Find employees NOT working on any project

Unsafe:
{ t | t ∈ Employee ∧ ¬∃w ∈ WorksOn (w.EmpID = t.EmpID) }

Actually this IS safe because:

  1. t bounded to Employee
  2. Result limited to Employee tuples

Result:

EmpIDNameDeptIDSalary
104Diana3050000

SQL:
SELECT *
FROM Employee e
WHERE NOT EXISTS (
SELECT * FROM WorksOn w WHERE w.EmpID = e.EmpID
);

Real-World Use

PRACTICAL APPLICATIONS:

1. Query Language Foundation:

Relational calculus is the theoretical basis for SQL's SELECT statement.

SQL SELECT = TRC/DRC expression

2. Query Optimization:

Understanding calculus helps:

  • Rewrite queries for efficiency
  • Understand query equivalences
  • Recognize optimization opportunities

3. Formal Verification:

Prove two queries are equivalent using calculus

Query 1 (SQL):
SELECT Name FROM Employee WHERE Salary > 50000;

Query 2 (SQL):
SELECT Name FROM Employee WHERE NOT (Salary <= 50000);

Both equivalent to:
{ t.Name | t ∈ Employee ∧ t.Salary > 50000 }

4. Database Theory:

  • Prove query language completeness
  • Study expressiveness limitations
  • Understand decidability

5. Advanced Query Features:

Calculus forms basis for:

  • Nested subqueries
  • Correlated subqueries
  • EXISTS/NOT EXISTS
  • ALL/ANY quantifiers

SQL TO CALCULUS PATTERNS:

EXISTS → ∃

SQL:
SELECT e.Name
FROM Employee e
WHERE EXISTS (
SELECT * FROM Department d
WHERE e.DeptID = d.DeptID AND d.DeptName = 'IT'
);

TRC:
{ t.Name | t ∈ Employee ∧ ∃d ∈ Department (t.DeptID = d.DeptID ∧ d.DeptName = 'IT') }

NOT EXISTS → ∀ (negated)

SQL:
SELECT e.Name
FROM Employee e
WHERE NOT EXISTS (
SELECT * FROM WorksOn w
WHERE w.EmpID = e.EmpID
);

TRC:
{ t.Name | t ∈ Employee ∧ ¬∃w ∈ WorksOn (w.EmpID = t.EmpID) }

Or equivalently:
{ t.Name | t ∈ Employee ∧ ∀w ∈ WorksOn (w.EmpID ≠ t.EmpID) }

ALL → ∀

SQL:
SELECT Name
FROM Employee
WHERE Salary >= ALL (SELECT Salary FROM Employee);

TRC:
{ t.Name | t ∈ Employee ∧ ∀s ∈ Employee (t.Salary ≥ s.Salary) }

ANY → ∃

SQL:
SELECT Name
FROM Employee
WHERE Salary > ANY (SELECT Salary FROM Employee WHERE DeptID = 10);

TRC:
{ t.Name | t ∈ Employee ∧ ∃s ∈ Employee (s.DeptID = 10 ∧ t.Salary > s.Salary) }

TOOLS FOR PRACTICE:

  1. Paper and Pencil: Best for learning
  2. Logic Proof Assistants: Coq, Isabelle
  3. Relational Calculus Interpreters: Some academic tools
  4. SQL: Practice by writing equivalent SQL

COMMON MISTAKES:

Mistake 1: Forgetting Domain Restriction

Wrong:
{ t | t.Salary > 50000 }

Right:
{ t | t ∈ Employee ∧ t.Salary > 50000 }

Mistake 2: Incorrect Quantifier Usage

Wrong (for "employees in IT"):
{ t.Name | ∀d ∈ Department (t.DeptID = d.DeptID ∧ d.DeptName = 'IT') }

Right:
{ t.Name | t ∈ Employee ∧ ∃d ∈ Department (t.DeptID = d.DeptID ∧ d.DeptName = 'IT') }

Mistake 3: Unsafe Expressions

Wrong:
{ t | ¬(t ∈ Employee) }

Right:
{ t | t ∈ SomeRelation ∧ ¬(t ∈ Employee) }

Mistake 4: Confusing ∀ with ∃

"Find employees working on project P1":
Wrong: ∀w ∈ WorksOn (w.ProjID = 'P1')
Right: ∃w ∈ WorksOn (w.EmpID = t.EmpID ∧ w.ProjID = 'P1')

INTERVIEW TIPS:

  1. Remember: ∃ = OR, ∀ = AND (loosely)
  2. Universal quantifier often used with implication (⇒)
  3. Double negation for "for all": ¬∃ ≡ ∀¬
  4. Always specify tuple variable domain (t ∈ R)
  5. Check safety: Result must be finite

For exams

IMPORTANT EXAM QUESTIONS:

1. Define Tuple Relational Calculus (TRC) and Domain Relational Calculus (DRC).
TRC: Variables range over tuples
DRC: Variables range over domain values

2. What are existential (∃) and universal (∀) quantifiers? Give examples.
∃: "There exists", ∀: "For all"

3. Explain the safety of relational calculus expressions. Why is it important?
Prevents infinite results; ensures computability

4. Give an example of an unsafe expression and explain why it's unsafe.
{ t | ¬(t ∈ R) } - includes infinite tuples not in R

5. Convert the following SQL to TRC:
SELECT Name FROM Employee WHERE Salary > 50000;

Answer: { t.Name | t ∈ Employee ∧ t.Salary > 50000 }

6. Convert the following TRC to SQL:
{ t.Name | t ∈ Employee ∧ ∃d ∈ Department (t.DeptID = d.DeptID ∧ d.DeptName = 'IT') }

Answer:
SELECT e.Name
FROM Employee e, Department d
WHERE e.DeptID = d.DeptID AND d.DeptName = 'IT';

7. Write TRC expression: Find employees earning more than all employees in Dept 10

Answer: { t.Name | t ∈ Employee ∧ ∀s ∈ Employee (s.DeptID ≠ 10 ∨ t.Salary > s.Salary) }

8. Write DRC expression: Find names and salaries of IT employees

Answer: { <n, s> | ∃i, d (<i, n, d, s> ∈ Employee ∧ ∃dn, l (<d, dn, l> ∈ Department ∧ dn = 'IT')) }

9. Explain De Morgan's laws in context of relational calculus quantifiers.

  • ¬(∃t (P(t))) ≡ ∀t (¬P(t))
  • ¬(∀t (P(t))) ≡ ∃t (¬P(t))

10. Compare Tuple Relational Calculus and Domain Relational Calculus.
TRC: More compact, closer to SQL
DRC: More explicit, closer to first-order logic

11. State and prove Codd's Theorem.
Relational Algebra ≡ Safe TRC ≡ Safe DRC (same expressive power)

12. Write TRC expression: Find students enrolled in all courses

Assume: Student(SID, Name), Course(CID, Name), Enrollment(SID, CID)

Answer:
{ t.Name | t ∈ Student ∧ ∀c ∈ Course (∃e ∈ Enrollment (e.SID = t.SID ∧ e.CID = c.CID)) }

13. Explain the difference between relational algebra and relational calculus.
Algebra: Procedural (how to compute)
Calculus: Declarative (what to compute)
Both relationally complete

14. Give TRC expressions for:
a) Selection: { t | t ∈ R ∧ t.A > 5 }
b) Projection: { t.A, t.B | t ∈ R }
c) Join: { <t, s> | t ∈ R ∧ s ∈ S ∧ t.A = s.A }
d) Union: { t | t ∈ R ∨ t ∈ S }
e) Difference: { t | t ∈ R ∧ ¬(t ∈ S) }

15. Convert complex SQL with nested subqueries to TRC:

SQL:
SELECT e.Name
FROM Employee e
WHERE e.Salary > (SELECT AVG(Salary) FROM Employee)
AND EXISTS (
    SELECT * FROM WorksOn w
    WHERE w.EmpID = e.EmpID AND w.Hours > 20
);

TRC:
{ t.Name | t ∈ Employee ∧
    t.Salary > (average of all salaries) ∧
    ∃w ∈ WorksOn (w.EmpID = t.EmpID ∧ w.Hours > 20)
}

Note: Aggregates not directly expressible in basic calculus

QUICK REVISION:

TRC Syntax:
{ t | P(t) } or { t.A1, t.A2, ... | P(t) }

DRC Syntax:
{ <x1, x2, ...> | P(x1, x2, ...) }

Quantifiers:

  • ∃t ∈ R (P(t)): There exists
  • ∀t ∈ R (P(t)): For all

Logical Operators:

  • ∧: AND
  • ∨: OR
  • ¬: NOT
  • ⇒: IMPLIES (A ⇒ B ≡ ¬A ∨ B)

De Morgan's Laws:

  • ¬(∃t (P(t))) ≡ ∀t (¬P(t))
  • ¬(∀t (P(t))) ≡ ∃t (¬P(t))

SQL Equivalences:

  • SELECT → Projection { t.A, t.B | ... }
  • WHERE → Selection { t | ... ∧ condition }
  • JOIN → Join { t | ∃s ∈ S (...) }
  • EXISTS → ∃
  • NOT EXISTS → ¬∃ or ∀¬
  • ALL → ∀
  • ANY → ∃

Safety:

  • Must restrict variables to finite domains
  • Result must be finite
  • { t | t ∈ R ∧ ... } is safe
  • { t | ¬(t ∈ R) } is unsafe

Key points

KEY TAKEAWAYS:

Relational calculus is declarative (what to compute), algebra is procedural (how to compute)

Two variants: Tuple Relational Calculus (TRC) and Domain Relational Calculus (DRC)

TRC variables range over tuples, DRC variables range over domain values

General form: { t | P(t) } where P(t) is a logical predicate

Quantifiers: ∃ (exists) and ∀ (for all) express complex conditions

∃ used for EXISTS, ANY in SQL; ∀ used for ALL, NOT EXISTS (negated)

Safety crucial: Expressions must produce finite results

Unsafe expressions allow infinite results (e.g., { t | ¬(t ∈ R) })

Safe expressions restrict variables to specific relations

De Morgan's laws: ¬∃ ≡ ∀¬ (useful for rewriting)

Codd's Theorem: Relational Algebra ≡ Safe TRC ≡ Safe DRC (all relationally complete)

Universal quantifier pattern: ∀t (P(t) ⇒ Q(t)) means "for all t satisfying P, Q is true"

TRC is theoretical basis for SQL SELECT statements

Understanding calculus helps: Write complex SQL, understand query semantics, optimize queries

All three equivalent: Relational Algebra, TRC, DRC can express same queries

REMEMBER: Relational calculus provides mathematical foundation for SQL. While you write SQL in practice, understanding calculus helps you reason about query correctness, equivalence, and optimization!