Database Systems Lec 5
Database Systems Lec 5
Relational Algebra
and Calculus
Query Languages Are specialized
languages for asking
(e.g. SQL) questions.
sname rating
yuppy 9
rusty 10
Set Operations
• Takes as input two relation instances
• Four standard operations
– Union
– Intersection
– Set-difference
– Cross-product
• Union, intersection, and difference
require the two input set to be union
compatible
– They have the same number of fields
– corresponding fields, taken in order from left
to right, have the same domains
Set Operation: Union
• R U S returns relation instance
containing all tuples that occur in
either relation instance R or S, or both.
• R and S must be union compatible.
• Schema of the result is defined to be
that of R.
S1
Set Operation: Union S2
sid sname rating age sid sname rating age
22 dustin 7 45.0 28 yuppy 9 35.0
31 lubber 8 55.5
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0 58 rusty 10 35.0
S1 – S2
sid sname rating age
22 dustin 7 45.0
Set Operation: Cross-Product
• R x S: Returns a relation instance whose
scheme contains:
– All the fields of R (in the same order as
they appear in R)
– All the fields os S (in the same order as
they
appear in S)
• The result contains one tuple <r,s> for each
pair with r ⋳ R and s ⋳ S
• Basically, it is the Cartesian product.
• Fields of the same name are unnamed.
Set Operation: Cross-Product
S1 R1
sid sname rating age
sid bid day
22 dustin 7 45.0
22 101 10/10/96
31 lubber 8 55.5 58 103 11/12/96
58 rusty 10 35.0
S1 x R1
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 22 101 10/10/96
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 22 101 10/10/96
31 lubber 8 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
Renaming
• Name conflict can arise in some
situations
• It is convenient to be able to give names
to the fields of a relation instance
defined by a relational algebra
expression.
(R(F),E)
•Take arbitrary relational expression E
• Returns an instance of a new relation R
• R is the result of E except that some fields are renamed
• Renaming list has the form (oldname newname or position
newname)
Renaming
(C(1sid1,5sid2),S1
R1)sname rating age (sid) bid day
(sid)
22 dustin 7 45.0 22 101 10/10/96
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 22 101 10/10/96
31 lubber 8 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