1.
Introduction
2.
What is Relational Calculus in DBMS?
3.
Why is it called Relational Calculus?
4.
Types of Relational Calculus in DBMS
4.1.
Tuple Relational Calculus (TRC)
4.2.
Domain Relational Calculus (DRC)
5.
Common Notations Used in Relational Calculus
6.
Use Cases of Relational Calculus in DBMS
7.
Limitations of Relational Calculus in DBMS
8.
8.1.
What is the difference between procedural and declarative language?
8.2.
What is TRC in DBMS?
8.3.
What is the role of Relational Calculus in DBMS?
8.4.
Where is domain relational calculus used?
8.5.
What is relational algebra in DBMS?
8.6.
What are the two types of relational calculus?
9.
Conclusion
Last Updated: Jun 21, 2024
Medium

Relational Calculus in DBMS

Jay Dhoot
1 upvote

Introduction

Do you know that there exists a mathematical foundation of SQL? The mathematical foundation of SQL is dependent upon Relational Algebra and Relational Calculus. This mathematical foundation came in 1970 and was given by Edgar Codd, regarded as the ‘Father of DBMS.’

Relational Calculus uses declarative Language, unlike Relational Algebra which uses Procedural Language. In this article, we will learn about relational calculus in DBMS and its types. We will also learn to write queries based on the two types of relational calculus with the help of examples.

What is Relational Calculus in DBMS?

Relational Calculus in DBMS is just another way of formulating queries. It is non-procedural and a declarative query language. Let’s first understand the difference between procedural and declarative query language.

Hence relational calculus is concerned with what to do rather than how to do it. Simply put, it only provides the information on the query description rather than the methods to do it.

Why is it called Relational Calculus?

It is called Relational Calculus because it is used to manipulate and describe relational data in DBMS using a mathematical and logical approach. The “relational” part refers to representing and organizing the data in a tabular form with the help of relations or tables. The “calculus” part signifies a system of calculation and reasoning.

Relational Calculus is based on predicate calculus, a truth-valued function with arguments. When the values of the arguments are substituted in the predicate calculus, the resulting expression is called a proposition which can be either true or false.

Relational calculus is a tailored version of a subset of Predicate Calculus, which helps communicate with the relational database.

Also read, File System vs DBMS

Types of Relational Calculus in DBMS

There are two types of Relational Calculus.

Tuple Relational Calculus (TRC)

It is a non-procedural query language with which we find tuples that hold true for a given condition. These given conditions are called predicates. Tuple Relational Calculus describes the desired information without providing specific information for obtaining that information, i.e., specifies ‘what’ but not ‘how.’

It goes to each table row and checks whether the predicate condition is satisfied (true) or not (false). It returns the tuple(s) which hold true for the predicate condition.

Syntax

``{t \| P(t)}  ``

Here, t represents the tuples returned as results, and P(t) is the predicate logic condition or expression.

*Note: TRC is used as a theoretical foundation for optimizing the queries. However, it cannot be executed in SQL-based RDBMS such as MySQL, SQLite, or PostgreSQL.

Example

Consider the following Ninja table

Query 1: Create a TRC query to get data of all the Ninja’s whose age>=17.

TRC Query

``{t | t ∈ Ninja (t) ^ t.Age >= 17}``

Explanation: The tuple variable (t) goes to every tuple of the Ninja table. Each row checks the age of the Ninjas, and only those tuples are returned in the result whose age is greater than or equal to 17.

The query can be interpreted as “Return all the tuples t which belong to table Ninja and have an age greater than or equal to 17.”

Result

Query 2: Create a TRC query to get names of all the Ninja’s who are enrolled in C++ course.

TRC Query

``{t| ∃ n ∈ Ninja(t.Ninja_Name = n.Ninja_Name ∧ t.Course_Enrolled =”C++”)}``

Explanation: The above query returns the Ninja_Name for all the tuples which have Course Enrolled as C++. The result is returned in the tuple t.

Result

Domain Relational Calculus (DRC)

The domain of the attributes is used in Domain Relational Calculus. It uses domain variables to get the column values needed based on predicate conditions.

Syntax:

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

Here, <x1,x2,...,xn> represents the domain variables used to get the column values, and P(x1,x2,...,xn) is the predicate which is the condition for results.

*Note: DRC is a theoretical foundation for manipulating and describing data in a relational database. However, it cannot be executed in SQL-based RDBMS such as MySQL, SQLite, or PostgreSQL.

Example

Consider the following Ninja table

Query 1: Create a DRC query to get data of all the Ninja’s whose Gender is M.

DRC Query:

``{<x1,x2,x3> \| <x1,x3> ∈ Ninja ∧ x2 = ‘M’ }``

Explanation: In the above query, x1(Ninja_id), x2(Gender), and x3(Age) represent the columns that are needed in the result. The predicate condition states that x1 and x3 should be present while x2 has to follow a matching condition for each row; that is, z2 should be equal to ‘M.’

Result

Query 2: Create a DRC query to get Ninja_id of all the Ninja’s whose Age<18.

DRC Query:

``{ <x1> \| ∈ Ninja ∧ x3 < 18 }``

Explanation: In the above query, x1(Ninja_id) represents the column needed in the result. The predicate condition states that x3 has to follow a matching condition for each row that x3 < 18.

Result

Common Notations Used in Relational Calculus

∧ - represents AND.

∨ - represents OR.

¬ - represents NOT.

∈ - represents Belongs to.

∀ - represents that in a set of tuples, all of them satisfy the condition (Universal Quantifier).

∃ - represents that in a set of tuples, there is at least one occurrence that satisfies the condition (Universal Quantifier).

Use Cases of Relational Calculus in DBMS

Below are the use cases of relational calculus in DBMS.

1. Relational Calculus helps in formulating complex queries which involves multiple tables, aggregations and conditions. It helps the users to retrieve the desired data from the database.

2. Relational Calculus also helps in data validation. It enables users to check whether data satisfies the conditions and also helps in identifying errors or inconsistencies.

3. Relational Calculus plays an important role in Database Modelling and Design. It helps in defining the schema, integrity constraints, primary and foreign key constraints, and other things required in database design.

4. It can perform advanced data analysis like sorting, filtering, aggregation and grouping. It helps users to get meaningful insights from large datasets.

Limitations of Relational Calculus in DBMS

Below are the limitations of relational calculus in DBMS.

1. It is not as user friendly as other query languages such as SQL. It lacks operations that make complex queries efficient and concise to write.

2. It is a declarative language which only focuses on what to retrieve rather than how to retrieve.

3. It does not provide a mechanism which optimizes the queries automatically and also it does not support recursive queries.

4. It may not be suitable for dealing with semi-structured or non relational data such as graph data or JSON documents.

Also see, Checkpoint in DBMS

What is the difference between procedural and declarative language?

Procedural Language defines how to get the results from the database, while Declarative Language defines what to get from the database. Relational Algebra is a procedural language, while Relational Calculus is a declarative language.

What is TRC in DBMS?

TRC, or Tuple Relational Calculus, is a type of relational calculus in DBMS. It is a non-procedural query language with which we find tuples that hold true for a given condition. Tuple Relational Calculus describes the desired information without providing specific information for obtaining that information, i.e., specifies ‘what’ but not ‘how.’

What is the role of Relational Calculus in DBMS?

Relational Calculus in DBMS is just another way of formulating queries. It is non-procedural and a declarative query language. It is concerned with what to do rather than how to do it.

Where is domain relational calculus used?

Domain relational calculus is a non-procedural query language employed by a database management system. Its focus is on what data is to be collected without telling the techniques used for its retrieval.

What is relational algebra in DBMS?

Relational algebra is a procedural query language used to retrieve and manipulate data stored in relational databases. It consists of a set of operations such as selection, projection, join, union, and intersection, which allow for the formulation and execution of queries to extract specific information from databases.

What are the two types of relational calculus?

Relational calculus provides a declarative approach to query execution. The two types are:

1. Tuple Relational Calculus: Specifies what data to retrieve using a tuple variable and a formula limiting its values.
2. Domain Relational Calculus: Specifies what data to retrieve using variables over attributes and a formula limiting their values.

Conclusion

Relational calculus in DBMS offers a declarative approach to query formulation, focusing on what data to retrieve rather than how to retrieve it. It provides two types: tuple relational calculus, which uses tuple variables and conditions, and domain relational calculus, which employs variables over attributes and conditions. This abstraction facilitates concise and flexible querying in relational databases.