Académique Documents
Professionnel Documents
Culture Documents
Lesson Outcomes
2
5 Basic RA Operations
Projection ( ) Retains only wanted columns from
relation (vertical).
Selection
(
) Selects a subset of rows from
relation (horizontal).
Cross-product (x) Allows us to combine two
relations.
Set-difference () Tuples in R1, but not in R2.
Union ( ) Tuples in R1 and/or in R2.
NOTE: R1 = relation/table 1
R2 = relation/table 2
Example Instances
Boats
bid
101
102
103
104
S2
bname
Interlake
Interlake
Clipper
Marine
color
blue
red
green
red
Rents
sid bid
day
22 101 10/10/96
58 103 11/12/96
sid
28
31
44
58
Projection
age
sname,rating(S2)
Projection
age(S2)
age
35.0
55.5
sname
yuppy
sname,rating(S 2) lubber
guppy
rusty
Eliminates
redundant
35.0
rating
9
8
5
10
Selection ()
rating >8(S2)
sid
28
31
44
58
R1 - R2
R2 - R1
Union
S1
sid
22
31
58
sid
28
31
44
58
S1 U S2
dustin
lubber
rusty
guppy
yuppy
7
8
10
5
9
Eliminates duplicates
45.0
55.5
35.0
35.0
35.0
sid
22
31
58
Set
Difference
S1 S2
S1
sid
28
31
44
58
S2 S1
Cross-Product
R1 x R2: Each row of R1 (m rows) paired with each row of
R2 (n rows).
Q: How many rows in the result?
mxn
Result schema has one field per field of R1 and R2, with
field names `inherited if possible.
May have a naming conflict: Both R1 and R2 have a field
with the same name.
In this case, can use the renaming operator (OPTIONAL):
X (E)
- rename expression E as x
(A1, A2, , An) (E) = rename attributes in E as A1, A2,..,An
sid bid
day
22 101 10/10/96
58 103 11/12/96
R2
R1
(sid) sname rating
R1 X R2 =
age
(sid)
bid
day
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
Compound Operator:
Intersection
R S = R (R S)
Intersection
sid
22
31
58
sid
28
31
44
58
Compute R X S
Select rows where attributes that appear in both relations have equal
values and domains
Project all unique atttributes and one copy of each of the common ones.
S1
R1 =
sid
22
58
sid
22
31
58
day
22
31
58
101 10/10/96
103 11/12/96
dustin
lubber
rusty
R1
7
8
10
45.0
55.5
35.0
S1
S1><
R1 =
S1.sid < R1.sid
(sid)
22
31
dustin
lubber
7
8
(sid)
45.0 58
55.5 58
bid
day
103 11/12/96
103 11/12/96
Outer Join
An extension of the join operation that avoids
loss of information.
Computes the join and then adds tuples form
one relation that does not match tuples in the
other relation to the result of the join.
Uses null values:
null signifies that the value is unknown or does
not exist
branch-name
amount
L-170
L-230
L-260
Downtown
Redwood
Perryridge
3000
4000
1700
Relation borrower
customer-name loan-number
Jones
Smith
Hayes
L-170
L-230
L-155
Borrower
Downtown
Redwood
3000
4000
Jones
Smith
loan
Borrower
Downtown
Redwood
Perryridge
3000
4000
1700
Jones
Smith
null
loan
borrower
Downtown
Redwood
null
3000
4000
null
Jones
Smith
Hayes
borrower
Downtown
Redwood
Perryridge
null
3000
4000
1700
null
Jones
Smith
null
Hayes
Compound Operator:
Division
pno
s1
p1
s1
p2
s1
p3
s1
p4
s2
p1
s2
p2
s3
p2
s4
p2
s4
p4
pno
p2
B1
pno
p2
p4
B2
pno
p1
p2
p4
B3
sno
s1
s2
s3
s4
sno
s1
s4
sno
s1
A/B1
A/B2
A/B3
Relations r, s:
B
a
a
a
a
a
a
a
a
D
a
a
b
a
b
a
b
b
E
1
1
1
1
3
1
1
1
A B C
r s:
a
a
D E
a 1
b 1
s
A B C
Relation r:
7
7
3
10
sum(c) (r)
sum-C
27
branch-name account-number
Perryridge
Perryridge
Brighton
Brighton
Redwood
branch-name
balance
A-102
A-201
A-217
A-215
A-222
400
900
750
750
700
sum(balance) (account)
branch-name
balance
Perryridge
Brighton
Redwood
1300
1500
700
Assignment Operation
The assignment operation () provides a
convenient way to express complex queries.
Write query as a sequential program consisting
of a series of assignments followed by an
expression whose value is displayed as a result
of the query.
Assignment must always be made to a
temporary relation variable.
Example: Write r s as
temp1 R-S (r)
temp2 R-S ((temp1 x s) R-S,S (r))
result = temp1 temp2
References
32
www.cs.kent.edu/~yuri/cs43005.s2008/lectures/lect
ure2.ppt
inst.eecs.berkeley.edu/~cs186/sp06/lecs/lecture8Al
g.ppt
and others
Exercises
33
Exercises
34
Exercises
35