![]() |
VOOZH | about |
Tuple Relational Calculus (TRC) is a non-procedural query language used to retrieve data from relational databases by describing the properties of the required data (not how to fetch it). It is based on first-order predicate logic and uses tuple variables to represent rows of tables.
Syntax: The basic syntax of TRC is as follows:
{ t | P(t) }
For example, let's say we have a table called "Employees" with the following attributes:
| Employee ID |
| Name |
| Salary |
| Department ID |
To retrieve the names of all employees who earn more than $50,000 per year, we can use the following TRC query:
{ t.Name | Employees(t) β§ t.Salary > 50000 }
Explanation:
TRC is non-procedural - it specifies what data to retrieve, not how to retrieve it.
While expressive, TRC is more abstract and mainly used in academic or theoretical contexts, not practical database systems.
In Tuple Calculus, a query is expressed as
{t| P(t)}
P(t) may have various conditions logically combined with OR (β¨), AND (β§), NOT(Β¬).
It also uses quantifiers:
Domain Relational Calculus is similar to Tuple Relational Calculus, where it makes a list of the attributes that are to be chosen from the relations as per the conditions.
{<a1,a2,a3,.....an> | P(a1,a2,a3,.....an)}
where a1,a2,...an are the attributes of the relation and P is the condition.
Table Customer
| Customer name | Street | City |
|---|---|---|
| John | A7 | Chicago |
| Emma | B6 | Houston |
| Olivia | D9 | Dallas |
| Sophia | A5 | Chicago |
Table Branch
| Branch name | Branch City |
|---|---|
| ABC | Chicago |
| DEF | Dallas |
| GHI | Houston |
Table Account
| Account number | Branch name | Balance |
|---|---|---|
| 1111 | ABC | 50000 |
| 1112 | DEF | 10000 |
| 1113 | GHI | 9000 |
| 1114 | ABC | 7000 |
Table Loan
| Loan number | Branch name | Amount |
|---|---|---|
| L33 | ABC | 10000 |
| L35 | DEF | 15000 |
| L49 | GHI | 9000 |
| L98 | DEF | 65000 |
Table Borrower
| Customer name | Loan number |
|---|---|
| John | L33 |
| Emma | L49 |
| Sophia | L98 |
Table Depositor
| Customer name | Account number |
|---|---|
| John | 1111 |
| Emma | 1113 |
| Olivia | 1114 |
Example 1: Find the loan number, branch, and amount of loans greater than or equal to 10000 amount.
{t| t β loan β§ t[amount]>=10000}
Resulting relation:
| Loan number | Branch name | Amount |
|---|---|---|
| L33 | ABC | 10000 |
| L35 | DEF | 15000 |
| L98 | DEF | 65000 |
In the above query, t[amount] is known as a tuple variable.
Example 2: Find the loan number for each loan of an amount greater or equal to 10000.
{ t[loan-number] | t β loan β§ t[amount] β₯ 10000 }
Resulting relation:
| Loan number |
|---|
| L33 |
| L35 |
| L98 |
It retrieves the loan numbers of all tuples in the loan relation where the amount is greater than or equal to 10000.
Example 3: Find the names of all customers who have a loan and an account at the bank.
{ t.customer_name |
β s β borrower (t.customer_name = s.customer_name)
β§ β u β depositor (t.customer_name = u.customer_name) }
Resulting relation:
| Customer name |
|---|
| John |
| Emma |
The query retrieves the names of customers who appear in both borrower and depositor relations, meaning they have both a loan and an account.
Example 4: Find the names of all customers having a loan at the "ABC" branch.
{ t[customer-name] | β s β borrower (t[customer-name] = s[customer-name]) β§ β u
β depositor (t[customer-name] = u[customer-name]) }
Resulting relation:
| Customer name |
|---|
| John |
Query retrieves the names of customers who have a loan (via borrower relation) and are linked to the specified branch, returning customers like Saurabh.
Feature | TRC | Relational Algebra |
|---|---|---|
Type | Non-procedural | Procedural |
Focus | What to retrieve | How to retrieve |
Expression Style | Logical expressions | Set-based operators |
Execution | Abstract, not directly executable | Directly convertible to query |
Use in DBMS | Theoretical foundation | Basis for query execution |