Upload
lynette-greene
View
217
Download
0
Embed Size (px)
Citation preview
Relational AlgebraCMSC 461Michael Wilson
Relational algebra Before we get into SQL, want to take a
look at what exactly SQL is really modeling Foundation for SQL and other query
languages
Base operations Union Difference Cartesian Product Projection Selection Rename
Base operations With these operations you can
accomplish everything you need in relational algebra
There are more operations, but they are for simplification
Union Say you have two relations with tuples
{a, b, c} and {d, e, f} The union of the two is {a, b, c, d, e, f}
One requirement: the two relations must have the same attributes This is called being “union-compatible”
Notation: A ∪ B
Difference Also known as the relative complement Say you have two relations
Relation A: {a, b, c} Relation B: {a, b, f}
Notation: B \ A The relative complement of A in B
B \ A results in the set of elements in B that are not in A B \ A = {f}
The two relations must be union-compatible
Cartesian product This one can get kind of ugly Requirement: the two relations involved
must have completely disjoint attributes A Cartesian product of two relations A
and B is the set of every tuple in A paired with every tuple in B
Notation: A × B
Cartesian product Two relations A and B with disjoint
attributes A has tuples {a, b, c} B has tuples {d, e, f}
Each tuple has attribute values a has a1, a2, …, an
b has b1, b2, …, bn
Etc.
Cartesian product Resulting product by tuples:
(a, d) (a, e) (a, f) (b, d) (b, e) (b, f) (c, d) (c, e) (c, f)
Cartesian product Listing out the attribute values:
(a1, a2, …, an, d1, d2, …, dn) (a1, a2, …, an, e1, e2, …, en) (a1, a2, …, an, f1, f2, …, fn) (b1, b2, …, bn, d1, d2, …, dn) (b1, b2, …, bn, e1, e2, …, en) (b1, b2, …, bn, f1, f2, …, fn) (c1, c2, …, cn, d1, d2, …, dn) (c1, c2, …, cn, e1, e2, …, en) (c1, c2, …, cn, f1, f2, …, fn)
Projection This is an operation that is used on one
relation Essentially restricts the attributes of a
relation to the ones you’re interested about
Notation: πa1,a2,…an(R) ai = attribute I R = relation to project
Projection Example (from Wikipedia):
πcontactName,contactPhoneNumber(addressBook)
Projectionaddress lastContacte
d
contactPhoneNumber
numberType contactName
111 Great Street
1 day ago 555 5555 Cell Phil
123 Not So Great Street
Last month 555 6666 Landline Henry
8 Get Out of Here Way
Last week 555 7777 Work Bob
7 RUN! Drive 2 years ago
555 8888 Cell Octavio
ProjectioncontactName contactPhoneNumber
Phil 555 5555
Henry 555 6666
Bob 555 7777
Octavio 555 8888
Selection Lists a set of tuples that match the
specified criteria Notation: σp(R)
p is a selection predicate This consists of terms that are connected
by logic operators ^ (and) v (or) ¬ (not)
Selection Terms have the following format:
<attribute> <op> <attribute/constant> <op> is =, ≠, >, ≥, <, ≤
Examples: phoneNumber=555 5555 GPA>2.0
SelectionUMBC ID Age FName LName GPA
SM11111 20 Mario Mario 4.0
SM11112 19 Luigi Mario 3.9
MT11111 21 Samus Aran 4.0
FF11113 21 Snow Villiers 0.2
SW11234 25 Snow White 3.5
Selection σGPA<4.0(R)
Select all from our relation where the attribute value for GPA is less than 4.0
SelectionUMBC ID Age FName LName GPA
SM11112 19 Luigi Mario 3.9
FF11113 21 Snow Villiers 0.2
SW11234 25 Snow White 3.5
Selection σage>20^fName=‘Snow’(R)
Select all from our relation where the attribute value for age is greater than 20 and the attribute value for fName is equal to ‘Snow’
SelectionUMBC ID Age FName LName GPA
FF11113 21 Snow Villiers 0.2
SW11234 25 Snow White 3.5
Rename The result of a rename is literally the
same as the original relation, but with an attribute renamed
ρa/b(R) The b attribute in the resulting tuples is
renamed to a
RenameUMBC ID Age FName LName GPA
SM11111 20 Mario Mario 4.0
SM11112 19 Luigi Mario 3.9
MT11111 21 Samus Aran 4.0
FF11113 21 Snow Villiers 0.2
SW11234 25 Snow White 3.5
Rename ρMIT ID/UMBC ID(R)
Rename UMBC ID to MIT ID
RenameMIT ID Age FName LName GPA
SM11111 20 Mario Mario 4.0
SM11112 19 Luigi Mario 3.9
MT11111 21 Samus Aran 4.0
FF11113 21 Snow Villiers 0.2
SW11234 25 Snow White 3.5