relational algebra cmsc 461 michael wilson. relational algebra before we get into sql, want to take...
TRANSCRIPT
![Page 1: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/1.jpg)
Relational AlgebraCMSC 461Michael Wilson
![Page 2: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/2.jpg)
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
![Page 3: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/3.jpg)
Base operations Union Difference Cartesian Product Projection Selection Rename
![Page 4: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/4.jpg)
Base operations With these operations you can
accomplish everything you need in relational algebra
There are more operations, but they are for simplification
![Page 5: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/5.jpg)
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
![Page 6: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/6.jpg)
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
![Page 7: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/7.jpg)
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
![Page 8: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/8.jpg)
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.
![Page 9: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/9.jpg)
Cartesian product Resulting product by tuples:
(a, d) (a, e) (a, f) (b, d) (b, e) (b, f) (c, d) (c, e) (c, f)
![Page 10: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/10.jpg)
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)
![Page 11: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/11.jpg)
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
![Page 12: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/12.jpg)
Projection Example (from Wikipedia):
πcontactName,contactPhoneNumber(addressBook)
![Page 13: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/13.jpg)
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
![Page 14: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/14.jpg)
ProjectioncontactName contactPhoneNumber
Phil 555 5555
Henry 555 6666
Bob 555 7777
Octavio 555 8888
![Page 15: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/15.jpg)
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)
![Page 16: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/16.jpg)
Selection Terms have the following format:
<attribute> <op> <attribute/constant> <op> is =, ≠, >, ≥, <, ≤
Examples: phoneNumber=555 5555 GPA>2.0
![Page 17: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/17.jpg)
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
![Page 18: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/18.jpg)
Selection σGPA<4.0(R)
Select all from our relation where the attribute value for GPA is less than 4.0
![Page 19: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/19.jpg)
SelectionUMBC ID Age FName LName GPA
SM11112 19 Luigi Mario 3.9
FF11113 21 Snow Villiers 0.2
SW11234 25 Snow White 3.5
![Page 20: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/20.jpg)
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’
![Page 21: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/21.jpg)
SelectionUMBC ID Age FName LName GPA
FF11113 21 Snow Villiers 0.2
SW11234 25 Snow White 3.5
![Page 22: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/22.jpg)
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
![Page 23: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/23.jpg)
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
![Page 24: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/24.jpg)
Rename ρMIT ID/UMBC ID(R)
Rename UMBC ID to MIT ID
![Page 25: Relational Algebra CMSC 461 Michael Wilson. Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling](https://reader035.vdocuments.site/reader035/viewer/2022062806/56649eff5503460f94c14c37/html5/thumbnails/25.jpg)
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