You are on page 1of 8

Relational Calculus

There is an alternate way of formulating queries known as Relational Calculus. Relational calculus
is a non-procedural query language. In the non-procedural query language, the user is concerned
with the details of how to obtain the end results. The relational calculus tells what to do but never
explains how to do. Most commercial relational languages are based on aspects of relational
calculus including SQL-QBE and QUEL.

Why it is called Relational Calculus?


It is based on Predicate calculus, a name derived from branch of symbolic language. A predicate
is a truth-valued function with arguments. On substituting values for the arguments, the function
result in an expression called a proposition. It can be either true or false. It is a tailored version
of a subset of the Predicate Calculus to communicate with the relational database.

Many of the calculus expressions involves the use of Quantifiers. There are two types of
quantifiers:
o Universal Quantifiers: The universal quantifier denoted by ∀ is read as for all which means that
in a given set of tuples exactly all tuples satisfy a given condition.
o Existential Quantifiers: The existential quantifier denoted by ∃ is read as for all which means that
in a given set of tuples there is at least one occurrences whose value satisfy a given condition.

Before using the concept of quantifiers in formulas, we need to know the concept of Free and
Bound Variables.

A tuple variable t is bound if it is quantified which means that if it appears in any occurrences a
variable that is not bound is said to be free.

Free and bound variables may be compared with global and local variable of programming
languages.
Types of Relational calculus:

1. Tuple Relational Calculus (TRC)


It is a non-procedural query language which is based on finding a number of tuple variables also
known as range variable for which predicate holds true. It describes the desired information
without giving a specific procedure for obtaining that information. The tuple relational calculus
is specified to select the tuples in a relation. In TRC, filtering variable uses the tuples of a relation.
The result of the relation can have one or more tuples.

Notation:

A Query in the tuple relational calculus is expressed as following notation

1. {T | P (T)} or {T | Condition (T)}

Where

T is the resulting tuples

P(T) is the condition used to fetch T.

For example:

1. { T.name | Author(T) AND T.article = 'database' }

Output: This query selects the tuples from the AUTHOR relation. It returns a tuple with 'name'
from Author who has written an article on 'database'.
TRC (tuple relation calculus) can be quantified. In TRC, we can use Existential (∃) and Universal
Quantifiers (∀).

For example:

1. { R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}

Output: This query will yield the same result as the previous one.

Tuple Relational Calculus (TRC) is a non-procedural query language used in


relational database management systems (RDBMS) to retrieve data from tables.
TRC is based on the concept of tuples, which are ordered sets of attribute values
that represent a single row or record in a database table.
TRC is a declarative language, meaning that it specifies what data is required from
the database, rather than how to retrieve it. TRC queries are expressed as logical
formulas that describe the desired tuples.
Syntax: The basic syntax of TRC is as follows:
{ t | P(t) }
where t is a tuple variable and P(t) is a logical formula that describes the
conditions that the tuples in the result must satisfy. The curly braces {} are used
to indicate that the expression is a set of tuples.
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 | Employees(t) ∧ t.Salary > 50000 }
In this query, the “Employees(t)” expression specifies that the tuple variable t
represents a row in the “Employees” table. The “∧” symbol is the logical AND
operator, which is used to combine the condition “t.Salary > 50000” with the table
selection.
The result of this query will be a set of tuples, where each tuple contains the Name
attribute of an employee who earns more than $50,000 per year.
TRC can also be used to perform more complex queries, such as joins and nested
queries, by using additional logical operators and expressions.
While TRC is a powerful query language, it can be more difficult to write and
understand than other SQL-based query languages, such as Structured Query
Language (SQL). However, it is useful in certain applications, such as in the formal
verification of database schemas and in academic research.
Tuple Relational Calculus is a non-procedural query language, unlike relational
algebra. Tuple Calculus provides only the description of the query but it does not
provide the methods to solve it. Thus, it explains what to do but not how to do it.
Tuple Relational Query
In Tuple Calculus, a query is expressed as
{t| P(t)}
where t = resulting tuples,
P(t) = known as Predicate and these are the conditions that are used to fetch t.
Thus, it generates a set of all tuples t, such that Predicate P(t) is true for t.
P(t) may have various conditions logically combined with OR (∨), AND (∧),
NOT(¬).
It also uses quantifiers:
∃ t ∈ r (Q(t)) = ”there exists” a tuple in t in relation r such that predicate Q(t) is true.
∀ t ∈ r (Q(t)) = Q(t) is true “for all” tuples in relation r.

2. Domain Relational Calculus (DRC)


The second form of relation is known as Domain relational calculus. In domain relational calculus,
filtering variable uses the domain of attributes. Domain relational calculus uses the same
operators as tuple calculus. It uses logical connectives ∧ (and), ∨ (or) and ┓ (not). It uses
Existential (∃) and Universal Quantifiers (∀) to bind the variable. The QBE or Query by example is
a query language related to domain relational calculus.

Notation:

1. { a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}


Where

a1,a2 areattributes
P stands for formula built by inner attributes

1. {< article, page, subject > | ∈ javatpoint ∧ subject = 'database'}

Output: This query will yield the article, page, and subject from the relational javatpoint, where
the subject is a database.

Domain Relational Calculus is a non-procedural query language equivalent in


power to Tuple Relational Calculus. Domain Relational Calculus provides only
the description of the query but it does not provide the methods to solve it. In
Domain Relational Calculus, a query is expressed as,
{ < x1, x2, x3, ..., xn > | P (x1, x2, x3, ..., xn ) }
where, < x1, x2, x3, …, xn > represents resulting domains variables and P (x1, x2, x3,
…, xn ) represents the condition or formula equivalent to the Predicate calculus.

Predicate Calculus Formula:


1. Set of all comparison operators
2. Set of connectives like and, or, not
3. Set of quantifiers
Example:
Table-1: Customer
Customer name Street City

Debomit Kadamtala Alipurduar

Sayantan Udaypur Balurghat

Soumya Nutanchati Bankura

Ritu Juhu Mumbai

Table-2: Loan
Loan number Branch name Amount

L01 Main 200

L03 Main 150

L10 Sub 90

L08 Main 60

Table-3: Borrower
Customer name Loan number

Ritu L01

Debomit L08

Soumya L03

Query-1: Find the loan number, branch, amount of loans of greater than or equal
to 100 amount.
{≺l, b, a≻ | ≺l, b, a≻ ∈ loan ∧ (a ≥ 100)}
Resulting relation:

Loan number Branch name Amount

L01 Main 200

L03 Main 150

Query-2: Find the loan number for each loan of an amount greater or equal to
150.
{≺l≻ | ∃ b, a (≺l, b, a≻ ∈ loan ∧ (a ≥ 150)}
Resulting relation:
Loan number

L01

L03

Query-3: Find the names of all customers having a loan at the “Main” branch and
find the loan amount .
{≺c, a≻ | ∃ l (≺c, l≻ ∈ borrower ∧ ∃ b (≺l, b, a≻ ∈ loan ∧ (b =
“Main”)))}
Resulting relation:

Customer Name Amount

Ritu 200

Debomit 60

Soumya 150

Note:
The domain variables those will be in resulting relation must appear before |
within ≺ and ≻ and all the domain variables must appear in which order they are
in original relation or table.
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.
1. 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.
2. 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.

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

You might also like