





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
full lecture notes provided by the lecturer.
Typology: Lecture notes
1 / 29
This page cannot be seen from the preview
Don't miss anything!






















4– 1 Relational Algebra and Calculus ( 1. 1. 1 )
Oct 2017 Version: 1. 1. 1
✥ The mathematical foundation of query languages such as SQL
✥ Relational Algebra and Calculus, and why they are important
✥ Basic algebra operators
sid sname rating age
28
31
44
58
yuppy
Lubber
guppy
Rusty
9
8
5
10
35
35
35
sid sname rating age
22
31
58
Dustin
Lubber
Rusty
7
8
10
45
35
(a) Instance S1 of Sailors
(b) Instance S2 of Sailors
sid bid day
22 101 1996 - 10 - 10
58 103 1996 - 11 - 12
(c) Instance R 1 of Re-
serves
bid bname color
101 Interlake blue
102 Interlake red
103 Clipper green
104 Marine red
(d) Instance B 1 of
Boats
✥ Relational algebra (RA) is a query language associated with
the relational model
✥ Every operator in RA takes one or two relations as parameters
and return a relation
✥ A relational algebra expression is recursively defined to be a
✦ relation ,
✦ a unary algebra operator applied to a single expression, or
✦ a binary algebra operator applied to two expressions
✥ Basic operators:
✦ Selection, projection, union, cross-product, and difference
✥ Procedural
✥ Allows us to extract columns from a relation
✥ Examples:
πsname,rating (S 2 )
sname rating
yuppy 9
Lubber 8
guppy 5
Rusty 10
π age
age
35
π sname,rating
(σ rating> 8
sname rating
yuppy 9
Rusty 10
✥ R ∩ S returns a relation which contains the tuples that are both in
R and S
✥ R and S should be union compatible
sid sname rating age
31
58
Lubber
Rusty
8
10
35
✥ R − S returns a relation which contains the tuples that are both in
R but not in S
✥ R and S should be union compatible
sid sname rating age
22 Dustin 7 45
✥ When operating in more than one table, name conflicts can arise
✥ renaming operator ρ renames the fields of a relation
✥ ρ(R(F
)E) takes an expression E and returns a new
instance relation called R
✥ R contains same columns as E, but some fields are renamed
✥ R contains same columns as E, but some fields are renamed
✥ Example: ρ(C (1 → sid 1 , 5 → sid 2 ), S 1 × R 1 ) returns:
sid 1 sname rating age sid 2 bid day
22
22
31
31
58
58
Dustin
Dustin
Lubber
Lubber
Rusty
Rusty
7
7
8
8
10
10
45
45
35
35
22
58
22
58
22
58
101
103
101
103
101
103
1996 - 10 - 10
1996 - 11 - 12
1996 - 10 - 10
1996 - 11 - 12
1996 - 10 - 10
1996 - 11 - 12
✥ With schema:
C (sid1: integer , sname: string , rating: integer ,
age: read , sid2: integer , bid: integer , day: date )
✥ The join operation is the most useful operation in relational
algebra
✥ It can be defined with cross product and selection, projection
✥ It is important to do joins without materializing the cross product
✥ There exist several variants of joins:
✦ Condition Join
✦ Equijoin
✦ Natural Join
4– 17 Relational Algebra and Calculus ( 1. 1. 1 )
✥ Essentially, a select of a cross product:
R r>< c
S = σ c
✥ Example: S 1 r>< S 1 .sid<R 1 .sid
(sid) sname rating age (sid) bid day
22
31
Dustin
Lubber
7
8
45
58
58
103
103
1996 - 11 - 12
1996 - 11 - 12
✥ (^) A natural join is a equijoin in which equalities are specified on
all fields that have the same name
✥ In this case we simply omit the condition
✥ The result does not have repeated field names
✥ Example: S 1 r>< R 1 = S 1 r>< S 1 .sid=R 1 .sid
sid sname rating age bid day
22
58
Dustin
Rusty
7
10
45
35
101
103
1996 - 10 - 10
1996 - 11 - 12
✥ If the two relations have no common attributes, then, the result is
the cross product
4– 20 Relational Algebra and Calculus ( 1. 1. 1 )
✥ It is a complex operator
✥ Useful in situations such as: find the sailors who have reserved
all the boats
✥ For relations A and B, A/B is the largest relation such that
✥ Definition:
A/B = {(x)|∀y s.t. (y) ∈ B, ∃(x, y) ∈ A}