Relational Calculus – A Non-Procedural Query Language
Relational Calculus is a non-procedural query language used in databases. Unlike Relational Algebra, which tells how to get the result (step-by-step), Relational Calculus tells what data to retrieve, without specifying how to do it.
There are two main types:
Â
- Tuple Relational Calculus (TRC)
- Domain Relational Calculus (DRC)
Â
Let’s look at both in detail.
1. Tuple Relational Calculus (TRC)
Tuple Relational Calculus is based on the use of tuples (rows) as variables. It expresses queries in the form of predicates (conditions) that a tuple must satisfy.
Â
General Format:
- t is a tuple variable.
- P(t) is a predicate (condition) applied to t.
Â
This notation means:
“Retrieve all tuples t
such that the predicate P(t)
is true.”
Example:
Suppose we have a Student
table with attributes (Name, Age, Dept).
To find names of students in the “CS” department:
Â
Â
Explanation:
t ∈ Student
means tuplet
comes from the Student relation.t.Dept = 'CS'
is the condition.- We are projecting only the
Name
attribute.
2. Domain Relational Calculus (DRC)
Domain Relational Calculus uses domain variables (i.e., variables that range over individual attribute values—not whole tuples).
Â
General Format:
- Each variable represents a column value (not the full row).
P(x1, x2, ..., xn)
is a condition on those values.
Â
Example:
From the same Student(Name, Age, Dept)
table, to find the names of students in the “CS” department:
Â
Explanation:
N, A, D
are domain variables for Name, Age, Dept.- We are selecting only the
N
(name) values. - The predicate ensures that the tuple
(N, A, D)
exists in the Student table and thatD = 'CS'
.
Key Differences Between TRC and DRC
Feature | Tuple Relational Calculus (TRC) | Domain Relational Calculus (DRC) |
---|---|---|
Variables represent | Whole tuples (rows) | Individual field values (columns) |
Format | `{ t | P(t) }` |
Style | Closer to logic with tuple-based view | Attribute-wise logical statements |
Focus | Tuples and their properties | Specific fields and domain values |
Relational Calculus is used to state what result we want without specifying how to get it.
Â
It is based on predicate logic and helps in theoretical understanding and query formulation.
Â
Both TRC and DRC form the theoretical foundation for high-level query languages like SQL.