





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 1
Chapter 4
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 2
āFormalā Query Languages
Understanding Algebra & Calculus is key to
understanding SQL, query processing!
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 3
Relational Algebra Preliminaries
Relational Algebra is
āclosedā
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 4
operators
U
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 5
a logical condition
combinations (ā§, V) of
terms
following forms:
{>, ā„, = ā¤, <}
5 Natalie Coughlin swimming USA
6 Paul Hamm gymnastics USA
4 Johann Koss skating Norway
3 Michael Phelps swimming USA
2 Jackie Joyner-Kersee track USA
1 Mary Lou Retton gymnastics USA
aid name sport country
Athlete
6 Paul Hamm gymnastics USA
1 Mary Lou Retton gymnastics USA
aid name sport country
( ) ' ' ' '
Athlete sport = gymnastics! country = USA
"
Schema of result?
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 6
that are not in
projection list
duplicates!
classical relational
model is a SET
5 Natalie Coughlin swimming USA
6 Paul Hamm gymnastics USA
4 Johann Koss skating Norway
3 Michael Phelps swimming USA
2 Jackie Joyner-Kerseetrack USA
1 Mary Lou Retton gymnastics USA
aidname sport country
Athlete
skating Norway
swimming USA
track USA
gymnastics USA
sport country
( ) ,
Athlete sportcountry
Schema of result?
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 10
Derived Operators: Division
Find customers with accounts at all branches in Brooklyn.
y tuple (branches in Brooklyn) in B, there is an < x,y>
tuple in A
fields in B, and x (^) !y is the list of fields of A.
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 11
Examples of Division A/B
cid bname
c 1 b 1
c 1 b 2
c 1 b 3
c 1 b 4
c 2 b 1
c 2 b 2
c 3 b 2
c 4 b 2
c 4 b 4
bname
b 2
bname
b 2
b 4
bname
b 1
b 2
b 4
cid
c 1
c 2
c 3
c 4
cid
c 1
c 4
cid
c 1
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 12
Expressing A/B Using Basic Operators
disqualified by some y value in B.
obtain an tuple that is not in A
x x
x
Can you express this # !# # "!
operator using basic
operators?
x
Disqualified x values
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 13
Examples of Division A/B
cid bname
c 1 b 1
c 1 b 2
c 1 b 3
c 1 b 4
c 2 b 1
c 2 b 2
c 3 b 2
c 4 b 2
c 4 b 4
bname
b 2
b 4
cid bname
c1 b
c1 b
c2 b
c2 b
c3 b
c3 b
c4 b
c4 b
-A
cid
c 1
c 4
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 14
Relational Calculus
saying how to compute them
tuple is essentially an assignment of constants to
variables that make the formula evaluate to true.
{ T | T " Loan! T. amount > 1400 }
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 15
Expressive Power
expressed as a safe query in relational
calculus; the converse is also true.
completeā query language (e.g., SQL) can
express every query that is expressible in
relational algebra/calculus.
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 19
Find names of sailors
whoāve reserved boat #
Solution 2:! ( T emp , " Re serves )
bid
= 103
! ( Temp 2 , Temp 1 >< Sailors )
sname
( T emp 2 )
Solution 3:
sname bid
( (Re serves Sailors ))
= 103
><
sname bid
(( Re s erves ) Sailors )
= 103
Solution 1:
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 20
Find names of sailors
whoāve reserved a red boat
! " sname color red
(( Boats serves Sailors
' '
) Re )
=
>< ><
An equivalent solution:
Ļ sname
(Ļ sid
((Ļ bid
Ļ color=āredā
Boats) ļ·ļ¶ Res) ļ·ļ¶ Sailors)
Query optimizer chooses from the (equivalent)
expressions and chooses one for efficiency of
evaluation.
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 21
Find the names of sailors
whoāve reserved at least
one boat
! ><
Sailor appears in this
intermediate relation
only if there is at least
one Reserves tuple with
same sid.
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 22
Find names of sailors
whoāve reserved a red OR
green boat
!( , ( "
' ' ' '
Tempboats ))
color red color green
Boats
= # =
sname
( T empboats >< Re serves >< Sailors )
( )))
' '
( )
' '
( , ( Boats
color green
Boats
color red
Tempboats
=
!
=
! sname
( T empboats >< Re serves >< Sailors )
Equivalent:
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 23
Find names of sailors
whoāve reserved a red AND
green boat
) Re ))
' '
( , ( ( Boats serves
sid color red
Tempred ><
=
sname
(( T empred " Tempgreen ) >< Sailors )
Tempgreen ) Re ))
sid color green
Boats serves
=
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 24
Find the sids of sailors over
age 20 who have not
reserved a red boat
) Re )
' '
20
Boats serves Sailors
sid color red
Sailors sid age
>