Relational Algebra: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1
Relational Algebra: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1
Chapter 4
Notation
Relational Algebra
Relational Algebra basic operators.
Relational Algebra derived operators.
Named-attribute notation
• Ex: Sailor(sid, sname, rating,age)
• more readable
Advantages/disadvantages of one over the
other?
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6
R1 sid bid day
Example Instances 22 101 10/10/96
58 103 11/12/96
“Sailors” and “Reserves”
S1 sid sname rating age
relations for our examples.
We’ll use positional or 22 dustin 7 45.0
named field notation. 31 lubber 8 55.5
Assume that names of fields 58 rusty 10 35.0
in query results are
inherited from names of S2 sid sname rating age
fields in query input 28 yuppy 9 35.0
relations.
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7
Relational Algebra
3
7
4
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9
Relational Algebra
Every operator takes one or two relation
instances
A relational algebra expression is recursively
defined to be a relation
Result is also a relation
Can apply operator to
• Relation from database
• Relation as a result of another operator
condition.
No duplicates in result! (Why?) rating 8(S2)
Schema of result identical to
schema of (only) input relation.
Selection conditions:
simple conditions comparing
attribute values (variables) sname rating
and / or constants or
yuppy 9
complex conditions that
combine simple conditions rusty 10
using logical connectives
AND and OR.
sname,rating( rating 8(S2))
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14
Union, Intersection, Set-Difference
sid sname rating age
All of these operations take 22 dustin 7 45.0
two input relations, which 31 lubber 8 55.5
must be union-compatible: 58 rusty 10 35.0
Same number of fields. 44 guppy 5 35.0
“Corresponding” fields 28 yuppy 9 35.0
have the same type. S1 S2
What is the schema of result?
sid sname rating age
sid sname rating age 31 lubber 8 55.5
22 dustin 7 45.0 58 rusty 10 35.0
S1 S2 S1 S2
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15
Exercise on Union
Num shape holes Num shape holes
ber ber
1 round 2 4 round 2
2 square 4 5 square 4
3 rectangle 8 6 rectangle 8
BB YB
BB.holes YB.holes
Write down 2 tuples in this join.