Relational Algebra and Calculus
Relational Algebra and Calculus
Year : 2016
Bina Nusantara
INTRODUCTION
• predicate (R)
– Works on a single relation R and defines a relation that
contains only those tuples (rows) of R that satisfy the
specified condition (predicate).
• Example : List all staff with a salary greater than £10,000.
city(Branch) city(PropertyForRent)
Set Difference
• R–S
– Defines a relation consisting of the tuples that are in
relation R, but not in S.
– R and S must be union-compatible.
city(Branch) – city(PropertyForRent)
Intersection
• RS
– Defines a relation consisting of the set of all tuples that
are in both R and S.
– R and S must be union-compatible.
• Expressed using basic operations:
R S = R – (R – S)
• Example : List all cities where there is both a branch office
and at least one property for rent.
city(Branch) city(PropertyForRent)
Cartesian product
• RXS
– Defines a relation that
is the concatenation of
every tuple of relation
R with every tuple of
relation S.
• Example : List the names
and comments of all
clients who have viewed a
property for rent.
(clientNo, fName,
lName(Client)) X
(clientNo, propertyNo,
comment (Viewing))
Example - Cartesian product
and Selection
• Use selection operation to extract those tuples where Client.clientNo =
Viewing.clientNo.
Client.clientNo = Viewing.clientNo((clientNo, fName, lName(Client)) (clientNo,
propertyNo, comment(Viewing)))
musicbydesign.com
Theta join (-join)
• R FS
– Defines a relation that contains tuples satisfying the
predicate F from the Cartesian product of R and S.
– The predicate F is of the form R.ai S.bi where may be
one of the comparison operators (<, , >, , =, ).
• Can rewrite Theta join using basic Selection and Cartesian
product operations.
R FS = F(R S)
Degree of a Theta join is sum of degrees of the operand
relations R and S. If predicate F contains only equality (=),
the term Equijoin is used.
Example - Equijoin
• List the names and comments of all clients who have viewed a property for
rent.
(clientNo, fName, lName(Client)) Client.clientNo = Viewing.clientNo (clientNo, propertyNo,
comment(Viewing))
Natural join
• R S
– An Equijoin of the two relations R and S over all common
attributes x. One occurrence of each common attribute is
eliminated from the result.
• Example : List the names and comments of all clients who have viewed a
property for rent.
(clientNo, fName, lName(Client)) (clientNo, propertyNo, comment(Viewing))
The (Left) Outer join
• Display rows in the result that don’t have matching values in the join column
• R S
– (Left) outer join is join in which tuples from R that do not have matching
values in common columns of S are also included in result relation.
• Example : How many properties cost more than £350 per month to rent?
R(myCount) COUNT propertyNo (σrent > 350 (PropertyForRent))
Grouping Operation
• GA AL(R)
– Groups tuples of R by grouping attributes, GA, and then applies
aggregate function list, AL, to define a new relation.
– AL contains one or more (<aggregate_function>, <attribute>) pairs.
– Resulting relation contains the grouping attributes, GA, along with
results of each of the aggregate functions.
http://img.ehowcdn.com/article-new/ehow/images/a07/hq/43/group-students-classroom-800x800.jpg
Example – Grouping Operation
• Find the number of staff working in each branch and the sum of their
salaries.
R(branchNo, myCount, mySum)branchNo COUNT staffNo, SUM salary(Staff)
Relational Calculus
• Relational calculus query specifies what is to be retrieved rather
than how to retrieve it.
– No description of how to evaluate a query.
• In first-order logic (or predicate calculus), predicate is a truth-
valued function with arguments.
• When we substitute values for the arguments, function yields an
expression, called a proposition, which can be either true or
false.
• If predicate contains a variable (e.g. ‘x is a member of staff’),
there must be a range for x.
• When we substitute some values of this range for x, proposition
may be true; for other values, it may be false.
• When applied to databases, relational calculus has forms: tuple
and domain.
Tuple Relational Calculus
• Interested in finding tuples for which a predicate is true. Based on
use of tuple variables.
• Tuple variable is a variable that ‘ranges over’ a named relation: i.e.,
variable whose only permitted values are tuples of the relation.
• Specify range of a tuple variable S as the Staff relation as:
Staff(S)
• To find set of all tuples S such that F(S) is true:
{S | F(S)}
Examples :
• To find details of all staff earning more than £10,000:
{S | Staff(S) S.salary > 10000}
• To find a particular attribute, such as salary, write:
{S.salary | Staff(S) S.salary > 10000}
Tuple Relational Calculus
• Can use two quantifiers to tell how many instances the predicate
applies to:
– Existential quantifier $ (‘there exists’)
– Universal quantifier " (‘for all’)
• Tuple variables qualified by " or $ are called bound variables,
otherwise called free variables.
• Existential quantifier used in formulae that must be true for at least one
instance, such as:
Staff(S) ($B)(Branch(B) (B.branchNo = S.branchNo) B.city = ‘London’)
• Can also use ~($B) (B.city = ‘Paris’) which means ‘There are no
branches with an address in Paris’.
Tuple Relational Calculus
• List the names of all managers who earn more than £25,000.
{S.fName, S.lName | Staff(S)
S.position = ‘Manager’ S.salary > 25000}
• List the names of staff who currently do not manage any properties.
{S.fName, S.lName | Staff(S) (~($P) (PropertyForRent(P)(S.staffNo =
P.staffNo)))}
Or
{S.fName, S.lName | Staff(S) (("P) (~PropertyForRent(P)
~(S.staffNo = P.staffNo)))}
• List the names of clients who have viewed a property for rent in Glasgow.
{C.fName, C.lName | Client(C) (($V)($P) (Viewing(V) PropertyForRent(P)
(C.clientNo = V.clientNo) (V.propertyNo=P.propertyNo)
P.city =‘Glasgow’))}
Example - Tuple Relational Calculus
• To avoid this, add restriction that all values in result must be values in
the domain of the expression.
www.ahmadfahmy.com
Domain Relational Calculus
• If F(d1, d2, . . . , dn) stands for a formula composed of atoms and d1, d2, . . .
, dn represent domain variables, then:
{d1, d2, . . . , dn | F(d1, d2, . . . , dn)}
is a general domain relational calculus expression.
• Example : Find the names of all managers who earn more than
£25,000.
{fN, lN | ($sN, posn, sex, DOB, sal, bN)
(Staff (sN, fN, lN, posn, sex, DOB, sal, bN) posn = ‘Manager’ sal >
25000)}
36
Example - Domain Relational Calculus
{fN, lN | ($sN)
(Staff(sN,fN,lN,posn,sex,DOB,sal,bN)
(~($sN1) (PropertyForRent(pN, st, cty, pc, typ, rms, rnt, oN, sN1, bN1)
(sN=sN1))))}
Example - Domain Relational Calculus
• List the names of clients who have viewed a property for rent in
Glasgow.