Relational Calculus in DBMS
In the world of databases, there are different ways to query and retrieve data. One of these approaches is Relational Calculus, a non-procedural query language used in Database Management Systems (DBMS). Unlike procedural query languages (like SQL), which require specific instructions on how to retrieve data, relational calculus only specifies what to retrieve, leaving the system to figure out how to do it.
Several commercial query languages like SQL, QBE (Query By Example), and QUEL are inspired by aspects of relational calculus. In this blog, we will discuss the key concepts of relational calculus, its types, and use cases, along with examples in tabular form.
Why is it Called Relational Calculus?
Relational calculus is based on Predicate Calculus, a part of symbolic logic. In predicate calculus, expressions are used to describe logical statements, which can either be true or false based on the values substituted for variables. Relational calculus applies a tailored version of this concept to interact with relational databases.
When using relational calculus, two common quantifiers are often employed:
- Universal Quantifier ( ∀ ): This means "for all." It indicates that all tuples in a given set must satisfy a particular condition.
- Existential Quantifier ( ∃ ): This means "there exists." It shows that at least one tuple in the given set satisfies the specified condition.
To better understand these, we also need to be familiar with the terms Free Variables and Bound Variables:
- A variable is considered bound if it is quantified (i.e., linked to either ∀ or ∃).
- A free variable is not linked to any quantifier, much like global variables in programming.
Types of Relational Calculus
Relational calculus is divided into two main types:
- Tuple Relational Calculus (TRC)
- Domain Relational Calculus (DRC)
Let’s explore these concepts in more detail.
1. Tuple Relational Calculus (TRC)
Tuple Relational Calculus (TRC) is a non-procedural query language where we work with tuples (rows) of a relation. In TRC, we express the desired result by specifying conditions, without defining how to obtain the result. TRC works with tuple variables (sometimes called range variables) and applies logical predicates to fetch the required tuples.
Notation:
A query in TRC follows this notation:
{T | P(T)}
T
represents the resulting tuples.
P(T)
represents the condition or predicate used to filter those tuples.
Example:
Suppose we have an EMPLOYEE
table with EMP_ID
, NAME
, and SALARY
attributes. We want to retrieve the names of employees who earn a salary greater than 50,000.
EMP_ID |
NAME |
SALARY |
101 |
Alice |
60,000 |
102 |
Bob |
45,000 |
103 |
Charlie |
55,000 |
TRC Query:
{T.NAME | EMPLOYEE(T) AND T.SALARY > 50,000}
Result:
This query fetches the names of employees earning more than 50,000 from the EMPLOYEE
table.
Using Quantifiers in TRC
TRC allows the use of Existential (∃) and Universal (∀) quantifiers to refine queries further.
Example:
We can use quantifiers to find employees who earn more than 50,000 and also work in the HR
department. Suppose we add a DEPARTMENT
column to our EMPLOYEE
table:
EMP_ID |
NAME |
SALARY |
DEPARTMENT |
101 |
Alice |
60,000 |
HR |
102 |
Bob |
45,000 |
IT |
103 |
Charlie |
55,000 |
HR |
TRC Query:
{T.NAME | ∃T ∈ EMPLOYEE (T.SALARY > 50,000 AND T.DEPARTMENT = 'HR')}
Result:
This query retrieves the names of employees in the HR
department who earn more than 50,000.
2. Domain Relational Calculus (DRC)
Domain Relational Calculus (DRC) is another type of non-procedural query language. Instead of working with tuples, DRC works with individual attribute values. In DRC, conditions are applied to domains (i.e., attributes of the table), and the result consists of specific attributes that satisfy those conditions.
Notation:
A query in DRC is expressed as:
{a1, a2, a3, ..., an | P(a1, a2, a3, ..., an)}
a1, a2, ...
are attributes of the relation.
P
represents the condition or predicate applied to the attributes.
Example:
Consider the same EMPLOYEE
table. Now, let’s write a DRC query to find the EMP_ID
and NAME
of employees who earn a salary greater than 50,000.
DRC Query:
{EMP_ID, NAME | EMPLOYEE(EMP_ID, NAME, SALARY) AND SALARY > 50,000}
Result:
EMP_ID |
NAME |
101 |
Alice |
103 |
Charlie |
This query returns the EMP_ID
and NAME
of employees whose salary exceeds 50,000.
Using Quantifiers in DRC
Similar to TRC, DRC also supports Existential (∃) and Universal (∀) quantifiers to refine queries.
Example:
Let’s say we want to find employees who earn more than 50,000 and work in the HR
department. Using quantifiers, we can achieve this as follows:
DRC Query:
{EMP_ID, NAME | ∃(EMP_ID, NAME, SALARY, DEPARTMENT) ∈ EMPLOYEE (SALARY > 50,000 AND DEPARTMENT = 'HR')}
Result:
EMP_ID |
NAME |
101 |
Alice |
103 |
Charlie |
Key Differences Between TRC and DRC
Feature |
Tuple Relational Calculus (TRC) |
Domain Relational Calculus (DRC) |
Query Basis |
Works with tuples (rows) in the relation |
Works with attributes (columns) in the relation |
Notation |
{T |
P(T)} |
Example Result |
Retrieves tuples from a relation |
Retrieves specific attributes from a relation |
Quantifiers |
Supports ∃ (Existential) and ∀ (Universal) quantifiers |
Also supports ∃ (Existential) and ∀ (Universal) quantifiers |
Conclusion
Relational calculus, whether in the form of Tuple Relational Calculus (TRC) or Domain Relational Calculus (DRC), is an essential aspect of non-procedural querying in DBMS. While TRC focuses on tuples, DRC works with attribute values. Both types use logical predicates and quantifiers to define what data to retrieve, allowing for flexible and precise queries. Understanding these concepts is crucial for those looking to master relational databases and develop efficient queries.
Whether you're querying tuples or domains, relational calculus helps ensure you get the data you need without worrying about how the database retrieves it.