Relational Calculus
Relational Calculus
SUMA . R
Relational Calculus
● It is a Declarative Language that uses Predicate Logic or
First-Order logic to determine the results from database.
● Procedural Language - it clearly define how to get the required
results from the database. Relational Algebra is a Procedural
Language.
● Declarative Language - it only cares about what to get from the
database without getting into how to get the results. Relational
Calculus is a declarative language.
2
Types of Relational Calculus in DBMS
Two types
1. Tuple Relational Calculus
2. Domain Relational Calculus
3. Tuple Relational Calculus (TRC) - uses a tuple variable (t) that goes
to each row of the table and checks if the predicate is true or false
for the given row. Depending on the given predicate condition, it
returns the row or part of row. It describes information without
giving a specific procedure for obtaining the information.
3
● A query in TRC is expressed as
{ t \ | P(t) }
Here t is the tuple variable that runs over every row and P(t) is the
predicate logic expression or condition.
Customer Table
Customer id Name Zipcode
1 Ram 12356
2 Krishna 13245
3 Kiran 16578
4 Gopi 12356
4
Example - Write a TRC query to get all the data of customers whose zip
code is 12356. ∃ - there exist
Query - { t \| t ∈ Customer ∧ t.Zipcode = 12356 } or ∈ - is an element
{ t \| Customer(t) ∧ t[Zipcode] = 12356 } ∧ - and
The tuple variable t will go through every tuple of the Customer table. Each row
will check whether the Zipcode is 12356 or not and only return rows that
satisfies the predicate expression condition.
Resultant Query
Customer id Name Zipcode
1 Ram 12356
4 Gopi 12356
5
Example - Write a TRC query to get the customer id of all customers
Query - { t \| ∃s (s ∈ Customer ∧ s.Customerid = t.Customerid}
Resultant Query
Customer id
1
2
3
4
6
Domain Relational Calculus (DRC)
● It uses domain variables to get the column values required from
the database based on the predicate expression or condition.
● This language uses domain variables that take on values from an
attribute’s domain.
● A query in DRC is expressed as
{ <x1,x2,x3,.....,xn> | P (x1,x2,x3,........,xn) }
Here x1,x2,x3 ….. Xn represent domain variables. P
(x1,x2,x3,...,xn) represents a predicate expression or condition.
7
Customer Table
Customer id Name Zipcode
1 Ram 12356
2 Krishna 13245
3 Kiran 16578
4 Gopi 12356
Example - Write a DRC query to get the data of all customers with zip
code 12356.
Query - { <x1,x2,x3>\| <x1,x2> ∈ Customer ∧ x3 = 12356 }
Here x1,x2,x3 are attributes or columns which need in the result and the
predicate condition is that the first 2 domain variables x1 and x2 should
be present while matching the condition for each row and the third 8
domain variable x3 should be equal to 12356.
Resultant Query
Customer id Name Zipcode
1 Ram 12356
4 Gopi 12356
Example - Write a DRC query to get the customer id of all customers
Query - : { <x1> \| ∃ x2,x3(<x1,x2,x3> ∈ Customer ) }
¬ - not Resultant Query Customer id
∃ - there exist 1
∈ - is an element 2
∨ - or 3
∧ - and 4 9
THANK YOU