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 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 a 1, a 2, …, a n b has b 1, b 2, …, b n 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: (a 1, a 2, …, a n, d 1, d 2, …, d n ) (a 1, a 2, …, a n, e 1, e 2, …, e n ) (a 1, a 2, …, a n, f 1, f 2, …, f n ) (b 1, b 2, …, b n, d 1, d 2, …, d n ) (b 1, b 2, …, b n, e 1, e 2, …, e n ) (b 1, b 2, …, b n, f 1, f 2, …, f n ) (c 1, c 2, …, c n, d 1, d 2, …, d n ) (c 1, c 2, …, c n, e 1, e 2, …, e n ) (c 1, c 2, …, c n, f 1, f 2, …, f n )
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) a i = attribute I R = relation to project
Projection Example (from Wikipedia): π contactName,contactPhoneNumber (addressBook)
Projection addresslastContacted contactPhoneNumber numberTypecontactName 111 Great Street 1 day ago CellPhil 123 Not So Great Street Last month LandlineHenry 8 Get Out of Here Way Last week WorkBob 7 RUN! Drive 2 years ago CellOctavio
Projection contactNamecontactPhoneNumber Phil Henry Bob Octavio
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: is =, ≠, >, ≥, <, ≤ Examples: phoneNumber= GPA>2.0
Selection UMBC IDAgeFNameLNameGPA SM Mario 4.0 SM LuigiMario3.9 MT SamusAran4.0 FF SnowVilliers0.2 SW SnowWhite3.5
Selection σ GPA<4.0 (R) Select all from our relation where the attribute value for GPA is less than 4.0
Selection UMBC IDAgeFNameLNameGPA SM LuigiMario3.9 FF SnowVilliers0.2 SW SnowWhite3.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’
Selection UMBC IDAgeFNameLNameGPA FF SnowVilliers0.2 SW SnowWhite3.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
Rename UMBC IDAgeFNameLNameGPA SM Mario 4.0 SM LuigiMario3.9 MT SamusAran4.0 FF SnowVilliers0.2 SW SnowWhite3.5
Rename ρ MIT ID/UMBC ID (R) Rename UMBC ID to MIT ID
Rename MIT IDAgeFNameLNameGPA SM Mario 4.0 SM LuigiMario3.9 MT SamusAran4.0 FF SnowVilliers0.2 SW SnowWhite3.5