Relational Algebra Continued CMSC 461 Michael Wilson
Additional operations Intersection Division Natural join
Intersection The intersection of two relations Find the elements of both relations that are the same Must have the same attributes! Notation: R ∩ S
Intersection (Relation R) addresslastContacted contactPhoneNumber numberTypecontactName 111 Great Street 1 day ago CellPhil 8 Get Out of Here Way Last week WorkBob 7 RUN! Drive 2 years ago CellOctavio
Intersection (Relation S) addresslastContacted contactPhoneNumber numberTypecontactName 111 Great Street 1 day ago CellPhil 123 Not So Great Street Last month LandlineHenry 7 RUN! Drive 2 years ago CellOctavio
R ∩ S addresslastContacted contactPhoneNumber numberTypecontactName 111 Great Street 1 day ago CellPhil 7 RUN! Drive 2 years ago CellOctavio
Division This one is a little complicated Two relations, R and S R consists of attributes (r 1, r 2, … r n, s 1, s 2, … s n ) S consists of attributes (s 1, s 2, … s n ) In other words, S has some subset of R’s attributes Notation: R ÷ S
Division Result contains: Tuples for which the attribute values in R are equal to those in S Attributes that exist in R, but not in S Easier to show by example
Division (Relation R) addresslastContacted contactPhoneNumber numberTypecontactName 111 Great Street 1 day ago CellPhil 8 Get Out of Here Way Last week WorkBob 7 RUN! Drive 2 years ago CellOctavio
Division (Relation S) numberType Cell
Division (R ÷ S) addresslastContacted contactPhoneNumber contactName 111 Great Street 1 day ago Phil 7 RUN! Drive 2 years ago Octavio
Division (Relation R) bandNamealbumgenre SepulturaChaos A.D.Thrash Devin TownsendKiProg Devin TownsendDeconstructionDeath metal GwarBattle MaximusThrash Cannibal CorpseThe BleedingDeath metal Pain of SalvationRemedy LaneProg GhoulManiaxeDeath metal Seventh WonderMercy FallsProg
Division (Relation S) genre Death metal Prog
Division (R ÷ S) bandNamealbum Devin TownsendKi Devin TownsendDeconstruction
Natural join Returns all combinations of tuples between two relations that are equal on their common attribute names Notation: R ⋈ S
Natural join (Relation R) guitarModelyear JEM7VWH2006 Music Man JPX72009 Stratocaster1954 Omen2008 PGM Jaguar1962 Les Paul1952
Natural join (Relation S) manufacturerguitarModel IbanezJEM7VWH Ernie BallMusic Man JPX7 FenderStratocaster SchecterOmen IbanezPGM301 FenderJaguar GibsonLes Paul
Natural join (R ⋈ S) manufacturerguitarModelyear IbanezJEM7VWH2006 Ernie BallMusic Man JPX72009 FenderStratocaster1954 SchecterOmen2008 IbanezPGM FenderJaguar1962 GibsonLes Paul1952
Joins in general Joins are very important There are more than one type of join Big piece of relating relations/tables to one another in reality
Relational algebra expressions All these operation can be strung together Best to imagine each operation as functions that output a relation In other words: S = π phoneNumber (R) S is also a relation, and we can use that in other operations σ phoneNumber= (S) Or: σ phoneNumber= (π phoneNumber (R))
Relational algebra expressions Get the model of all guitars made in 2006 or later π guitarModel (σ year >= 2006 (GuitarModel ⋈ Guitar Year)) List age of everybody with a 3.0 GPA π age (σ GPA=3.0 (Grades))
Combining expressions Very similar to the kinds of statements we’ll see later when it comes to databases Stringing together and combining various queries