2. TUPLE RELATIONAL CALCULUS
A nonprocedural query language, where eachp q y g g ,
query is of the form
{t | P (t ) }
It is the set of all tuples t such that predicate P is
true for t
t is a tuple variable, t [A ] denotes the value of tuple t
on attribute A
t r denotes that tuple t is in relation r
P is a formula similar to that of the predicate P is a formula similar to that of the predicate
calculus
2
3. PREDICATE CALCULUS FORMULA
Set of attributes and constants
Set of comparison operators: (e.g., , , , , , )
Set of connectives: and (), or (v)‚ not ()
Implication (): x y, if x is true, then y is true
x y x v y
Set of quantifiers:
t r (Q (t )) “there exists” a tuple in t in relation r
such that predicate Q (t ) is truep Q ( )
t r (Q (t )) Q(t) is true “for all” tuples t in relation
r
3
5. EXAMPLE QUERIES
Find the loan number, branch name, and_ , _ ,
amount for loans of over Rs1200
{t| t loan t[amount]> 1200}
Fi d h l b f h l f Find the loan number for each loan of an amount
greater than Rs1200
{t | s loan (t [loan_number ] = s [loan_number ] { | ( [ _ ] [ _ ]
s [amount ] 1200)}
Find the names of all customers having a loan,
an account or both at the bankan account, or both at the bank
{t | s borrower ( t [customer_name ] = s
[customer_name ])
d i ( [ ] u depositor ( t [customer_name ] = u
[customer_name ])}
5
6. EXAMPLE QUERIES (2)
Find the names of all customers who have a loan
and an account at the bank
{t | s borrower ( t [customer_name ] = s
[customer name ])[customer_name ])
u depositor ( t [customer_name ] = u
[customer_name] )}
Fi d th f ll t h i l t Find the names of all customers having a loan at
the Patliputra branch
{t | s borrower (t [customer_name ] = s{ ( [ ]
[customer_name ]
u loan (u [branch_name ] = “Patliputra”
u [loan_number ] = s [loan_number ]))}
6
7. EXAMPLE QUERIES (3)
Find the names of all customers who have a loan
at the Patliputra branch, but no account at any
branch of the bank
{t | b (t [ t ] {t | s borrower (t [customer_name ] = s
[customer_name ]
u loan (u [branch_name ] = “Patliputra”
u [loan number ] = s [loan number ])) u [loan_number ] = s [loan_number ]))
not v depositor (v [customer_name ] =
t [customer_name ])}
7
8. EXAMPLE QUERIES (4)
Find the names of all customers having a loang
from the Patliputra branch, and the cities in
which they live
{t | l ( [b h ] “P tli t ” {t | s loan (s [branch_name ] = “Patliputra”
u borrower (u [loan_number ] = s [loan_number ]
t [customer_name ] = u [customer_name ]
v customer (u [customer name ] = v v customer (u [customer_name ] = v
[customer_name ]
t [customer_city ] = v
[customer city ])))}[customer_city ])))}
8
9. EXAMPLE QUERIES (5)
Find the names of all customers who have an
account at all branches located in branch_city =
“Dhanbad”:
{t | b h ( [b h it ] “Dh b d” {t | u branch (u [branch_city ] = “Dhanbad” v
account (v [branch_name] = u [branch_name ]
w depositor ( w[account_number ] = v
[account number ] ( t [customer name ] = w[account_number ] ( t [customer_name ] = w
[customer_name ])))}
9
10. SAFETY OF EXPRESSION
It is possible to write tuple calculus expressionsp p p
that generate infinite set
For example, { t | t r } results in an infinite set if
the domain of any attribute of relation r is infinitethe domain of any attribute of relation r is infinite
To guard against the problem, we restrict the set
of allowable expressions to safe expressions
10
11. SAFE EXPRESSION
Let’s consider an expression {t | P (t )} in thep { | ( )}
tuple relational calculus
dom(P): the set of all values referenced by P i.e., the
relations tuples or constants that appear in Prelations, tuples, or constants that appear in P
The expression is said to be safe if every
component of t appears from the dom(P)
E.g. { t |t r| t [A] = 5 true } is not safe --- it
defines an infinite set with attribute values that do
not appear in any relation or tuples or constants in P.pp y p
11
12. DOMAIN RELATIONAL CALCULUS
A nonprocedural query language equivalent inp q y g g q
power to the tuple relational calculus
Each query is an expression of the form:
{ x1, x2, …, xn | P (x1, x2, …, xn)}
x1, x2, …, xn represent domain variables
P represents a formula similar to that of the
predicate calculus
12
14. EXAMPLE QUERIES
Find the loan_number, branch_name, and
amount for loans of over Rs1200
{ l, b, a | l, b, a loan a > 1200}
Find the names of all customers who have a loan Find the names of all customers who have a loan
of over Rs1200
{ c | l, b, a ( c, l borrower l, b, a
loan a > 1200)} loan a > 1200)}
Find the names of all customers who have a loan
from the Patliputra branch and the loan amount:
{ c, a | l ( c, l borrower b ( l, b, a loan
b = “Patliputra”))}
{ c, a | l ( c, l borrower l, “ Patliputra”, a { ( p
loan)} 14
15. EXAMPLE QUERIES (2)
Find the names of all customers having a loan,g ,
an account, or both at the Patliputra branch:
{ c | l ( c, l borrower
b ( l b l b “P tli t ”)) b,a ( l, b, a loan b = “Patliputra”))
a ( c, a depositor b,n ( a,
b, n account b = “Patliputra”))}
15
16. EXAMPLE QUERIES (3)
Find the names of all customers who have an
account at all branches located in Dhanbad:
{ c | x,y,z ( x, y, z branch y = “Dhanbad”)
a b ( a x b account c a depositor)} a,b ( a, x, b account c,a depositor)}
16
17. SAFETY OF EXPRESSION
The expression:p
{ x1, x2, …, xn | P (x1, x2, …, xn )}
is safe if -
all values that appear in tuples of the expression
are values from dom (P )are values from dom (P )
17
18. EXPRESSIVE POWER OF LANGUAGES
The following languages are equivalentg g g q
The basic relational algebra (without extended
relational algebra operation)
The tuple relational calculus (restricted to safe The tuple relational calculus (restricted to safe
expression)
The domain relational calculus (restricted to safe
i )expression)
18
19. RELATIONAL ALGEBRA TO TRC
Let the following relation schemas be given:g g
r<A,B,C>
s<D,E,F>
Gi i i h l l i l Give an expression in the tuple relational
calculus that is equivalent to each of the
following:g
i. πA(r)
ii. σB=17(r)
iiiiii. r x s
iv. ΠA,F(σC=D(r x s))
19
20. RELATIONAL ALGEBRA TO DRC
Let R=(A,B,C) and let r1 and r2 both be relations( , , ) 1 2
on schema R. Give an expression in the DRC that
is equivalent to each of the following:
i Π ( )i. ΠA(r1)
ii. σ B=17(r1)
iii. r1∩ r2
iv. r1 – r2
v. ΠA,B(r1) ΠB,C(r2)
20