## Tuple Relational Calculus in DBMS

Tuple Relational Calculus (TRC) is a formal language used to specify queries on a relational database. It is a non-procedural language that operates on a set of tuples (rows) in one or more relations (tables) and returns a set of tuples that satisfy a given condition. TRC expresses queries in terms of logical formulas, using variables to refer to tuples and conditions to restrict the result set. The variables can be constrained using selection and projection operators, and the conditions can be combined using logical operators such as AND, OR, and NOT.

- A tuple relational calculus is a non-procedural query language that specifies to select the tuples in a relation.
- It can select the tuples with a range of values or tuples for certain attribute values etc.
- The resulting relation can have one or more tuples.
- It uses first-order Logic to generate the result.
- All the conditions used in the tuple expression are called a well-formed formula – WFF.
- All the conditions in the expression are combined by using logical operators like AND, OR, and NOT, and quantifiers like ‘For All’ (?) or ‘there exists’ (?).
- If the tuple variables are all bound variables in a WFF is called a closed WFF.
- In an open WFF, we will have at least one free variable.

## Tuple Relational Query

A tuple relational query (TRQ) is a query language used to retrieve data from a relational database. It operates on a set of tuples (rows) in one or more relations (tables) and returns a set of tuples that meet the specified criteria.

TRQ is a declarative language, meaning that the user specifies what data they want and the database management system (DBMS) determines how to retrieve it. TRQ has a similar syntax to SQL (Structured Query Language), but the main difference is that TRQ operates on individual tuples rather than on sets of tuples.

**{t | P (t)}**or**{t | condition (t)} --**this is also known as expression of relational calculusWhere t is the resulting tuples, P(t) is the condition used to fetch t.

- P(t) is the first-order predicate logic to set the condition, and t is the tuple variable.

### Tuple Variables

There are basically two types of tuple variables

**Free Variables:**Any tuple variable without any ‘*For All’*or ‘*there exists’*condition is called**Free Variable in C**. In a simple manner, we can say that if the tuple variable is not bounded to quantifiers, then it is said to be a free variable.

Example:**{t | EMPLOYEE (t) AND t.DEPT_ID = 10}** – this selects all the tuples of employee names who work for Department 10.

**Bounded Variables:**Any tuple variable with the ‘*For All’*or ‘*there exists’*condition is called Bounded**Variable**. In a simple manner, we can say that if the tuple variable is bounded to quantifiers, then it is said to be a bounded variable.

Example: **{t | EMPLOYEE (t) and t.SALARY>10000}** - implies that it selects the tuples from EMPLOYEE relation such that resulting employee tuples will have salaries greater than 10000. This is an example of selecting a range of values.

### Unsafe Query

A TRC Query with an infinite set of tuples in the result is said to be an unsafe query. the reason for a query being unsafe is basically the use of a universal tuple variable. The domain of the universal tuple variable is not restricted to the relation for which it is getting used.

Example:{t| t ∉ Student}- this query will result in all the tuples which are not part of the student table which are infinite. hence the query is unsafe.

## Domain Relational Calculus(DRC)

As compared to tuple relational calculus, domain relational calculus makes use of the list of attributes that needs to be selected from the relation based on the condition. Domain relational calculus is the same as tuple relational calculus it only differs in the way of selecting the attributes instead of selecting whole tuples. It is denoted as below:

{< a_{1}, a_{2}, a_{3}, … a_{n} > | P(a_{1}, a_{2}, a_{3}, … a_{n})}

Where a_{1}, a_{2}, a_{3}, … a_{n} are attributes of the relation and P is the condition.

For e.g.: Select the EMP_NAME and EMP_ID of employees who work for Department 12

{<EMP_ID, EMP_NAME> | <EMP_ID, EMP_NAME> ? EMPLOYEE Λ DEPT_ID = 12}

Other concepts of Tuple Relational Calculus like a free variable, bound variable, WFF, etc. remain the same in Domain Relational Calculus too. The only difference is Domain Relational Calculus is based on attributes of the relation.

## Tuple Relational Calculus Examples

Tuple Relational Calculus (TRC) is a query language that is used to retrieve data from a relational database by specifying conditions that the desired tuples must satisfy. Here are a few examples of Tuple Relational Calculus:

- Retrieve all the employees who work in the "Sales" department.: {t | Employee(t) ∧ t.department = "Sales"}
- Retrieve the names of all the employees who earn more than $50,000.: {t.name | Employee(t) ∧ t.salary > 50000}
- Retrieve the names of all the employees who work in the "Sales" department and earn more than $50,000.: {t.name | Employee(t) ∧ t.department = "Sales" ∧ t.salary > 50000}

Other Important GATE Topics | |

2's Complement | Data Hazard |

Floating Point Representation | Asynchronous Counter |

User-defined Data Types in C | Full Subtractor |

## Comments

write a comment