4. What is Relational calculus?
Relational calculus is a Non-Procedural query
language (Declarative 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.
5. Why it is called Relational Calculus?
It uses mathematical Predicate Calculus (first-order logic).
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.
Calculus has variables, constants, comparison operator, logical
connectives and quantifiers.
Expressions in the calculus are called formulas.
7. Relational Calculus
Tuple Relational
Calculus
• Proposed by Codd in the year 1972.
• Works on tuples (row).
• Example - SQL
Domain Relational
Calculus
• Proposed by Lacroix & Pirotte in the
year 1977.
• Works on domain of attributes
(columns).
• Example - QBE
9. Tuple Relational calculus is a Non-Procedural query language.
It is used for selecting the tuples in a relation that satisfy the given
condition (or predicate). The result of the relation can have one or
more tuples.
A query in TRC is expressed as:
{ T | P (T) } or { T | Condition (T) }
Where
T is the resulting tuples
P(T) is the condition used to fetch T.
10. Predicate Calculus Formula
01
02
03
Set of attributes and constants
Set of comparison operators eg: < , = , >
Set of connectives eg: ∧ (and), ∨ (or) and ┓ (not)
04
05
Implication eg: x => y, if x is true, then y is true
Quantifiers eg: Existential (∃) and Universal Quantifiers (∀)
11. Free and Bound variables
The use of quantifiers ∃x and ∀x in a formula is
said to bind x in the formula.
A variable that is not bound is free.
The variable ‘ t ’ that appears to the left of ‘|’ must
be the only free variable in the formula P(T).
All the other tuple variables must be bound using
a quantifier.
{ T | P (T) }
12. Example Queries: TRC
o Find the branch–name, loan–number, and amount for loans of
over $1200 .
{ t | t ∈ Loan ∧ t[amount] > 1200 }
Branch (branch-name, branch-city, assets)
Customer (customer-name, customer-city)
Account (account-number, branch-name, balance)
Loan (loan-number, branch-name, amount)
Depositor (customer-name, account-number)
Borrower (customer-name, loan-number)
13. o Find the loan–number for each loan of an amount greater
than $1200.
{ t | ∃s ∈ Loan( t[ loan–number ] = s[ loan–number ]
∧ s[ amount > 1200 ] ) }
o Find the names of all customers having a loan at the Perryridge
branch.
{ t | ∃b ∈ Borrower( t[ customer-name ] = b[ customer–name]
∧ ∃s ∈ Loan ( s[ branch-name ] = “Perryridge”
∧ s[ loan–number ] = b[ loan–number ] )) }
14. o Find the names of all customers having a loan, an account, or
both at the bank.
{ t | ∃b ∈ Borrower( t[ customer-name ] = b[ customer–name] )
∨ ∃d ∈ Depositor( t[ customer-name ] = d[ customer–name] ) }
o Find the names of all customers who having a loan and an
account at the bank.
{ t | ∃b ∈ Borrower( t[ customer-name ] = b[ customer–name] )
∧ ∃d ∈ Depositor( t[ customer-name ] = d[ customer–name] ) }
16. Domain Relational calculus is a Non-Procedural query language.
DRC uses list of attributes to be selected from relation based on the
condition (or predicate). It is same as TRC but differs by selecting the
attributes rather than selecting whole tuples.
A query in DRC is expressed as:
{ <a1, a2, a3, ..., an> | P (a1, a2, a3, ... ,an) }
Where
a1, a2 … an are attributes (domain variables)
P stands for formula built by inner attributes similar to that of the
predicate calculus.
17. Predicate Calculus Formula
01
02
03
Set of domain variables and constants
Set of comparison operators eg: < , = , >
Set of connectives eg: ∧ (and), ∨ (or) and ┓ (not)
04
05
Implication eg: x => y, if x is true, then y is true
Quantifiers eg: Existential (∃) and Universal Quantifiers (∀)
∃ x (P(x)) and ∀ x (P(x))
x is a free domain variable instead of bind(opposite to that in TRC).
18. Example Queries: DRC
o Find the loan–number(l), branch–name(b) and amount(a) for
loans of over $1200.
{ < l, b, a >| < l, b, a > ∈ Loan ∧ a > 1200 }
Branch (branch-name, branch-city, assets)
Customer (customer-name, customer-city)
Account (account-number, branch-name, balance)
Loan (loan-number, branch-name, amount)
Depositor (customer-name, account-number)
Borrower (customer-name, loan-number)
19. o Find the loan–number for each loan of an amount greater
than $1200.
{ < l >| ∃ b, a (< l, b, a > ∈ Loan ∧ a > 1200 ) }
o Find the names of all customers who have a loan of over $1200.
{ < c >| ∃ l, b, a (< c, l > ∈ Borrower ∧ (< l, b, a > ∈ Loan ∧ a > 1200) ) }
o Find the names of all customers having a loan at the Perryridge
branch and find the loan amount.
{ < c, a >| ∃ l (< c, l > ∈ Borrower ∧ ∃ b (< l, b, a > ∈ Loan
∧ b = “Perryridge”) ) }