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โtis a tuple from theSTUDENTtable.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โtis a tuple fromSTUDENT.t.Age > 21โ Select students withAge > 21.{ t.Name }โ Only return theNameattribute.
๐ 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 theSTUDENTtable.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.
