0% found this document useful (0 votes)
37 views45 pages

Lecture 03 Relational ALgebra and Relational Calculus

This document discusses relational algebra and relational calculus. It defines selection, projection, Cartesian product, union, set difference, intersection and other relational algebra operations. It provides examples of how to write queries using these operations on sample relations from a property rental database. The document also discusses theta joins, equijoins, natural joins and outer joins.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views45 pages

Lecture 03 Relational ALgebra and Relational Calculus

This document discusses relational algebra and relational calculus. It defines selection, projection, Cartesian product, union, set difference, intersection and other relational algebra operations. It provides examples of how to write queries using these operations on sample relations from a property rental database. The document also discusses theta joins, equijoins, natural joins and outer joins.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Lecture 03:

RELATIONAL ALGEBRA AND


RELATIONAL CALCULUS

1
Objectives
• The meaning of the term ‘relational completeness’.
• How to form queries in the relational algebra.
• How to form queries in the tuple relational calculus.
• How to form queries in the domain relational calculus.
• The categories of relational Data Manipulation
Languages (DMLs).

2
The Relational Algebra
• The relational algebra is a theoretical language
• The five fundamental operations in relational algebra, are
Selection, Projection, Cartesian product, Union, and Set
difference.
• The basic operations are also the Join, Intersection, and
Division operations.
– The Selection and Projection operations are unary operations,
since they operate on one relation
– The other operations work on pairs of relations and are
therefore called binary operations.
Example:
– let R and S be two relations defined over the attributes
• A = (a1, a2, . . . , an) and B = (b1, b2, . . . , bm).

3
Unary Operations
P Q PQ
a 1 a 1
b 2 a 2
3 = a 3
b 1
b 2
b 3
(a) Selection (b) Projection (c) Cartesian product

RS PQ P-Q

R R R

S S S

(d) Union (e) Intersection (f) Set difference


4
Unary Operations
T U T U T >BU T CU

A B B C A B C A B A B C
a 1 1 x a 1 x a 1 a 1 x
b 2 1 y a 1 y a 1 y
3 z (h) Semijoin b 2
(g) Natural join
(g) Left Outer join

R S RS V W VW
B C B A
a 1 1 a
a 2 2 b
b 1
b 2
c 1
Example of Division
Remainder
(j) Division (shared area) 5
Example
• Instance of the DreamHome rental database.

6
Example

7
Example
Branch (branchNo, street, city, postcode)
Staff (staffNo, fName, lName, position, sex, DOB, salary, branchNo)
PropertyForRent (propertyNo, street, city, postcode, type, rooms, rent,
ownerNo, staffNo, branchNo)
Client (clientNo, fName, lName, telNo, prefType, maxRent)
PrivateOwner (ownerNo, fName, lName, address, telNo)
Viewing (clientNo, propertyNo, viewDate, comment)
Registration (clientNo, branchNo, staffNo, dateJoined)

8
Example
Selection (or Restriction)
σspredicate(R): The Selection operation works on a single relation R
and defines a relation that contains only those tuples of R that
satisfy the specified condition (predicate).
– More complex predicates can be generated using the logical
operators ∧ (AND), ∨ (OR) and ~ (NOT).
• Selection operation
List all staff with a salary greater than £10,000.
σsalary > 10000(Staff)

9
Example
Projection
ς𝒂𝟏 …,𝒂𝒏(𝑹): The Projection operation works on a single
relation R & defines a relation that contains a
vertical subset of R, extracting the values of
specified attributes and eliminating duplicates.
• Projection operation
– Produce a list of salaries for all staff, showing only the staffNo, fName,
lName, and salary details.
staffNo, fName, lName, salary(Staff)

10
Set Operations
Union
R ∪ S: The union of two relations R and S defines a relation
that contains all the tuples of R, or S, or both R and S,
duplicate tuples being eliminated. R and S must be
union-compatible.
Example: Union operation
List all cities where there is either a branch office or a property
for rent.
city(Branch) ∪ city(PropertyForRent)

11
Set Operations
Set difference
R − S: The Set difference operation defines a relation
consisting of the tuples that are in relation R, but not
in S. R and S must be union-compatible.
Example: Set difference operation
List all cities where there is a branch office but no properties for
rent.
city(Branch) − city(PropertyForRent)

12
Set Operations
Intersection
R ∩ S: The Intersection operation defines a relation
consisting of the set of all tuples that are in both R
and S. R and S must be union-compatible.
– Note that we can express the Intersection operation in terms of the
Set difference operation: R ∩ S = R − (R - S)
Example: Intersection operation
List all cities where there is both a branch office and at least
one property for rent.
city(Branch) ∩ city(PropertyForRent)

13
Set Operations
Cartesian product
R × S: The Cartesian product operation defines a relation
that is the concatenation of every tuple of relation R
with every tuple of relation S.
Example: Cartesian product operation
List the names and comments of all clients who have viewed a
property for rent.
(clientNo, fName, lName(Client)) × (clientNo, propertyNo, comment(Viewing))

14
15
– List the names and comments of all clients who have viewed a
property for rent.
– Selection operation on this relation to extract those tuples
where Client.clientNo =Viewing.clientNo.
σClient.clientNo = Viewing.clientNo(( clientNo, fName, lName(Client))
× ( clientNo, propertyNo, comment(Viewing)))

16
Set Operations
Decomposing complex operations
We use the assignment operation, denoted by ←, to name the
results of a relational algebra operation

TempViewing(clientNo, propertyNo, comment) ←  clientNo, propertyNo, comment(Viewing)

TempClient(clientNo, fName, lName) ←  clientNo, fName, lName(Client)

Comment(clientNo, fName, lName, vclientNo, propertyNo, comment) ←


TempClient × TempViewing

Result ← sclientNo = vclientNo(Comment)

17
Set Operations
• Join Operations
– Theta join
– Equijoin (a particular type of Theta join)
– Natural join
– Outer join
– Semi-join.

18
Theta join (-join)
Theta join (-join)
R F S: The Theta join operation 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 (<, ≤, >, ≥, =, ≠).
R F S = σF (R × S)
Example: Equijoin operation
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))
or
Result ← TempClient TempClient.clientNo = TempViewing.clientNo TempViewing

19
Set Operations
Natural join
R S: The Natural join is 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: Natural join operation
List the names and comments of all clients who have viewed a property
for rent.
(clientNo, fName, lName(Client)) (clientNo, propertyNo, comment(Viewing))
or
Result ← TempClient TempViewing

20
Set Operations
Outer join
R S: The (left) Outer join is a join in which tuples from R
that do not have matching values in the common
attributes of S are also included in the result relation.
Missing values in the second relation are set to null.
Example: Left Outer join operation
Produce a status report on property viewings.
(propertyNo, street, city(PropertyForRent)) Viewing

21
Set Operations
Semijoin
R >F S: The Semijoin operation defines a relation that contains
the tuples of R that participate in the join of R with S.
R >F S = A(R F S) A is the set of all attributes for R
Example: Semijoin operation
List complete details of all staff who work at the branch in Glasgow.
Staff > Staff.branchNo = Branch branchNo.(σcity = ‘Glasgow’(Branch))

22
Set Operations
Division Operation
R ÷ S: The Division operation defines a relation over the
attributes C that consists of the set of tuples from R that
match the combination of every tuple in S.
T1 C(R)
T2  C((T1 × S) − R)
T  T1 − T2
Example: Division operation
Identify all clients who have viewed all properties with three rooms.
(clientNo, propertyNo(Viewing)) ÷ (propertyNo(σrooms = 3(PropertyForRent)))

(clientNo, propertyNo(Viewing)) propertyNo(σrooms = 3(PropertyForRent)) RESULT 23


Aggregation and Grouping Operations
Aggregate operations
ℑAL(R): Applies the aggregate function list, AL, to the relation R to define
a relation over the aggregate list. AL contains one or more
(<aggregate_function>, <attribute>) pairs.

The main aggregate functions are:


– COUNT – returns the number of values in the associated attribute.
– SUM – returns the sum of the values in the associated attribute.
– AVG – returns the average of the values in the associated attribute.
– MIN – returns the smallest value in the associated attribute.
– MAX – returns the largest value in the associated attribute.

24
Example
(a) How many properties cost more than £350 per month to rent?
ρR(myCount) ℑ COUNT propertyNo (σrent > 350 (PropertyForRent))

(b) Find the minimum, maximum, and average staff salary.


ρR(myMin, myMax, myAverage) ℑMIN salary, MAX salary, AVERAGE salary (Staff)

25
Grouping operation
GAℑAL(R): Groups the tuples of relation R by the grouping attributes, GA,
and then applies the aggregate function list AL to define a new
relation. AL contains one or more (<aggregate_function>,
<attribute>) pairs. The resulting relation contains the grouping
attributes, GA, along with the results of each of the aggregate
functions.
a1, a2, . . . , an ℑ <Ap ap>, <Aq aq>, . . . , <Az az> (R)
Example: Grouping operation
Find the number of staff working in each branch & the sum of their salaries.
ρR(branchNo, myCount, mySum) branchNo ℑ COUNT staffNo, SUM salary (Staff)

26
Summary of the Relational Algebra Operations
Operation Notation Function
Produces a relation that contains only those tuples of R that
Selection σpredicate(R)
satisfy the specified predicate.
Produces a relation that contains a vertical subset of R,
Projection a1, . . . , an(R) extracting the values of specified attributes and eliminating
duplicates.
Produces a relation that contains all the tuples of R, or S, or
Union R∪S both R and S, duplicate tuples being eliminated. R and S
must be union-compatible.
Produces a relation that contains all the tuples in R that are
Set difference R − S
not in S. R and S must be union-compatible.
Produces a relation that contains all the tuples in both R and
Intersection R∩S
S. R and S must be union-compatible.
Cartesian Produces a relation that is the concatenation of every tuple
R×S
product of relation R with every tuple of relation S.
Produces a relation that contains tuples satisfying the
Theta join R FS
predicate F from the Cartesian product of R and S.
27
Summary of the Relational Algebra Operations
Produces a relation that contains tuples satisfying the predicate
Equijoin R FS F(which only contains equality comparisons) from the Cartesian
product of R and S.
An Equijoin of two relations R and S over all common attributes x.
Natural join R S
One occurrence of each common attribute is eliminated.
(Left) Outer A join in which tuples from R that do not have matching values in
R S
join the common attributes of S are also included in the result relation.
Produces a relation that contains the tuples of R that participate
Semijoin R >F S
in the join of R with S satisfying the predicate F.
Produces a relation that consists of the set of tuples from R defined
Division R÷S over the attributes C that match the combination of every tuple in S,
where C is the set of attributes that are in R but not in S.
Applies the aggregate function list, AL, to the relation R to define a
Aggregate ℑAL(R) relation over the aggregate list. AL contains one or more
(<aggregate_function>, <attribute>) pairs.
Groups the tuples of relation R by the grouping attributes, GA, and
then applies the aggregate function list AL to define a new relation.
Grouping GAℑAL(R) AL contains one or more (<aggregate_function>, <attribute>) pairs.
The resulting relation contains the grouping attributes, GA, along
with the results of each of the aggregate functions. 28
The Relational Calculus
• If a predicate contains a variable, as in ‘x is a member of
staff’, there must be an associated range for x. When we
substitute some values of this range for x, the proposition
may be true; for other values, it may be false.
For example: if the range is the set of all people and we replace x by
John White, the proposition ‘John White is a member of staff’ is true.
If we replace x by the name of a person who is not a member of staff,
the proposition is false.
– If P is a predicate, then we can write the set of all x such that P is true
for x, as:
{x | P(x)}
We may connect predicates by the logical connectives ∧ (AND), ∨ (OR),
and ~ (NOT) to form compound predicates.

29
Tuple Relational Calculus
• The calculus is based on the use of tuple variables. A tuple
variable is a variable that is a variable whose only permitted
values are tuples of the relation. (The word ‘range’ here does not
correspond to the mathematical use of range, but corresponds to
a mathematical domain.)
For example:
– to specify the range of a tuple variable S as the Staff relation.
Staff(S)
– To express the query ‘Find the set of all tuples S such that F(S) is true’.
{S | F(S)}
– To express the query ‘Find the staffNo, fName, lName, position, sex,
DOB, salary, and branchNo of all staff earning more than £10,000’
{S | Staff(S) ∧ S.salary > 10000}
– S.salary means the value of the salary attribute for the tuple variable
S. To retrieve a particular attribute, such as salary, we would write:
{S.salary | Staff(S) ∧ S.salary > 10000}
30
The existential and universal quantifiers
• The existential quantifier ∃ (‘there exists’) is used in
formulae that must be true for at least one instance.
– “There exists a Branch tuple that has the same branchNo as the
branchNo of the current Staff tuple, S, and is located in London”
Staff(S) ∧ (∃B) (Branch(B) ∧ (B.branchNo = S.branchNo) ∧ B.city = ‘London’)

• The universal quantifier  (‘for all’) is used in statements


about every instance
– “For all Branch tuples, the address is not in Paris”
(B) (B.city ≠ ‘Paris’)

31
De Morgan’s laws
(∃X)(F(X)) ≡ ~(X)(~(F(X)))
(X)(F(X)) ≡ ~(∃X)(~(F(X)))
(∃X)(F1(X) ∧ F2(X)) ≡ ~(X)(~(F1(X)) ∨ ~(F2(X)))
(X)(F1(X) ∧ F2(X)) ≡ ~(∃X)(~(F1(X)) ∨ ~(F2(X)))

– “There are no branches with an address in Paris”


~(∃B) (B.city = ‘Paris’)
• Tuple variables that are qualified by  or ∃ are called bound variables,
otherwise the tuple variables are called free variables. The only free
variables in a relational calculus expression should be those on the left
side of the bar ( | ). For example, in the following query:
{S.fName, S.lName | Staff(S) ∧ (∃B) (Branch(B) ∧ (B.branchNo = S.branchNo)
∧ B.city = ‘London’)}
S is the only free variable and S is then bound successively to each tuple of Staff.
32
Expressions and formulae
• The formulae should be those sequences. An expression
in the tuple relational calculus has general form:
{S1.a1, S2.a2, . . . , Sn.an | F(S1, S2, . . . , Sm)} m≥n
where: S1, S2, . . . , Sn, . . . , Sm are tuple variables
each ai is an attribute of the relation over Si ranges,
F is a formula.

33
Expressions and formulae
• An atom has one of the following forms:
– R(Si), where Si is a tuple variable and R is a relation.
– Si.a1 θ Sj.a2, where Si and Sj are tuple variables, a1 is an
attribute of the relation over which Si ranges, a2 is an
attribute of the relation over which Sj ranges, and θ is one
of the comparison operators (<, ≤, >, ≥ , =, ≠); the
attributes a1 and a2 must have domains whose members
can be compared by θ.
– Si.a1 θ c, where Si is a tuple variable, a1 is an attribute of
the relation over which Si ranges, c is a constant from the
domain of attribute a1, and θ is one of the comparison
operators.
34
Expressions and formulae
• Formulae from atoms using the following rules:
– An atom is a formula.
– If F1 and F2 are formulae, so are their conjunction F1 ∧ F2,
their disjunction F1 ∨ F2, and the negation ~F1.
– If F is a formula with free variable X, then (∃X)(F) and
(1X)(F) are also formulae.

35
Example
Tuple relational calculus
(a) List the names of all managers who earn more than £25,000.
{S.fName, S.lName | Staff(S) ∧ S.position = ‘Manager’ ∧ S.salary > 25000}
(b) List the staff who manage properties for rent in Glasgow.
{S|Staff(S) ∧ (∃P)(PropertyForRent(P)∧(P.staffNo=S.staffNo) ∧ P.city = ‘Glasgow’)}

‘For each member of staff whose details be wanted to list, there


exists a tuple in the relation PropertyForRent for that member of staff
with the value of the attribute city in that tuple being Glasgow.’
Note: in this formulation of the query, there is no indication of a strategy
for executing the query – the DBMS is free to decide the operations
required to fulfil the request and the execution order of these
operations. On the other hand, the equivalent relational algebra
formulation would be: ‘Select tuples from PropertyForRent such
that the city is Glasgow and perform their join with the Staff
relation’, which has an implied order of execution.
36
Example
(c) List the names of staff who do not manage any properties.
{S.fName, S.lName | Staff(S) ∧ (~(∃P) (PropertyForRent(P) ∧
(S.staffNo = P.staffNo)))}
Using the general transformation rules:
{S.fName, S.lName | Staff(S) ∧ ((1P) (~PropertyForRent(P) ∨
~(S.staffNo = P.staffNo)))}
(d) 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’))}
note that: we can rephrase ‘clients who have viewed a property in
Glasgow’ as ‘clients for whom there exists some viewing
of some property in Glasgow’.
37
Example
(e) List all cities where there is either a branch office or a
property for rent.
{T.city | (∃B) (Branch(B) ∧ B.city = T.city) ∨ (∃P)
(PropertyForRent(P) ∧ P.city = T.city)}
(f) List all the cities where there is a branch office but no
properties for rent.
{B.city | Branch(B) ∧ (~(∃P) (PropertyForRent(P) ∧
B.city = P.city))}
(g) List all the cities where there is both a branch office
and at least one property for rent.
{B.city | Branch(B) ∧ ((∃P) (PropertyForRent(P) ∧
B.city = P.city))}
38
Domain Relational Calculus
• In the tuple relational calculus, variables is in range over
tuples in a relation or from domains of attributes.
{d1, d2, . . . , dn | F(d1, d2, . . . , dm)} m ≥ n
where d1, d2, . . . , dn, . . . , dm represent domain variables
F(d1, d2, . . . , dm) represents a formula composed of atoms,
• Each atom has one of the following forms:
– R(d1, d2, . . . , dn), R is a relation of degree n and each di is a
domain variable.
– di θ dj, where di and dj are domain variables and θ is one of the
comparison operators (<, ≤, >, ≥, =, ≠); the domains di and dj
must be compared by θ.
– di θ c, where di is a domain variable, c is a constant from the
domain of di, and θ is one of the comparison operators.
39
Domain Relational Calculus
• Using the following rules:
– An atom is a formula.
– If F1 and F2 are formulae, so are their conjunction F1 ∧ F2, their
disjunction F1 ∨ F2, and the negation ~F1.
– If F is a formula with domain variable X, then (∃X)(F) and (1X)(F)
are also formulae.

40
Example
Domain relational calculus
(∃d1, d2, . . . , dn) in place of (∃d1), (∃d2), . . . , (∃dn)
(a) 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)}
(b) List the staff who manage properties for rent in Glasgow.
{sN, fN, lN, posn, sex, DOB, sal, bN | (∃sN1, cty) (Staff(sN, fN, lN,
posn, sex, DOB, sal, bN) ∧ PropertyForRent(pN, st, cty, pc, typ, rms,
rnt, oN, sN1, bN1) ∧ (sN = sN1) ∧ cty = ‘Glasgow’)}
• This query can also be written as:
{sN, fN, lN, posn, sex, DOB, sal, bN | (Staff(sN, fN, lN, posn, sex,
DOB, sal, bN) ∧ PropertyForRent(pN, st, ‘Glasgow’, pc, typ, rms, rnt,
oN, sN, bN1))}
41
Example
(c) List the names of staff who currently do not manage any
properties for rent.
{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))))}
(d) List the names of clients who have viewed a property for
rent in Glasgow.
{fN, lN | (∃cN, cN1, pN, pN1, cty) (Client(cN, fN, lN, tel, pT,
mR) ∧ Viewing(cN1, pN1, dt, cmt) ∧ PropertyForRent(pN, st,
cty, pc, typ, rms, rnt, oN, sN, bN) ∧ (cN = cN1) ∧ (pN = pN1) ∧
cty = ‘Glasgow’)}

42
Example
(e) List all cities where there is either a branch office or a
property for rent.
{cty | (Branch(bN, st, cty, pc) ∨ PropertyForRent(pN, st1, cty,
pc1, typ, rms, rnt, oN, sN, bN1))}
(f) List all the cities where there is a branch office but no
properties for rent.
{cty | (Branch(bN, st, cty, pc) ∧ (~(∃cty1)
(PropertyForRent(pN,st1, cty1, pc1, typ, rms, rnt, oN, sN, bN1)
∧ (cty = cty1))))}
(g) List all the cities where there is both a branch office and at
least one property for rent.
{cty | (Branch(bN, st, cty, pc) ∧ (∃cty1) (PropertyForRent(pN,
st1, cty1, pc1, typ, rms, rnt, oN, sN, bN1) ∧ (cty = cty1)))}
43
Chapter Summary
• The relational algebra is a (high-level) procedural language
• The relational calculus is used to measure the selective power of relational
languages.
• The five fundamental operations in relational algebra, Selection, Projection,
Cartesian product, Union, Set difference, the Join, Intersection, and Division
operations perform most of the data retrieval operations.
• The relational calculus is a formal non-procedural language that uses
predicates. Two forms are tuple relational calculus and domain relational
calculus.
• In the tuple relational calculus, A tuple variable is a variable that ‘ranges
over’ a named relation.
• In the domain relational calculus, domain variables take their values from
domains of attributes rather than tuples of relations.
• The relational algebra is logically equivalent to a safe subset of the relational
calculus (and vice versa).
• Relational data manipulation languages are sometimes classified as
procedural or non-procedural, transform-oriented, graphical, fourth-
generation, or fifth-generation.
44
Thank you for your listen
End of lecture 03

45

You might also like