# Relational Calculus in DBMS

**Challenge Inside! :**Find out where you stand! Try quiz, solve problems & win rewards!

## Overview

You would have learned SQL in Databases. Do you know the mathematical foundation of SQL? The **mathematical foundation of SQl is based upon Relational Algebra and Relational Calculus**. The Tuple Relational Calculus and Domain Relational Calculus are the two ways we can write Relational Calculus Queries, which are explained in this Article.

success

- In this article, we will
**understand how to write Relational Calculus Queries**and get the desired results from the Query. - The article explains how to read and understand the
**TRC and DRC queries**with suitable examples. :::

## Introduction

Relational Calculus in database management system (DBMS) is all about * "What you want ?"*. Relational calculus does not tell us how to get the results from the Database, but it just cares about what we want.

The theory of Relational calculus was introduced by computer scientist and mathematician **Edgar Codd**. Let's deep dive and try to understand Relational calculus.

## 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.