Relational Calculus in DBMS
Relational calculus, a non-procedural query language in database management systems, guides users on what data is needed without specifying how to obtain it. Commonly utilized in commercial relational languages like SQL-QBE and QUEL, relational calculus ensures a focus on desired data without delving into procedural details, promoting a more efficient and abstract approach to querying in relational databases.
What is Relational Calculus?
Before understanding Relational calculus in DBMS, we need to understand Procedural Language and Declarative Langauge.
- Procedural Language - Those Languages which clearly define how to get the required results from the Database are called Procedural Language. Relational algebra is a Procedural Language.
- Declarative Language - Those Language that only cares about What to get from the database without getting into how to get the results are called Declarative Language. Relational Calculus is a Declarative Language.
So Relational Calculus is a Declarative Language that uses Predicate Logic or First-Order Logic to determine the results from Database.
Types of Relational Calculus in DBMS
Relational Calculus is of Two Types:
- Tuple Relational Calculus (TRC)
- Domain Relational Calculus (DRC)
Tuple Relational Calculus (TRC)
Tuple Relational Calculus in DBMS uses a tuple variable (t) that goes to each row of the table and checks if the predicate is true or false for the given row. Depending on the given predicate condition, it returns the row or part of the row.
The Tuple Relational Calculus expression Syntax
Where t is the tuple variable that runs over every Row, and P(t) is the predicate logic expression or condition.
Let's take an example of a Customer Database and try to see how TRC expressions work.
Customer Table
Customer_id | Name | Zip code |
---|---|---|
1 | Rohit | 12345 |
2 | Rahul | 13245 |
3 | Rohit | 56789 |
4 | Amit | 12345. |
Example 1: Write a TRC query to get all the data of customers whose zip code is 12345.
TRC Query: {t \| t ∈ Customer ∧ t.Zipcode = 12345} or TRC Query: {t \| Customer(t) ∧ t[Zipcode] = 12345 }
Workflow of query - The tuple variable "t" will go through every tuple of the Customer table. Each row will check whether the Cust_Zipcode is 12345 or not and only return those rows that satisfies the Predicate expression condition.
The TRC expression above can be read as "Return all the tuple which belongs to the Customer Table and whose Zipcode is equal to 12345."
Result of the TRC expression above:
Customer_id | Name | Zip code |
---|---|---|
1 | Rohit | 12345 |
4. | Amit | 12345 |
Example 2: Write a TRC query to get the customer id of all the Customers.
TRC query: { t \| ∃s (s ∈ Customer ∧ s.Customer_id = t.customer_id) }
Result of the TRC Query:
Customer_id |
---|
1 |
2 |
3 |
4 |
Domain Relational Calculus (DRC)
Domain Relational Calculus uses domain Variables to get the column values required from the database based on the predicate expression or condition.
The Domain realtional calculus expression syntax:
where,
<x1,x2,x3,x4...> are domain variables used to get the column values required, and P(x1,x2,x3...) is predicate expression or condition.
Let's take the example of Customer Database and try to understand DRC queries with some examples.
Customer Table
Customer_id | Name | Zip code |
---|---|---|
1 | Rohit | 12345 |
2 | Rahul | 13245 |
3 | Rohit | 56789 |
4 | Amit | 12345 |
Example 1: Write a DRC query to get the data of all customers with Zip code 12345.
DRC query: {<x1,x2,x3> \| <x1,x2> ∈ Customer ∧ x3 = 12345 }
Workflow of Query: In the above query x1,x2,x3 (ordered) refers to the attribute or column which we need in the result, and the predicate condition is that the first two domain variables x1 and x2 should be present while matching the condition for each row and the third domain variable x3 should be equal to 12345.
Result of the DRC query will be:
Customer_id | Name | Zip code |
---|---|---|
1 | Rohit | 12345 |
4 | Amit | 12345 |
Example 2: Write a DRC query to get the customer id of all the customer.
DRC Query: { <x1> \| ∃ x2,x3(<x1,x2,x3> ∈ Customer ) }
Result of the above Query will be:
Customer_id |
---|
1 |
2 |
3 |
4 |
NOTE: The above Queries are written for both TRC and DRC so that they can be compared and can be corelated.
Conclusion
- Relational Calculus in DBMS tells us what we want from the database and not how to get that.
- Relational Calculus is a Declarative Language.
- TRC uses tuple variable and checks every Row with the Predicate expression condition.
- DRC uses domain variables and returns the required attribute or column based on the condition.
- For any requirement both, TRC and DRC can be written.
- TRC and DRC queries can give more than one tuple or attribute in the result.