relational algebra...relational algebra ´ unary operations ´selection: σ ´project: π ´rename:...
TRANSCRIPT
Relational AlgebraCS 4750 – Database Systems
1
Why Relational Algebra?
´ Relational Algebraisthemathematical foundation forhowwedefinerelationships andgetdata inandoutofthesystem
´ Doeseveryquerylanguage (e.g.SQL)followRelational Algebra?No!
´ Someexamples…´EasytodoinRA
´VeryhardinSQL
2
Relational Algebra
´ UnaryOperations
´Selection:σ´Project:π´Rename:ρ
Anoperatortakesone(unary)ortwo(binary)relations asinputandreturnsanewsingle relationasoutput
´ BinaryOperations
´Union:U
´Setdifference:−´Cartesian product: ×
´ Sixfundamental operations:
3
Relational Algebra´ Basedonthesixfundamentaloperationswecandefine…´ Additionalrelationaloperations(addnoadditional‘power’)
´ Setintersection:∩´ Naturaljoin: ⋈
´ Division:÷´ Assignment:←
´ Extendedoperations(withadditionalexpressiveness)´ AggregatefunctionG
´ Outerjoin
4
Selection (σ) – Sigma
´ σp(r), wherep =selection predicate,r=relation(table)
´ Comparisons intheselection predicate:=, <, ≤, >, ≥, ≠´Comparisonsarenotonlybetweenanattributeandavalue,butmayalsobeacomparisonbetweentwoattributes
´ Combining severalpredicates intoalargerpredicatecanbedonebyusingtheconnectives: AND,OR,andNOT
´ Selects specific tuples (rows)fromarelation (table)
´ Limits therowsbased onwhatwe’relookingfor
5
Selection (σ)
´σcity = “Zurich” AND postcode > 8010 (customer)´ “Findalltuples inthecustomerrelationthatareinthecityofZurichand
haveapostcode greaterthan8010.”
´ Result?
6
CD store Relational Database example © B.Signer
Selection (σ)
´ !!!Attention !!!
´ Theselection operation inrelational algebrahasadifferent meaningthattheSELECT statement used inSQL´SELECTinSQLcorrespondstoaprojection inrelationalalgebra
´Wewilltalkaboutprojectionnext
7
Selection (SQL) == Projection (RA)8
´ Example: ThetableE(forEMPLOYEE)
Projection (π) – Pi
´ πA1, A2, …, Am (r), whereAi =attribute
´ Returnspecificattributes (columns)fromarelation
´ Identical tuplescollapse intoasingle tuple(duplicates removed)intheresulting relation
9
Projection (π) – Pi
Identical tuplescollapse intoasingle tuple(duplicates removed)intheresulting relation
10
Rename (ρ) – Rho
´ ρx(E), renames theresultofexpression Etox
´ ρx(A1, A2, …, An)(E), renamestheresultofexpression Etoxandrenames theattributes toA1, A2, …, An
´ρperson(name, location) (πname, city (customer))
11
CD store Relational Database example © B.Signer
Rename the result of expression E to x; rename the resultant attributes to A1, A2, …, An
ρfriend_contact(ID, primary_email, alternative_email)(contact)
ID email1mi1y [email protected]
mi1y [email protected]
contact
= ID primary_emailmi1y [email protected]
mi1y [email protected]
friend_contact
Rename (ρ) – Another Example
Union (U)
´ r U s, whererandsaretworelations´ Unify(combine)tuples fromtworelations
´ Twothingsmust apply´ randsmusthavethesamedegree(samenumberofattributes)
´Thecorrespondingattributedomainsmusthaveacompatibletype
´ “Or”:“FindthenamesofSailorswhorentedaGreenor Redboat”
13
Intersect (∩)
´ r ∩ s, whererandsaretworelations´ Unify(combine)tuples thatarepresent inbothrelations
´ SamerulesforUnionmustapply
´ Union(U)is“or”
´ Intersect(∩)is“and”
´ “And”:“Findthenames ofpeople living inVirginiaandwhoownahome”
14
Set Difference (−)
´ r − s, whererandsaretworelations´ Findthetuplesthatareinonerelationbutarenotinanother
´ Twothingsmust apply´ randsmusthavethesamedegree(samenumberofattributes)
´Thecorrespondingattributedomainsmusthaveacompatibletype
´ πname (supplier) − πname (customer)´ “Findthenamesofsupplierswhoarenotcustomers”
15
Compatible type
CS1_studentscomputingID name
mi1y Mickey
mi2e Minnie
do3d Donald
da4y Daisy
do5d Donald
Find tuples that are in one relation but are not in another
Same number of attributes
computingID name
go9y Goofy
mi2e Minnie
do3d Donald
pl4o Pluto
do5d Donald
pe6e Pete
CS1_students – CS2_students
computingID name
mi1y Mickey
da4y Daisy
Attribute names match
− =
CS2_students
Set Difference (−) – Another Example
Cartesian (Cross) Product (×)
´ r × s, whererandsaretworelations´ Combines informationfromanytwotables
´ Schemaoftheresult isthecombinedschemas ofthetworelations´Relation1has3attributes
´Relation2has4attributes
´TheresultingrelationaftercombiningthetwotablesusingtheCartesianproductwillhave7attributes
´Theattributesofthe1st relationfollowedbytheattributesofthe2nd
17
Cartesian Product (×)
´ Question: Customerwhoate>1itemfromthemenu
´ Crossatablewithitself, canrenameoneofthetables
´ MatchwhereC1 =C2 andD1 ≠ D2
´ So,customerAnnate>1dish
18
Customer Dish
Ann Chicken
Bob Fish
Ann Veal
EatLog
C1 D1 C2 D2
Ann Ch Ann Ch
Ann Ch Bob Fi
Ann Ch Ann Ve
Bob Fi Ann Ch
Bob Fi Bob Fi
Bob Fi Ann Ve
Ann Ve Ann Ch
Ann Ve Bob Fi
Ann Ve Ann Ve
EatLogxEatLog
à
à
Cartesian Product (×) Example19
A B Ca b cd a fc b d
D E Fx y zp q r
A B C D E Fa b c x y za b c p q rd a f x y zd a f p q rc b d x y zc b d p q r
R
S
R × SNote: RxS is not the same as SxR!
ID email
mi1y [email protected]
mi2e [email protected]
mi1y [email protected]
ID email
mi1y [email protected]
mi2e [email protected]
mi1y [email protected]
ID email
mi1y [email protected]
mi2e [email protected]
mi1y [email protected]
Combinetworelations(mergecolumns)Cartesian(Cross)Product(×)– AnotherExampleUsuallynotmeaningfulwhenitisperformedalone.
×
C1× C2
=
ID email
mi1y [email protected]
mi2e [email protected]
mi1y [email protected]
(Let’scall itC1)contact
(Let’scall itC2)
ID1 email1
mi1y [email protected]
mi1y [email protected]
mi1y [email protected]
mi2e [email protected]
mi2e [email protected]
mi2e [email protected]
mi1y [email protected]
mi1y [email protected]
mi1y [email protected]
ID2 email2
mi1y [email protected]
mi2e [email protected]
mi1y [email protected]
Meaningfulwhenitisfollowedbyotheroperations.Findallstudentswhohavemorethanoneemail(2steps:crossproduct,thenselect tuples)
σID1=ID2 ^ email1≠ email2 (C1× C2)
ID1 email1
mi1y [email protected]
mi1y [email protected]
ID2 email2
mi1y [email protected]
mi1y [email protected]
Natural Join (⋈) (“Inner Join”)
´ r ⋈ s = πR US (σr.A1=s.A1 ^ r.A2=s.A2 ^ … ^ r.An=s.An (r x s))´ whereR U S = {A1, A2, …, An}´ Takethecrossproductoftwotables, select therowsyoucareabout
´ Binaryoperation
´ Combine certainSELECTIONSandaCARTESIANPRODUCTintooneoperation´FormsaCartesianproductofitstwoarguments
´Performsaselectionforcingequalityonthoseattributesthatappearinbothrelationschemas
´Removesduplicates
21
Natural Join (⋈) (“Inner Join”)
´r ⋈ s = πR US (σr.A1=s.A1 ^ r.A2=s.A2 ^ … ^ r.An=s.An (r x s))
´ Natural joinisanassociative operation
´ Usedquiteoften!
22
It performs a PROJECTION on the unionof all the attributes of both relations
It forms a SELECTION on tuples with equal, similarly named attributes
That is, keep all tuples of the cartesian product r x s that have the same value for the shared attributes or r(R) and s(S)
Natural Join (⋈) Example23
A B Ca b cd b cb b fc a d
B C Db c db c ea d b
A B C Da b c da b c ed b c dd b c ec a d bR
S
R ⋈ S
TheNaturalJoineliminatesreplicatedattributes(e.g.columns“B”and“C”showuponlyonceintheR ⋈ S table.)
Assignment (←)
´ v← E´ Theexpression (E)ontheRHSoftheassignment operatorisassigned
temporarily totherelation variable(v)ontheLHS
Temp1 ={someexpression}
Temp2 ={someotherexpression}
Result =Temp1 UTemp2
24
25
Division (÷)
´ r ÷ s´ Wheresisasubset ofr
´ “Forall”queries
´ “AB÷ B” Find“A”forall“B”,where“A”and“B”areattributes
´ (AllA’spairedwiththeirB’s)÷ (Allpossible B’s)
“AB”set ÷ “B”set
Result: ListofA’s
26
Division (÷)
´ Exampler ÷ s
27
Aggregate Function (G)
´G1, G2, …, Gm G F1(A1), F2(A2), …, Fn(An) (R)´ Where
´Gn =attributesonwhichtogroup,
´Fi =aggregatefunctionsonanattribute (An),
´R=relation
´ Theaggregatefunctions canbe:´Min,max,sum,count,average
´ Countofnumberofsailors: G count(sid) (Sailors)´ Result isarelation,singlecolumn: Sidwithresult value
28
Aggregate Function (G)
´ “Numberofeachofthecolorsoftheboats”
´ Countofnumberofboats,groupedbycolor:
colorG count(bid) (Boats)
29
color count(bid)Red 2Blue 1
Yellow 1Green 1
Aggregate Function (G)
´ Justaddcommastoincludemultiple groupingcolums
´ E.g.UsingBankDatabase
´ branch-nameG sum(amount), max(amount) (loan)
30
branch-name sum(amount) max(amount)Downtown 2500 1500Perryridge 2800 1500
RelationalAlgebra:SUMMARYBinaryoperations
Union rUs -- “or”
Intersection r∩ s-- “and”
Setdifference r− s
AdditionalExpressiveness
AssignmentAggregatefunction
Takeonerelation, returnanewrelationUnaryoperations
Selection σp(r)
Project πA1, A2, …, Am (r)
Rename ρx(A1, A2, …, An)(E)
Findtuplesthatsatisfyagivencondition
Slicearelation,returnanewrelationwithcertainattributes
RenametheresultofexpressionEtox;renameresultantattributestoA1,A2,…
Combinetuplesfrom2relations[require:samenumberofattributes;compatibledomains][result:sameattributes]
Combinetuplesfrom2relations[require:samenumberofattributes;compatibledomains][result:sameattributes]
Findtuplesthatareinonerelationbutarenotinanother[require:samenumberofattributes;compatibledomains][result:sameattributes]
Cartesianproduct r × s
Naturaljoin r⋈ s
Division r÷ s
Taketworelation,returnanewrelation
Combine2relations,allcombination[result:combinedattributes]
Selecttuplesthatsatisfythematchingconditions fromcombinedrelations[result:combinedattributes]
SimilartoAB÷ BFind“A”forall“B”[require:thereexistsB’sattributes inA][result:Aschema]
32
33
Additional Material(Not on exam ~ but may come in useful sometimes!)
34
Outer Join
´ Leftouter join:=⋈ (mostcommon)– discussed next
´ Rightouterjoin:⋈=´ Fullouter join:=⋈= (mostuncommon)
´ Extensions ofthenaturaljoinoperation
´ Computes thenaturaljoinandthenaddsthetuples fromonerelationthatdonotmatchtheotherrelation
´ Padsthetupleswithnullvalues
35
Outer Join
´ r1 =⋈ r2 Example – Leftouterjoin
´ Takes alltuples inthe left relation (r1)thatdidnotmatch withanytupleintheright relation(r2),padsthetupleswithnullvaluesforallotherattributes fromtheright relation(r2),andaddsthemtotheresultofthenaturaljoin
´ Want: List of all customers with their ordersYou want to know if you have any customers who still haven’t placed any orders yet. So you need ALL of the rows from the left relation r1 (ALL customer names) but you might not need all of the rows from relation r2
36
Name C_id …… … …
Name Order# Items …… … … …
Customers Table (r1) Orders Table (r2)
Outer Join´ Giventhefollowingexample “CDStore”Database
37
CD store Relational Database example © B.Signer
Outer Join38
CD store Relational Database example © B.Signer
Outer Join
´ Leftouter join
´ customer =⋈ order´ Result: (nullscomefromtherelation onthe⋈ side- order)
´ (Inorder table,customers“53”and“2”madepurchases)
39