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.
Procedural Language
Declarative Language
These languages define how to get the results from the database.
These languages define what to get from the database.
An example of procedural language is Relational Algebra.
An example of Declarative Language is Relational Calculus.
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.
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
Ninja_Name
Course_Enrolled
Age
Ninja_1
Java
18
Ninja_5
C++
16
Ninja_3
Web development
20
Ninja_4
C++
17
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
Ninja_Name
Course_Enrolled
Age
Ninja_1
Java
18
Ninja_3
Web development
20
Ninja_4
C++
17
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
Ninja_Name
Ninja_5
Ninja_4
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
Ninja_id
Gender
Age
CN_1
M
18
CN_2
F
16
CN_3
F
20
CN_4
M
17
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
Ninja_id
Gender
Age
CN_1
M
18
CN_4
M
17
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
Ninja_id
CN_2
CN_4
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.
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.
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.
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.
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.
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.
It is a declarative language which only focuses on what to retrieve rather than how to retrieve.
It does not provide a mechanism which optimizes the queries automatically and also it does not support recursive queries.
It may not be suitable for dealing with semi-structured or non relational data such as graph data or JSON documents.
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:
Tuple Relational Calculus: Specifies what data to retrieve using a tuple variable and a formula limiting its values.
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.
To learn more about relational calculus and relational algebra, you can refer to the below-mentioned articles:
We hope this article has helped you understand relational calculus in DBMS and its types. If this article helped you in any way, then you can read more such articles on our platform, Code360. You will find articles on almost every topic on our platform. Also, for cracking good product-based companies, you can practise coding questions at Coding Ninjas. You can also consider our Database Management Course to give your career an edge over others.