CH_17 Relational Calculus in DBMS

by Jasleen Chhabra | Updated on 29 September 2024

Relational Calculus in DBMS

In the world of databases, there are different ways to query and retrieve data. One of these approaches is Relational Calculus, a non-procedural query language used in Database Management Systems (DBMS). Unlike procedural query languages (like SQL), which require specific instructions on how to retrieve data, relational calculus only specifies what to retrieve, leaving the system to figure out how to do it.

Several commercial query languages like SQL, QBE (Query By Example), and QUEL are inspired by aspects of relational calculus. In this blog, we will discuss the key concepts of relational calculus, its types, and use cases, along with examples in tabular form.


Why is it Called Relational Calculus?

Relational calculus is based on Predicate Calculus, a part of symbolic logic. In predicate calculus, expressions are used to describe logical statements, which can either be true or false based on the values substituted for variables. Relational calculus applies a tailored version of this concept to interact with relational databases.

When using relational calculus, two common quantifiers are often employed:

  1. Universal Quantifier ( ∀ ): This means "for all." It indicates that all tuples in a given set must satisfy a particular condition.
  2. Existential Quantifier ( ∃ ): This means "there exists." It shows that at least one tuple in the given set satisfies the specified condition.

To better understand these, we also need to be familiar with the terms Free Variables and Bound Variables:

  • A variable is considered bound if it is quantified (i.e., linked to either ∀ or ∃).
  • A free variable is not linked to any quantifier, much like global variables in programming.

Types of Relational Calculus

Relational calculus is divided into two main types:

  1. Tuple Relational Calculus (TRC)
  2. Domain Relational Calculus (DRC)

Let’s explore these concepts in more detail.


1. Tuple Relational Calculus (TRC)

Tuple Relational Calculus (TRC) is a non-procedural query language where we work with tuples (rows) of a relation. In TRC, we express the desired result by specifying conditions, without defining how to obtain the result. TRC works with tuple variables (sometimes called range variables) and applies logical predicates to fetch the required tuples.

Notation:

A query in TRC follows this notation:

{T | P(T)}

  • T represents the resulting tuples.
  • P(T) represents the condition or predicate used to filter those tuples.

Example:

Suppose we have an EMPLOYEE table with EMP_ID, NAME, and SALARY attributes. We want to retrieve the names of employees who earn a salary greater than 50,000.

EMP_ID NAME SALARY
101 Alice 60,000
102 Bob 45,000
103 Charlie 55,000

TRC Query:

{T.NAME | EMPLOYEE(T) AND T.SALARY > 50,000}

Result:

NAME
Alice
Charlie

This query fetches the names of employees earning more than 50,000 from the EMPLOYEE table.


Using Quantifiers in TRC

TRC allows the use of Existential (∃) and Universal (∀) quantifiers to refine queries further.

Example:

We can use quantifiers to find employees who earn more than 50,000 and also work in the HR department. Suppose we add a DEPARTMENT column to our EMPLOYEE table:

EMP_ID NAME SALARY DEPARTMENT
101 Alice 60,000 HR
102 Bob 45,000 IT
103 Charlie 55,000 HR

TRC Query:

{T.NAME | ∃T ∈ EMPLOYEE (T.SALARY > 50,000 AND T.DEPARTMENT = 'HR')}

Result:

NAME
Alice
Charlie

This query retrieves the names of employees in the HR department who earn more than 50,000.


2. Domain Relational Calculus (DRC)

Domain Relational Calculus (DRC) is another type of non-procedural query language. Instead of working with tuples, DRC works with individual attribute values. In DRC, conditions are applied to domains (i.e., attributes of the table), and the result consists of specific attributes that satisfy those conditions.

Notation:

A query in DRC is expressed as:

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

  • a1, a2, ... are attributes of the relation.
  • P represents the condition or predicate applied to the attributes.

Example:

Consider the same EMPLOYEE table. Now, let’s write a DRC query to find the EMP_ID and NAME of employees who earn a salary greater than 50,000.

DRC Query:

{EMP_ID, NAME | EMPLOYEE(EMP_ID, NAME, SALARY) AND SALARY > 50,000}

Result:

EMP_ID NAME
101 Alice
103 Charlie

This query returns the EMP_ID and NAME of employees whose salary exceeds 50,000.


Using Quantifiers in DRC

Similar to TRC, DRC also supports Existential (∃) and Universal (∀) quantifiers to refine queries.

Example:

Let’s say we want to find employees who earn more than 50,000 and work in the HR department. Using quantifiers, we can achieve this as follows:

DRC Query:

{EMP_ID, NAME | ∃(EMP_ID, NAME, SALARY, DEPARTMENT) ∈ EMPLOYEE (SALARY > 50,000 AND DEPARTMENT = 'HR')}

Result:

EMP_ID NAME
101 Alice
103 Charlie

 


Key Differences Between TRC and DRC

Feature Tuple Relational Calculus (TRC) Domain Relational Calculus (DRC)
Query Basis Works with tuples (rows) in the relation Works with attributes (columns) in the relation
Notation {T P(T)}
Example Result Retrieves tuples from a relation Retrieves specific attributes from a relation
Quantifiers Supports ∃ (Existential) and ∀ (Universal) quantifiers Also supports ∃ (Existential) and ∀ (Universal) quantifiers

Conclusion

Relational calculus, whether in the form of Tuple Relational Calculus (TRC) or Domain Relational Calculus (DRC), is an essential aspect of non-procedural querying in DBMS. While TRC focuses on tuples, DRC works with attribute values. Both types use logical predicates and quantifiers to define what data to retrieve, allowing for flexible and precise queries. Understanding these concepts is crucial for those looking to master relational databases and develop efficient queries.

Whether you're querying tuples or domains, relational calculus helps ensure you get the data you need without worrying about how the database retrieves it.


FAQ

Any Questions?
Look Here.

Related Articles

CH_01 Database Management Systems (DBMS)

CH_02 DBMS Architecture

CH_03 DBMS Data Models

CH_04 Difference between DBMS and RDBMS

CH_05 DBMS Data Schemas and Data Independence

CH_06 Database Languages in DBMS

CH_07 ACID Properties in DBMS

CH_08 ER (Entity-Relationship) Diagrams in DBMS

CH_09 Cardinality in DBMS

CH_10 Keys in DBMS

CH_11 Generalization, Specialization, and Aggregation in DBMS

CH_12 Relational Model in DBMS

CH_13 Operations on Relational Model in DBMS

CH_14 Relational Algebra in DBMS

CH_15 Join Operations in DBMS

CH_16 Integrity Constraints in DBMS

CH_18 Anomalies in DBMS

CH_19 Normalization in DBMS

CH_20 Transaction Management in DBMS

CH_21 ACID Properties in DBMS

CH_22 Concurrency Control in DBMS

CH_23 Data Backup and Recovery in DBMS

CH_24 Storage System in DBMS