Professional Documents
Culture Documents
CPSC 421 Database Management Systems: Relational Algebra, More SQL, Repeat
CPSC 421 Database Management Systems: Relational Algebra, More SQL, Repeat
CPSC 421 Database Management Systems: Relational Algebra, More SQL, Repeat
CPSC 421
Database Management Systems
Lecture 3:
Relational Algebra, More SQL,
Repeat
Todays Agenda
Quiz
Finish up MySQL
More on SQL queries
Relational Algebra
CPSC421,2009
9/8/09
CPSC421,2009
Rela)onalAlgebra
Prac1caldeni1onof
rela1onaldatabase
Mathema1caldeni1on
ofrela1onaldatabase
Operatesontables
(withduplicatesbags)
Operatesonrela1ons
(i.e.,sets)
Variouskeywords,
statements
Setbasedopera1ons
SELECT,FROM,WHERE,
Intersec1on,Union,
crossfer1liza1on
CPSC421,2009
9/8/09
The Plan
Present mathematical definition of a relational
database (relational algebra)
Intermix relational algebra and SQL
Well use relational algebra again when we talk about
evaluating and optimizing queries
CPSC421,2009
9/8/09
CPSC421,2009
Cross Products
Let A = {a, b, c} and B = {1, 2}
In set theory, the cross product is defined as
A X B = {(a, 1), (b, 1), (c, 1), (a, 2), (b, 2), (c, 2)}
A X B is a set consisting of ordered pairs (2-tuples)
where each pair consists of an element from A and
an element from B
CPSC421,2009
9/8/09
Practice Question
Suppose A = {a, b, c} and B = {1, 2}
What is B X B?
B X B = {(1, 1), (2, 1), (1, 2), (2, 2)}
CPSC421,2009
Defining Relations
Suppose we have the relation
Person(name, salary, num, status) with domains
NameValues = {all possible strings of 30 characters}
SalValues = {real numbers between 0 and 100,000}
StatusValues = {f, p}
NumValues = {integers between 0 and 9999}
10
9/8/09
Defining Relations
Each relation instance is a subset of the cross
product of its domains (see the Codd paper)
One element of a relation is called a tuple
If n domains, then n-tuples
CPSC421,2009
11
CPSC421,2009
12
9/8/09
CPSC421,2009
13
CPSC421,2009
14
9/8/09
15
Owner
J.Smith
W.Wei
J.Smith
M.Jones
H.Mar1n
Balance
1000.00
2000.00
5000.00
1000.00
10000.00
Type
checking
checking
savings
checking
checking
Balance<3000(Account)
is similar to the SQL query:
SELECT *
FROM Account
WHERE Balance < 3000;
CPSC421,2009
16
9/8/09
:RR
i.e., it is always applied to a single relation
Balance<3000(Account)
rela1onorrela1onal
algebraexpression
selectoperator
thepredicate(condi1on)
A,ributeCompartor(,>,=,,,<)A,ribute|Constant
CPSC421,2009
17
Examples
Balance < 3000(Account)
Number = 103(Account)
Balance = Number(Account)
Type = checking(Balance < 3000(Account))
Account
Number
101
102
103
104
105
Owner
J.Smith
W.Wei
J.Smith
M.Jones
H.Mar1n
Balance
1000.00
2000.00
5000.00
1000.00
10000.00
CPSC421,2009
Type
checking
checking
savings
checking
checking
18
9/8/09
Owner
J.Smith
W.Wei
J.Smith
M.Jones
H.Mar1n
Balance
1000.00
2000.00
5000.00
1000.00
10000.00
Type
checking
checking
savings
checking
checking
Number,Owner(Account)
is similar to the SQL query:
SELECT Number, Owner
FROM Account;
CPSC421,2009
19
:RR
i.e., it is always applied to a single relation
Number,Owner(Account)
rela1onorrela1onal
algebraexpression
projectoperator
listofalributestokeep
CPSC421,2009
20
10
9/8/09
Example
Owner(Account)
Account
Number
101
102
103
104
105
Owner
J.Smith
W.Wei
M.Jones
H.Mar1n
SELECTOwner
FROMAccount;
vs.
Owner
J.Smith
W.Wei
J.Smith
M.Jones
H.Mar1n
Balance
1000.00
2000.00
5000.00
1000.00
10000.00
Type
checking
checking
savings
checking
checking
Rela1onsarealwayssets
Sothequeryanswerisa
setofnames
andJ.Smithappearsjust
onceintheanswer
Owner
J.Smith
W.Wei
J.Smith
M.Jones
H.Mar1n
CPSC421,2009
21
Account
Number
101
102
103
104
105
CPSC421,2009
Owner
J.Smith
W.Wei
J.Smith
M.Jones
H.Mar1n
Balance
1000.00
2000.00
5000.00
1000.00
10000.00
Type
checking
checking
savings
checking
checking
22
11
9/8/09
CPSC421,2009
23
CPSC421,2009
24
12
9/8/09
Example
Suppose we have these relations in a university DB
Teacher(tnum, tname)
Course(cnum, cname)
This is just a simple example!
If we developed an actual university schema, it would
be much more detailed than this
CPSC421,2009
25
Example
Teacher
tnum
101
105
110
Course
cnum tname
346
Opera1ngSystems
491
SeniorDesign
tname
Smith
Crowley
Yerion
TeacherXCourse
The cross product
produces every
possible combination
of teacher and course
tnum
101
105
110
101
105
110
tname
Smith
Crowley
Yerion
Smith
Crowley
Yerion
CPSC421,2009
cnum
346
346
346
491
491
491
tname
Opera1ngSystems
Opera1ngSystems
Opera1ngSystems
SeniorDesign
SeniorDesign
SeniorDesign
26
13
9/8/09
Deposit
Account
CPSC421,2009
27
:RRR
i.e., it is always applied to a two relations and returns one
AccountNumber=AccountDeposit
rela1onorrela1onal
algebraexpression
rela1onorrela1onal
algebraexpression
thejoinpredicate(condi1on)
A,ributeCompartor(,>,=,,,<)A,ribute
CPSC421,2009
28
14
9/8/09
R1 a1 = a2 R2 a1 = a2(R1 R2)
Thus, we dont really need the join operator
Any query with a join can always be rewritten into cross
product followed by selection
And vice-versa it is also possible to define crossproduct based on join
CPSC421,2009
29
30
15
9/8/09
Examples
Student advisor=number Faculty
Student S S.age < F.age Faculty F
Student S S.salary F.age Faculty F
Join is sometimes called theta-join or -join
where represents any of the 6 comparators
The most common join is called a equi-join (for
equality condition)
R1 a1=a2 R2
CPSC421,2009
31
Challenge Question
How can you in general convert a basic SQL statement
(SELECT-FROM-WHERE) to an equivalent relational
algebra expression
SELECT
DISTINCT attributes
FROM
T1, T2,
WHERE conditions
CPSC421,2009
???
32
16
9/8/09
33
17