relational algebra...relational algebra ´ based on the six fundamental operations we can define…...
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
Union (U)
´ r U s, whererandsaretworelations´ Unify(combine)tuples fromtworelations
´ Twothingsmust apply´ randsmusthavethesamedegree(samenumberofattributes)
´Thecorrespondingattributedomainsmusthaveacompatibletype
´ “Or”:“FindthenamesofSailorswhorentedaGreenor Redboat”
12
Intersect (∩)
´ r ∩ s, whererandsaretworelations´ Unify(combine)tuples thatarepresent inbothrelations
´ SamerulesforUnionmustapply
´ Union(U)is“or”
´ Intersect(∩)is“and”
´ “And”:“Findthenames ofpeople living inVirginiaandwhoownahome”
13
Set Difference (−)
´ r − s, whererandsaretworelations´ Findthetuplesthatareinonerelationbutarenotinanother
´ Twothingsmust apply´ randsmusthavethesamedegree(samenumberofattributes)
´Thecorrespondingattributedomainsmusthaveacompatibletype
´ πname (supplier) − πname (customer)´ “Findthenamesofsupplierswhoarenotcustomers”
14
Cartesian (Cross) Product (×)
´ r × s, whererandsaretworelations´ Combines informationfromanytwotables
´ Schemaoftheresult isthecombinedschemas ofthetworelations´Relation1has3attributes
´Relation2has4attributes
´TheresultingrelationaftercombiningthetwotablesusingtheCartesianproductwillhave7attributes
´Theattributesofthe1st relationfollowedbytheattributesofthe2nd
15
Cartesian Product (×)
´ Question: Customerwhoate>1itemfromthemenu
´ Crossatablewithitself, canrenameoneofthetables
´ MatchwhereC1 =C2 andD1 ≠ D2
´ So,customerAnnate>1dish
16
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 (×) Example17
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!
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
18
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!
19
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 (⋈) Example20
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.)
Outer Join
´ Leftouter join:=⋈ (mostcommon)– discussed next
´ Rightouterjoin:⋈=´ Fullouter join:=⋈= (mostuncommon)
´ Extensions ofthenaturaljoinoperation
´ Computes thenaturaljoinandthenaddsthetuples fromonerelationthatdonotmatchtheotherrelation
´ Padsthetupleswithnullvalues
21
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
22
Name C_id …… … …
Name Order# Items …… … … …
Customers Table (r1) Orders Table (r2)
Outer Join´ Giventhefollowingexample “CDStore”Database
23
CD store Relational Database example © B.Signer
Outer Join24
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)
25
Assignment (←)
´ v← E´ Theexpression (E)ontheRHSoftheassignment operatorisassigned
temporarily totherelation variable(v)ontheLHS
Temp1 ={someexpression}
Temp2 ={someotherexpression}
Result =Temp1 UTemp2
26
27
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
28
Division (÷)
´ Exampler ÷ s
29
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
30
Aggregate Function (G)
´ “Numberofeachofthecolorsoftheboats”
´ Countofnumberofboats,groupedbycolor:
colorG count(bid) (Boats)
31
color count(bid)Red 2Blue 1
Yellow 1Green 1
Aggregate Function (G)
´ Justaddcommastoincludemultiple groupingcolums
´ E.g.UsingBankDatabase
´ branch-nameG sum(amount), max(amount) (loan)
32
branch-name sum(amount) max(amount)Downtown 2500 1500Perryridge 2800 1500
33
34