Categories: DBMS

Relational Calculus

A non-procedural query language called relational calculus is used to retrieve data from relational databases. Thanks to this mathematical based system, users can describe what they want to retrieve from a database without specifying how to do so.

Note: There are two types of quantifiers:

  • Universal Quantifiers: The universal quantifier, denoted by ∀, is read as for all, which denotes that in a given set of tuples, precisely all tuples satisfy a particular condition.
  • Existential Quantifiers: The existential quantifier denoted by ∃ is read as for all, indicating that there is at least one instance in a given set of tuples whose value satisfies a particular condition.

Relational Calculus exists in two forms:

  • Tuple Relational Calculus (TRC)
  • Domain Relational Calculus (DRC)
Tuple Relational Calculus (TRC):

Data from relational databases can be retrieved using the non-procedural query language tuple relational calculus (TRC). Users can describe what they want to retrieve from a database without stating how to do so thanks to this system, which is based on the idea of sets of tuples.

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

Where

T is the resulting tuples

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

For instance,

{ T.name | Batch(T) AND T.roll = 126 }    

Output:

This query selects the tuples from the Batch relation. It returns a tuple with ‘name’ from Batch whose roll number is 126.

Domain Relational Calculus (DRC):

Domain relational calculus is the name for the second type of relation. The domain of attributes is used by the filtering variable in domain relational calculus. The same operators in tuple calculus are also used in domain relational calculus. It makes use of the logical connectives (and), (or), and (not). The variable is bound using existential () and universal quantifiers (). A query language related to domain relational calculus is called QBE, or Query by Example.

The Domain relational calculus expression syntax:

{<x1,x2,x3,x4...> \| P(x1,x2,x3,x4...)} 

where,

<x1,x2,x3,x4…> are domain variables used to get the column values required, and P(x1,x2,x3…) is a predicate expression or condition.

For example:

{< name, roll, marks > |  ∈ Result ∧ subject = 'database'}  

Output:

This query will yield the name, roll, and marks from the relation Result, where the subject is database.

Note: also read about What is Relational Algebra?

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago