Relational Algebra: R & G, Chapter 4
Relational Algebra: R & G, Chapter 4
Relational Algebra: R & G, Chapter 4
R & G, Chapter 4
By relieving the brain of all unnecessary
work, a good notation sets it free to
concentrate on more advanced problems,
and, in effect, increases the mental power of
the race.
-- Alfred North Whitehead (1861 - 1947)
Preliminaries
A query is applied to relation instances, and
the result of a query is also a relation
instance.
Schemas of input relations for a query are fixed
(but query will run over any legal instance)
The schema for the result of a given query is also
fixed. It is determined by the definitions of the
query language constructs.
Compare above two points to XPath, XQuery!
Positional vs. named-field notation:
Positional notation easier for formal definitions,
named-field notation more readable.
Both used in SQL
Though positional notation is not encouraged
bid
101
102
103
104
bname
Interlake
Interlake
Clipper
Marine
color
blue
red
green
red
day
101 10/10/96
103 11/12/96
S1 sid
S2 sid
22
31
58
28
31
44
58
Projection ()
Examples:
age(S2) ; sname,rating(S2)
(How
Projection ()
sname rating
yuppy
lubber
guppy
rusty
sid
28
31
44
58
9
8
5
10
sname,rating (S 2)
age
35.0
55.5
age(S2)
Selection ()
Selects rows that satisfy selection condition.
Result is a relation.
Schema of result is same as that of the input relation.
sid
28
31
44
58
sname
yuppy
lubber
guppy
rusty
rating
9
8
5
10
rating >8(S2)
age
35.0
55.5
35.0
35.0
sname rating
yuppy 9
rusty 10
Union
sid sname rating age
22
31
58
22
31
58
44
28
dustin
lubber
rusty
7
8
10
45.0
55.5
35.0
S1
sid
28
31
44
58
dustin
lubber
rusty
guppy
yuppy
7
8
10
5
9
S1 S2
45.0
55.5
35.0
35.0
35.0
Set Difference
sid sname rating age
22
31
58
22
dustin
lubber
rusty
7
8
10
45.0
55.5
35.0
dustin 7
45.0
S1 S2
S1
sid
28
31
44
58
S2 S1
Cross-Product
S1 R1: Each row of S1 paired with each row of
R1.
Q: How many rows in the result?
Result schema has one field per field of S1 and
R1, with field names `inherited if possible.
May have a naming conflict: Both S1 and R1 have a
field with the same name.
In this case, can use the renaming operator:
sid bid
day
22 101 10/10/96
58 103 11/12/96
R1
S1 x R1 =
22 dustin
45.0
22 101 10/10/96
22 dustin
45.0
58 103 11/12/96
31 lubber
55.5
22 101 10/10/96
31 lubber
55.5
58 103 11/12/96
58 rusty
10
35.0
22 101 10/10/96
58 rusty
10
35.0
58 103 11/12/96
R S = R (R S)
Intersection
sid sname rating age
22
31
58
dustin
lubber
rusty
7
8
10
45.0
55.5
35.0
S1
sid
28
31
44
58
S1S2
S1
sid
22
31
58
R1 =
sid
22
58
R ><c S c R S
S1><
R1
Examples
sid bid
day
Reserves
22 101 10/10/96
58 103 11/12/96
sid
Sailors 22
31
58
Boats
bid
101
102
103
104
bname
Interlake
Interlake
Clipper
Marine
color
Blue
Red
Green
Red
sname ((
sname (
bid =103
bid =103
sname ((
sname ( ((
Boats) >< Re s) >< Sailors)
sid bid color =' red '
(Tempboats , (
Boats))
(Tempboats,(
color='red 'color='green'
Boats))
sid
(Tempgreen ,
sid
((
Summary
Relational Algebra: a small set of
operators mapping relations to relations
Operational, in the sense that you specify
the explicit order of operations
A closed set of operators! Can mix and
match.
Basic ops include: , , , ,
Important compound ops: ,