1. Introduction to Relational Calculus
Relational Calculus is a non-procedural query language used in relational databases. Unlike Relational Algebra, which focuses on how to retrieve data, Relational Calculus focuses on what data to retrieve.
🔹 Key Idea: Instead of specifying step-by-step operations, it defines conditions that must be met to retrieve data.
🔹 Used in: Query Optimization & Database Query Languages like SQL.
Comparison: Relational Algebra vs. Relational Calculus
Feature | Relational Algebra | Relational Calculus |
---|---|---|
Type | Procedural | Non-Procedural |
Focus | How to retrieve data | What data to retrieve |
Approach | Uses operations like selection, projection, join | Uses predicate logic |
Output | New relation (table) | Desired tuples satisfying conditions |
Basis of | Query Execution | Query Definition |
2. Types of Relational Calculus
Relational Calculus is of two types:
1️⃣ Tuple Relational Calculus (TRC) → Uses tuples (rows) as variables.
2️⃣ Domain Relational Calculus (DRC) → Uses columns (attributes) as variables.
3. Tuple Relational Calculus (TRC)
Tuple Relational Calculus uses tuples (rows) as variables and applies conditions to select tuples.
Syntax of TRC
{ t | Condition (t) }
🔹 { t }
→ Represents the tuples (rows) to be selected.
🔹 Condition(t)
→ Specifies the condition that must be satisfied.
Example 1: Retrieve Students Enrolled in ‘CS’ Course
{ t | t ∈ STUDENT ∧ t.Course = 'CS' }
✅ Explanation:
t ∈ STUDENT
→t
is a tuple from theSTUDENT
table.t.Course = 'CS'
→ Select only those students who are in the CS course.
📌 Equivalent SQL Query:
SELECT * FROM STUDENT WHERE Course = 'CS';
Example 2: Retrieve Names of Students Aged Over 21
{ t.Name | t ∈ STUDENT ∧ t.Age > 21 }
✅ Explanation:
t ∈ STUDENT
→t
is a tuple fromSTUDENT
.t.Age > 21
→ Select students withAge > 21
.{ t.Name }
→ Only return theName
attribute.
📌 Equivalent SQL Query:
SELECT Name FROM STUDENT WHERE Age > 21;
TRC Using Logical Connectors
Symbol | Meaning |
---|---|
∧ (AND) | Both conditions must be true |
∨ (OR) | At least one condition must be true |
¬ (NOT) | Negation of a condition |
∃ (Exists) | There exists at least one tuple satisfying the condition |
∀ (For All) | The condition must hold for all tuples |
🔹 Example: Retrieve students who are not enrolled in CS.
{ t | t ∈ STUDENT ∧ ¬(t.Course = 'CS') }
📌 Equivalent SQL Query:
SELECT * FROM STUDENT WHERE Course <> 'CS';
4. Domain Relational Calculus (DRC)
Domain Relational Calculus uses column values (attributes) as variables instead of tuples.
Syntax of DRC
{ <X1, X2, ..., Xn> | Condition (X1, X2, ..., Xn) }
🔹 <X1, X2, ..., Xn>
→ Specifies the attributes (columns) to be retrieved.
🔹 Condition (X1, X2, ..., Xn)
→ Specifies the conditions that must be met.
Example 1: Retrieve Names of Students in CS Course
{ <Name> | ∃ Age, Course (STUDENT(Name, Age, Course) ∧ Course = 'CS') }
✅ Explanation:
∃ Age, Course
→ These attributes exist in theSTUDENT
table.STUDENT(Name, Age, Course)
→ Name, Age, and Course are columns.Course = 'CS'
→ Select only CS students.
📌 Equivalent SQL Query:
SELECT Name FROM STUDENT WHERE Course = 'CS';
Example 2: Retrieve Students Aged Above 21
{ <Name> | ∃ Age, Course (STUDENT(Name, Age, Course) ∧ Age > 21) }
📌 Equivalent SQL Query:
SELECT Name FROM STUDENT WHERE Age > 21;
5. Differences Between Tuple and Domain Relational Calculus
Feature | Tuple Relational Calculus (TRC) | Domain Relational Calculus (DRC) |
---|---|---|
Based On | Tuples (rows) | Attributes (columns) |
Variables | Represent rows in a relation | Represent values of attributes |
Syntax | `{ t | Condition(t) }` |
Example | `{ t | t ∈ STUDENT ∧ t.Age > 21 }` |
6. Advantages of Relational Calculus
✅ Declarative Approach: Focuses on what data is needed, not how to retrieve it.
✅ Logical and Flexible: Uses logical expressions to define queries.
✅ Foundation of SQL: Helps in query formulation and optimization.
7. Disadvantages of Relational Calculus
❌ Complex Queries: Writing queries with logical expressions can be difficult.
❌ Less Efficient Execution: Since it lacks step-by-step operations, DBMS must convert it into Relational Algebra for execution.
8. Conclusion
Relational Calculus is a powerful non-procedural query language that defines conditions for retrieving data logically rather than specifying steps. It plays a crucial role in query formulation, optimization, and database theory.