0% found this document useful (0 votes)
6 views85 pages

IMS Lect10 12 RAlgebra

Uploaded by

Helios 7404
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)
6 views85 pages

IMS Lect10 12 RAlgebra

Uploaded by

Helios 7404
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/ 85

Relational Algebra and

Relational Calculus

© Pearson Education Limited 1995, 2005


Introduction
▪ Relational Algebra and Relational Calculus are
formal languages associated with the relational
model.
▪ Informally, relational algebra is a (high-level)
procedural language and relational calculus a
non-procedural language.
▪ However, formally both are equivalent to one
another.
Relational Algebra
• Relational algebra operations work on one or more
relations to define another relation without
changing the original relations.
• Both operands and results are relations, so output
from one operation can become input to another
operation.
• Allows expressions to be nested, just as in
arithmetic. This property is called closure.
Relational Algebra
• Five basic operations in relational algebra:
• Selection,
• Projection,
• Cartesian product,
• Union and
• Set Difference.
• These perform most of the data retrieval operations
needed.
• Other operations are Join, Intersection, and Division
operations, that can be expressed in terms of 5 basic
operations.
Relational Algebra Operations
Relational Algebra Operations
Relational Algebra Operations

i
Relational Algebra operations
• Relational Algebra consists of several groups of operations
• Unary Relational Operations
• SELECT (symbol:  (sigma))
• PROJECT (symbol:  (pi))
• RENAME (symbol:  (rho))
• Relational Algebra Operations from Set Theory
• UNION (  ), INTERSECTION (  ), DIFFERENCE ( MINUS, – )
• CARTESIAN PRODUCT ( x )
• Binary Relational Operations
• JOIN (several variations of JOIN exist)
• DIVISION
• Additional Relational Operations
• OUTER JOIN, OUTER UNION
• AGGREGATE FUNCTIONS (These compute summary of
information: e.g. SUM, COUNT, AVG, MIN, MAX)
Relational Database
A relational database consists of following relations:
• 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,
eMail)
• PrivateOwner (ownerNo, fName, lName, address, telNo,
eMail, password)
• Viewing (clientNo, propertyNo, viewDate, comment)
• Registration (clientNo, branchNo, staffNo, dateJoined)
Selection (or Restriction)
• 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).

© Pearson Education Limited 1995, 2005


Example - Selection (or Restriction)
• List all staff with a salary greater than 10,000.

© Pearson Education Limited 1995, 2005


Example - Selection (or Restriction)
• Display all staff with a salary greater than 10,000.

salary > 10000 (Staff)

© Pearson Education Limited 1995, 2005


Exercise
Delhi L-16 1500
Delhi L-15 1300
Noida L-17 1100

• Retrieve all loans taken from Delhi (Branch-name) of


amount greater than 1200 from loan relation

σ branch-name=“Delhi” ^ amount > 1200 (loan)


Projection
• col1, . . . , coln(R)
• Works on a single relation R and defines a
relation that contains a vertical subset of R,
extracting the values of specified attributes and
eliminating duplicates.
Example - Projection
• Produce a list of salaries for all staff, showing only
staffNo, fName, lName, and salary details.
Example - Projection
• Produce a list of salaries for all staff, showing
only staffNo, fName, lName, and salary details.

staffNo, fName, lName, salary(Staff)


Example
• To list all loan- numbers and loan amounts
 loan-number, amount(loan)

loan-number amount

L-14 1300

L-17 2000

L-12 1200
Exercise
• Given a relation
Customer(customer-name, customer-street, customer-city)

Find the name of all those customers who live in


“Bangalore”.
 customer-name(σ customer-city=“Bangalore” (customer))

customer-name

Shreya

Kartik
Union
• RS
• 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.
• If R and S have I and J tuples respectively, union is
obtained by concatenating them into one relation with a
maximum of (I + J) tuples.

© Pearson Education Limited 1995, 2005


Example - Union
• List all cities where there is either a branch office
or a property for rent.
Example - Union
• List all cities where there is either a branch office
or a property for rent.
city(Branch)  city(PropertyForRent)
Example
•Given relations
borrower(customer-name, loan-number)
depositor(customer-name, account-number)
Find the names of all bank customers who have either an
account or loan or both.
 customer-name(borrower) U  customer-name(depositor)

customer-name
Shreya
Rahul
Kartik
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.
Example - Set Difference
• List all cities where there is a branch office
but no properties for rent.
Example - Set Difference
• List all cities where there is a branch office but
no properties for rent.
city(Branch) – city(PropertyForRent)
Find name of all customers who take a loan but are
not account holders
 customer-name(borrower) -  customer-name(depositor)

customer-name
Shreya
Murali
Intersection
• RS
• 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 - Intersection
• List all cities where there is both a branch office
and at least one property for rent.

© Pearson Education Limited 1995, 2005


Example - Intersection
• List all cities where there is both a branch office
and at least one property for rent.
city(Branch)  city(PropertyForRent)

© Pearson Education Limited 1995, 2005


Notion of Concatenation
Consider two tuples
d(d1, d2,…….., dm)
e(e1, e2,………., en)
The operation of concatenation denoted by ^ is defined as
:
d ^ e = (d1, d2,……., dm, e1, e2,……., en)
Degree of resultant tuple becomes (m+n).
Cartesian/Cross Product
• RxS
• Defines a relation that is the concatenation of every
tuple of relation R with every tuple of relation S.
Example - Cartesian Product
• List the names and comments of all clients who
have viewed a property for rent.
Example - Cartesian Product
• List the names and comments of all clients who
have viewed a property for rent.
( clientNo, fName, IName ( Client ) )  ( clientNo, propertyNo, comment ( Viewing) )
client.client N o f Name l Name Viewing.client No property No comment
CR76 John Kay CR56 PA14 too small
CR76 John Kay CR76 PG4 too remote
CR76 John Kay CR56 PG4 Blank
CR76 John Kay CR62 PA14 no dining
room
CR76 John Kay CR56 PG36 Blank
CR56 Aline Stewart CR56 PA14 too small
CR56 Aline Stewart CR76 PG4 too remote
CR56 Aline Stewart CR56 PG4 Blank
CR56 Aline Stewart CR62 PA14 no dining
room
CR56 Aline Stewart CR56 PG36 Blank
Example - Cartesian Product
client.client f Name l Name Viewing.client property N comment
No No o
CR74 Mike Ritchie CR56 PA14 too small
CR74 Mike Ritchie CR76 PG4 too remote
CR74 Mike Ritchie CR56 PG4 Blank
CR74 Mike Ritchie CR62 PA14 no dining room
CR74 Mike Ritchie CR56 PG36 Blank
CR62 Mary Tregear CR56 PA14 too small
CR62 Mary Tregear CR76 PG4 too remote
CR62 Mary Tregear CR56 PG4 Blank
CR62 Mary Tregear CR62 PA14 no dining room
CR62 Mary Tregear CR56 PG36 Blank
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)))
client.clientNo f Name l Name Viewing.clientNo propertyN o Comment
C R76 John Kay C R76 P G4 too remote
C R56 Aline Stewart C R56 P A14 too small
C R56 Aline Stewart C R56 P G4
C R56 Aline Stewart C R56 P G36
C R62 Mary Tregear C R62 P A14 no dining
room

• Cartesian product and Selection can be reduced to a


single operation called a Join.
Join Operations
• Join is a derivative of Cartesian product.
• Equivalent to performing a Selection, using join
predicate as selection formula, over Cartesian
product of the two operand relations.
Join Operations
Various forms of join operation:
• Theta join
• Equijoin (a particular type of Theta
join)
• Natural join
• Outer join
• Semi-join
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 (<, , >, , =, ).
Theta join (-join)
• 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 - Natural join
• List the names and comments of all clients who have
viewed a property for rent.
(clientNo, fName, lName(Client))
(clientNo, propertyNo, comment(Viewing))
Division
• RS
• Defines a relation over the attributes C that
consists of set of tuples from R that match
combination of every tuple in S.
Division

s1 p1 Find suppliers who supply all parts being used X/Y s1


s1 p2 s2
s1 p3 p2
s3
s1 p4 Y s4
s2 p1
s2 p2
s3 p1
s3 p2 Find suppliers who supply all parts being used X/Z s1
s4 p2
s4
s4 p3
s4 p4 p2
p4
XX
Z
Relations
• Relations: Sailors, Reserves, Boats
• Sailors (sid, sname, rating, age)
• Boats(bid, bname, color)
• Reserves(sid, bid, day)
Sailors instance Reserves instance

Boats instance
Examples
Q1:Find names of sailors who’ve reserved boat #103
Q2:Find names of sailors who’ve reserved a red boat
Q3:Find names of sailors who’ve reserved a red or a green
boat
Q4:Find names of sailors who’ve reserved a red and a green
boat
Q5:Find the names of sailors who’ve reserved all boats
Find names of sailors who’ve reserved boat #103

• Solution 1:  (Temp1,  bid = 103 Re serves)

 ( Temp2, Temp1  Sailors)


 sname (Temp2)

 sname ( (Re serves  Sailors))


bid =103
Find names of sailors who’ve reserved a red boat
• Information about boat colour only available in
Boats➔ need an extra join:
 sname (( Boats)  Re serves  Sailors)
color =' red '
Find sailors who’ve reserved a red or a green boat

• Identify all red or green boats, then find sailors


who’ve reserved one of these boats:
 (Tempboats, ( Boats))
color =' red '  color =' green '
 sname(Tempboats  Re serves  Sailors)

❖ Can also define Tempboats using union


❖ What happens if  is replaced by  in this query?
Sailors instance Reserves instance

Boats instance
Find sailors who’ve reserved a red and a
green boat
• Must identify sailors who’ve reserved red boats, sailors
who’ve reserved green boats, then find the intersection
(sid is a key for Sailors):

 (Tempred,  (( Boats)  Re serves))


sid color =' red '
 (Tempgreen,  (( Boats)  Re serves))
sid color =' green'

 sname((Tempred  Tempgreen)  Sailors)


Find the names of sailors who’ve reserved all boats
• Uses division: schemas of the input relations to /
must be carefully chosen:
 (Tempsids, ( Re serves) / ( Boats))
sid, bid bid
 sname (Tempsids  Sailors)

❖ To find sailors who’ve reserved all ‘Interlake’ boats:


..... / ( Boats)
bid bname =' Interlake'
Join Operations
• Various forms of join operation
• Theta join
• Equijoin (a particular type of Theta
join)
• Natural join
• Outer join
• Semijoin
A B
A1 A2 A3 A1 B1 B2
1 2 3 1 5 6
2 4 5 4 5 6
AҲB
A.A1 A2 A3 B.A1 B1 B2

1 2 3 1 5 6
2 4 5 1 5 6
1 2 3 4 5 6
2 4 5 4 5 6

EQUI JOIN
A.A1 A2 A3 B.A1 B1 B2

1 2 3 1 5 6
A
A1 A2 A3 THETA JOIN
1 2 3 A.A1<B.A1
2 4 5 A.A1 A2 A3 B.A1 B1 B2
B 1 2 3 4 5 6
A1 B1 B2 2 4 5 4 5 6
1 5 6
4 5 6 NATURAL JOIN
A.A1 A2 A3 B1 B2
1 2 3 5 6
Outer join
• To display rows in the result that do not have
matching values in the join column, use Outer join.

• 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.
Semijoin
• R FS
• Defines a relation that contains the tuples of R that
participate in the join of R with S.

Can rewrite Semijoin using Projection and Join:

R FS = A(R F S)
A B
A1 A2 A3 A1 B1 B2
1 2 3 1 5 6
2 4 5 4 5 6

A LEFT OUTER B A RIGHT OUTER B


A.A1 A2 A3 B1 B2 B.A1 A2 A3 B1 B2

1 2 3 5 6 1 2 3 5 6

2 4 5 NULL NULL 4 NULL NULL 5 6


A B
A1 A2 A3 A1 B1 B2
1 2 3 1 5 6
2 4 5 4 5 6

A FULL OUTER
A.A1 A2 A3 B1 B2
1 2 3 5 6
2 4 5 NULL NULL
4 NULL NULL 5 6
A A1 A2 A3
1 2 3
4 3 4
A NATURAL JOIN B
2 4 5
A1 A2 A3 B1 B2
B
A1 B1 B2 4 3 4 5 6
1 5 6
1 2 3 5 6
4 5 6

A SEMIJOIN B WHERE A3=3


A1 A2 A3 B1 B2

1 2 3 5 6
Aggregate Operations
• ℑAL (R)
• ℑ (Pronounced script F)
• Applies Aggregate List (AL) to R to define a relation over
the aggregate list.
• AL contains one or more
(<aggregate_function>, <attribute>) pairs.
• Some aggregate functions: COUNT, SUM, AVG, MIN,
MAX.
How many properties cost more than 350 per month
to rent?

myCount
5
• How many properties cost more than 350 per month
to rent?
• Select count(propertyNo)
from 𝑃𝑟𝑜𝑝𝑒𝑟𝑡𝑦𝐹𝑜𝑟𝑅𝑒𝑛𝑡
where rent >350

σrent >350 (PropertyForRent)

ℑCOUNT propertyNo (σrent >350 (PropertyForRent))

myCount
5
Grouping Operation
• GAℑAL (R)
• Groups tuples of R by grouping attributes (GA) and
then applies aggregate 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.
Find the number of staff working in each branch
and the sum of their salaries.

branchNo myCount mySum


B003 3 54000
B005 2 39000
B007 1 9000
Find the number of staff working in each branch and the
sum of their salaries.
Select branchno, count(staffNo), sum(salary)
from Staff
group by branchNo
order by branchNo
branchNoℑCOUNT staffNo , SUM(salary) Staff

ρR branchNo, myCount, mySum


branchNoℑCOUNT staffNo , SUM(salary) Staff

branchNo myCount mySum


B003 3 54000
B005 2 39000
B007 1 9000
Relational Algebra Operations
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.
• A relational calculus expression creates a new relation,
which is specified in terms of variables that range over
rows of the stored database relations (in tuple calculus) or
over columns of the stored relations (in domain
calculus).
• It is considered to be a non-procedural or declarative
language.
Relational Calculus
• 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.
• 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)}
Tuple Relational Calculus
• The tuple relational calculus is based on specifying a
number of tuple variables.
• Each tuple variable usually ranges over a particular
database relation, meaning that the variable may take as its
value any individual tuple from that relation.
• A simple tuple relational calculus query is of the form
{t | COND(t)}
• where t is a tuple variable and COND(t) is a
conditional expression involving t.
• The result of such a query is the set of all tuples t that
satisfy COND(t).
Tuple Relational Calculus
• To find the first and last names of all employees whose
salary is above 50000, tuple calculus expression:
{t.FNAME, t.LNAME | EMPLOYEE(t) AND
t.SALARY>50000}
• The condition EMPLOYEE(t) specifies that the range
relation of tuple variable t is EMPLOYEE.
• The first and last name (PROJECTION FNAME, LNAME) of
each EMPLOYEE tuple t that satisfies the condition
t.SALARY>50000 (SELECTION  SALARY >50000) will be
retrieved.
The Existential and Universal Quantifiers

• Two special symbols called quantifiers can appear in formula:


• Universal quantifier () and
• Existential quantifier ().
• Universal quantifier (): “for all” quantifier
• Every tuple in the universe of tuples must make F true.
• Existential quantifier (): “there exists” quantifier
• Any tuple that exists in “the universe of” tuples may make F
true.
The Existential and Universal Quantifiers
• Informally, a tuple variable t is bound if it is quantified,
meaning that it appears in an ( t) or ( t) clause; otherwise, it
is free.
• If F is a formula, then so are ( t)(F) and ( t)(F), where t is a
tuple variable.
• The formula ( t)(F) is true if the formula F evaluates to true
for some (at least one) tuple assigned to free occurrences of t
in F; otherwise ( t)(F) is false.
• The formula ( t)(F) is true if the formula F evaluates to
true for every tuple (in the universe) assigned to free
occurrences of t in F; otherwise ( t)(F) is false.
Example Query Using Existential Quantifier
• Retrieve the name and address of all employees who work for the
‘Research’ department. The query can be expressed as :
{t.FNAME, t.LNAME, t.ADDRESS | EMPLOYEE(t) and ( d)
(DEPARTMENT(d) and d.DNAME=‘Research’ and
d.DNUMBER=t.DNO) }
• The only free tuple (t ) variables in a relational calculus expression
should be those that appear to the left of the bar ( | )
• If a tuple satisfies the conditions specified in the query, the attributes
FNAME, LNAME, and ADDRESS are retrieved for each such tuple.
• The conditions EMPLOYEE (t) and DEPARTMENT(d) specify
the range relations for t and d.
• The condition d.DNAME = ‘Research’ is a selection condition
and corresponds to a SELECT operation in the relational algebra,
whereas the condition d.DNUMBER = t.DNO is a JOIN
condition.
Languages Based on Tuple Relational Calculus
• The language SQL is based on tuple calculus. It uses the basic
block structure to express the queries in tuple calculus:
• SELECT <list of attributes>
• FROM <list of relations>
• WHERE <conditions>
• SELECT clause mentions the attributes being projected, the
FROM clause mentions the relations needed in the query, and
the WHERE clause mentions the selection as well as the join
conditions.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy