relational algebra...relational algebra ´ unary operations ´selection: σ ´project: π ´rename:...

39
Relational Algebra CS 4750 – Database Systems 1

Upload: others

Post on 25-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Relational AlgebraCS 4750 – Database Systems

1

Page 2: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Why Relational Algebra?

´ Relational Algebraisthemathematical foundation forhowwedefinerelationships andgetdata inandoutofthesystem

´ Doeseveryquerylanguage (e.g.SQL)followRelational Algebra?No!

´ Someexamples…´EasytodoinRA

´VeryhardinSQL

2

Page 3: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Relational Algebra

´ UnaryOperations

´Selection:σ´Project:π´Rename:ρ

Anoperatortakesone(unary)ortwo(binary)relations asinputandreturnsanewsingle relationasoutput

´ BinaryOperations

´Union:U

´Setdifference:−´Cartesian product: ×

´ Sixfundamental operations:

3

Page 4: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Relational Algebra´ Basedonthesixfundamentaloperationswecandefine…´ Additionalrelationaloperations(addnoadditional‘power’)

´ Setintersection:∩´ Naturaljoin: ⋈

´ Division:÷´ Assignment:←

´ Extendedoperations(withadditionalexpressiveness)´ AggregatefunctionG

´ Outerjoin

4

Page 5: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

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

Page 6: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Selection (σ)

´σcity = “Zurich” AND postcode > 8010 (customer)´ “Findalltuples inthecustomerrelationthatareinthecityofZurichand

haveapostcode greaterthan8010.”

´ Result?

6

CD store Relational Database example © B.Signer

Page 7: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Selection (σ)

´ !!!Attention !!!

´ Theselection operation inrelational algebrahasadifferent meaningthattheSELECT statement used inSQL´SELECTinSQLcorrespondstoaprojection inrelationalalgebra

´Wewilltalkaboutprojectionnext

7

Page 8: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Selection (SQL) == Projection (RA)8

´ Example: ThetableE(forEMPLOYEE)

Page 9: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Projection (π) – Pi

´ πA1, A2, …, Am (r), whereAi =attribute

´ Returnspecificattributes (columns)fromarelation

´ Identical tuplescollapse intoasingle tuple(duplicates removed)intheresulting relation

9

Page 10: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Projection (π) – Pi

Identical tuplescollapse intoasingle tuple(duplicates removed)intheresulting relation

10

Page 11: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

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

Page 12: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

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]

[email protected]

[email protected]

contact

= ID primary_emailmi1y [email protected]

mi1y [email protected]

[email protected]

[email protected]

friend_contact

Rename (ρ) – Another Example

Page 13: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Union (U)

´ r U s, whererandsaretworelations´ Unify(combine)tuples fromtworelations

´ Twothingsmust apply´ randsmusthavethesamedegree(samenumberofattributes)

´Thecorrespondingattributedomainsmusthaveacompatibletype

´ “Or”:“FindthenamesofSailorswhorentedaGreenor Redboat”

13

Page 14: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Intersect (∩)

´ r ∩ s, whererandsaretworelations´ Unify(combine)tuples thatarepresent inbothrelations

´ SamerulesforUnionmustapply

´ Union(U)is“or”

´ Intersect(∩)is“and”

´ “And”:“Findthenames ofpeople living inVirginiaandwhoownahome”

14

Page 15: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Set Difference (−)

´ r − s, whererandsaretworelations´ Findthetuplesthatareinonerelationbutarenotinanother

´ Twothingsmust apply´ randsmusthavethesamedegree(samenumberofattributes)

´Thecorrespondingattributedomainsmusthaveacompatibletype

´ πname (supplier) − πname (customer)´ “Findthenamesofsupplierswhoarenotcustomers”

15

Page 16: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

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

Page 17: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Cartesian (Cross) Product (×)

´ r × s, whererandsaretworelations´ Combines informationfromanytwotables

´ Schemaoftheresult isthecombinedschemas ofthetworelations´Relation1has3attributes

´Relation2has4attributes

´TheresultingrelationaftercombiningthetwotablesusingtheCartesianproductwillhave7attributes

´Theattributesofthe1st relationfollowedbytheattributesofthe2nd

17

Page 18: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

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

à

à

Page 19: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

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!

Page 20: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

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]

Page 21: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

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

Page 22: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

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)

Page 23: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

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.)

Page 24: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Assignment (←)

´ v← E´ Theexpression (E)ontheRHSoftheassignment operatorisassigned

temporarily totherelation variable(v)ontheLHS

Temp1 ={someexpression}

Temp2 ={someotherexpression}

Result =Temp1 UTemp2

24

Page 25: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

25

Page 26: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

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

Page 27: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Division (÷)

´ Exampler ÷ s

27

Page 28: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

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

Page 29: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Aggregate Function (G)

´ “Numberofeachofthecolorsoftheboats”

´ Countofnumberofboats,groupedbycolor:

colorG count(bid) (Boats)

29

color count(bid)Red 2Blue 1

Yellow 1Green 1

Page 30: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

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

Page 31: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

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]

Page 32: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

32

Page 33: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

33

Page 34: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Additional Material(Not on exam ~ but may come in useful sometimes!)

34

Page 35: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Outer Join

´ Leftouter join:=⋈ (mostcommon)– discussed next

´ Rightouterjoin:⋈=´ Fullouter join:=⋈= (mostuncommon)

´ Extensions ofthenaturaljoinoperation

´ Computes thenaturaljoinandthenaddsthetuples fromonerelationthatdonotmatchtheotherrelation

´ Padsthetupleswithnullvalues

35

Page 36: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

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)

Page 37: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Outer Join´ Giventhefollowingexample “CDStore”Database

37

CD store Relational Database example © B.Signer

Page 38: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Outer Join38

CD store Relational Database example © B.Signer

Page 39: Relational Algebra...Relational Algebra ´ Unary Operations ´Selection: σ ´Project: π ´Rename: ρ An operator takes one (unary) or two (binary) relations as input and returns

Outer Join

´ Leftouter join

´ customer =⋈ order´ Result: (nullscomefromtherelation onthe⋈ side- order)

´ (Inorder table,customers“53”and“2”madepurchases)

39