53
CompSci 516 Database Systems Lecture 4 Relational Algebra and Relational Calculus Instructor: Sudeepa Roy 1 Duke CS, Fall 2018 CompSci 516: Database Systems

Relational Algebra and Relational Calculus - cs.duke.edu · Recap: SQL --Lecture 2/3 •Creating/modifying relations •Specifying integrity constraints •Key/candidate key, superkey,

  • Upload
    ngoanh

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

CompSci 516DatabaseSystems

Lecture4RelationalAlgebra

andRelationalCalculus

Instructor:Sudeepa Roy

1DukeCS,Fall2018 CompSci516:DatabaseSystems

Announcements• Reminder:HW1

– Sakai:Resources->HW->HW1folder– Dueon09/20(Thurs),11:55pm,nolatedays– Startnow!– Submissioninstructionsforgradescope tobeupdated(willbenotifiedthroughpiazza)

• Yourpiazzaandsakai accountsshouldbeactive– Lastcall!willusepiazzaforpop-upquizzes– ifnotonpiazza,sendmeanemail– Installpiazzaapponyourphone(orbringalaptop)

DukeCS,Fall2018 CompSci516:DatabaseSystems 2

Recap:SQL-- Lecture2/3

• Creating/modifyingrelations• Specifyingintegrityconstraints• Key/candidatekey,superkey,primarykey,foreignkey• ConceptualevaluationofSQLqueries• Joins• Groupbys andaggregates• Nestedqueries• NULLs• Views

DukeCS,Fall2018 CompSci516:DatabaseSystems 3

Onwhiteboard:Fromlastlecture

Correct/incorrectgroup-byqueries

Today’stopics

• RelationalAlgebra(RA)andRelationalCalculus(RC)

• Readingmaterial– [RG]Chapter4(RA,RC)– [GUW]Chapters2.4,5.1,5.2

DukeCS,Fall2018 CompSci516:DatabaseSystems 4

Acknowledgement:Thefollowingslideshavebeencreatedadaptingtheinstructormaterialofthe[RG]bookprovidedbytheauthorsDr.Ramakrishnan andDr.Gehrke.

RelationalQueryLanguages

DukeCS,Fall2018 CompSci516:DatabaseSystems 5

RelationalQueryLanguages

• Querylanguages:Allowmanipulationandretrievalofdatafromadatabase

• Relationalmodelsupportssimple,powerfulQLs:– Strongformalfoundationbasedonlogic– Allowsformuchoptimization

• QueryLanguages!= programminglanguages– QLsnotintendedtobeusedforcomplexcalculations– QLssupporteasy,efficientaccesstolargedatasets

DukeCS,Fall2018 CompSci516:DatabaseSystems 6

FormalRelationalQueryLanguages

• Two“mathematical”QueryLanguagesformthebasisfor“real”languages(e.g.SQL),andforimplementation:– RelationalAlgebra:Moreoperational,veryusefulforrepresentingexecutionplans

– RelationalCalculus:Letsusersdescribewhattheywant,ratherthanhowtocomputeit(Non-operational,declarative,orprocedural)

• Note:Declarative(RC,SQL)vs.Operational(RA)

DukeCS,Fall2018 CompSci516:DatabaseSystems 7

Preliminaries(recap)• Aqueryisappliedtorelationinstances,andtheresultofaqueryisalsoarelationinstance.– Schemasofinputrelationsforaqueryarefixed

• querywillrunregardlessofinstance

– Theschemafortheresultofagivenqueryisalsofixed• Determinedbydefinitionofquerylanguageconstructs

• Positionalvs.named-fieldnotation:– Positionalnotationeasierforformaldefinitions,named-fieldnotationmorereadable

DukeCS,Fall2018 CompSci516:DatabaseSystems 8

ExampleSchemaandInstances

sid sname rating age22 dustin 7 45.031 lubber 8 55.558 rusty 10 35.0

sid sname rating age28 yuppy 9 35.031 lubber 8 55.544 guppy 5 35.058 rusty 10 35.0

sid bid day22 101 10/10/9658 103 11/12/96

R1

S1 S2

Sailors(sid,sname,rating,age)Boats(bid,bname,color)Reserves(sid,bid,day)

LogicNotations

• $ Thereexists• " Forall• ∧ LogicalAND• ∨ LogicalOR• ¬NOT• ⇒ Implies

RelationalAlgebra(RA)

RelationalAlgebra

• Takesoneormorerelationsasinput,andproducesarelationasoutput– operator– operand– semantic– soanalgebra!

• Sinceeachoperationreturnsarelation,operationscanbecomposed– Algebrais“closed”

DukeCS,Fall2018 CompSci516:DatabaseSystems 12

RelationalAlgebra• Basicoperations:

– Selection(σ)Selectsasubsetofrowsfromrelation– Projection(π)Deletesunwantedcolumnsfromrelation.– Cross-product(x)Allowsustocombinetworelations.– Set-difference(-)Tuplesinreln.1,butnotinreln.2.– Union(∪)Tuplesinreln.1orinreln.2.

• Additionaloperations:– Intersection(∩)– join⨝– division(/)– renaming(ρ)– Notessential,but(very)useful.

DukeCS,Fall2018 CompSci516:DatabaseSystems 13

Projection

sname ratingyuppy 9lubber 8guppy 5rusty 10p sname rating S, ( )2

age35.055.5

page S( )2

• Deletesattributesthatarenotinprojectionlist.

• Schema ofresultcontainsexactlythefieldsintheprojectionlist,withthesamenamesthattheyhadinthe(only)inputrelation.

• Projectionoperatorhastoeliminateduplicates(Why)

– Note:realsystemstypicallydon’tdoduplicateeliminationunlesstheuserexplicitlyasksforit(performance)

sid sname rating age28 yuppy 9 35.031 lubber 8 55.544 guppy 5 35.058 rusty 10 35.0

S2

10DukeCS,Fall2016 CompSci516:DataIntensiveComputingSystems

Selection

s rating S>8 2( )

sid sname rating age28 yuppy 9 35.058 rusty 10 35.0

• Selectsrowsthatsatisfyselectioncondition

• Noduplicatesinresult.Why?

• Schemaofresultidenticaltoschemaof(only)inputrelation

sid sname rating age28 yuppy 9 35.031 lubber 8 55.544 guppy 5 35.058 rusty 10 35.0

S2

11DukeCS,Fall2016 CompSci516:DataIntensiveComputingSystems

CompositionofOperators

• Resultrelationcanbetheinputforanotherrelationalalgebraoperation– Operatorcomposition s rating S>8 2( )

sid sname rating age28 yuppy 9 35.058 rusty 10 35.0

sname ratingyuppy 9rusty 10

p ssname rating rating S, ( ( ))>8 2

Union,Intersection,Set-Difference

• Alloftheseoperationstaketwoinputrelations,whichmustbeunion-compatible:– Samenumberoffields.– `Corresponding’fieldshavethesametype

– sameschemaastheinputs

sid sname rating age22 dustin 7 45.031 lubber 8 55.558 rusty 10 35.044 guppy 5 35.028 yuppy 9 35.0

S S1 2È

sid sname rating age22 dustin 7 45.031 lubber 8 55.558 rusty 10 35.0

sid sname rating age28 yuppy 9 35.031 lubber 8 55.544 guppy 5 35.058 rusty 10 35.0

S1 S2

Duke%CS,%Spring%2016 CompSci 516:%Data%Intensive%Computing%Systems 12

Union,Intersection,Set-Difference

• Note:noduplicate– “Setsemantic”– SQL:UNION– SQLallows“bagsemantic”aswell:UNIONALL

sid sname rating age22 dustin 7 45.031 lubber 8 55.558 rusty 10 35.044 guppy 5 35.028 yuppy 9 35.0

S S1 2È

sid sname rating age22 dustin 7 45.031 lubber 8 55.558 rusty 10 35.0

sid sname rating age28 yuppy 9 35.031 lubber 8 55.544 guppy 5 35.058 rusty 10 35.0

S1 S2

Duke%CS,%Spring%2016 CompSci 516:%Data%Intensive%Computing%Systems 12

Union,Intersection,Set-Difference

sid sname rating age31 lubber 8 55.558 rusty 10 35.0

S S1 2Ç

sid sname rating age22 dustin 7 45.0

S S1 2-

sid sname rating age22 dustin 7 45.031 lubber 8 55.558 rusty 10 35.0

sid sname rating age28 yuppy 9 35.031 lubber 8 55.544 guppy 5 35.058 rusty 10 35.0

S1 S2

Duke%CS,%Spring%2016 CompSci 516:%Data%Intensive%Computing%Systems 13

Cross-Product• EachrowofS1ispairedwitheachrowofR.• ResultschemahasonefieldperfieldofS1andR,withfield

names`inherited’ifpossible.– Conflict:BothS1andRhaveafieldcalledsid.

(sid) sname rating age (sid) bid day22 dustin 7 45.0 22 101 10/10/9622 dustin 7 45.0 58 103 11/12/9631 lubber 8 55.5 22 101 10/10/9631 lubber 8 55.5 58 103 11/12/9658 rusty 10 35.0 22 101 10/10/9658 rusty 10 35.0 58 103 11/12/96

DukeCS,Fall2018 CompSci516:DatabaseSystems 20

sid sname rating age22 dustin 7 45.031 lubber 8 55.558 rusty 10 35.0

sid bid day22 101 10/10/9658 103 11/12/96

RenamingOperator⍴

(sid) sname rating age (sid) bid day22 dustin 7 45.0 22 101 10/10/9622 dustin 7 45.0 58 103 11/12/9631 lubber 8 55.5 22 101 10/10/9631 lubber 8 55.5 58 103 11/12/9658 rusty 10 35.0 22 101 10/10/9658 rusty 10 35.0 58 103 11/12/96

§Ingeneral,canuse⍴(<Temp>,<RA-expression>)

DukeCS,Fall2018 CompSci516:DatabaseSystems 21

(⍴sid →sid1S1)⨉ (⍴sid →sid1R1)or

⍴(C(1→sid1,5→sid2),S1⨉ R1)Cisthenewrelationname

Joins

• Resultschemasameasthatofcross-product.• Fewertuplesthancross-product,mightbeableto

computemoreefficiently

R c S c R S!" = ´s ( )

(sid) sname rating age (sid) bid day22 dustin 7 45.0 58 103 11/12/9631 lubber 8 55.5 58 103 11/12/96

S RS sid R sid1 11 1!" . .<

DukeCS,Fall2018 CompSci516:DatabaseSystems 22

Findnamesofsailorswho’vereservedboat#103

DukeCS,Fall2018 CompSci516:DatabaseSystems 23

Sailors(sid,sname,rating,age)Boats(bid,bname,color)Reserves(sid,bid,day)

Findnamesofsailorswho’vereservedboat#103

• Solution1:

• Solution2:

p ssname bid serves Sailors(( Re ) )=103 !"

p ssname bid serves Sailors( (Re ))=103 !"

DukeCS,Fall2018 CompSci516:DatabaseSystems 24

Sailors(sid,sname,rating,age)Boats(bid,bname,color)Reserves(sid,bid,day)

ExpressinganRAexpressionasaTree

DukeCS,Fall2018 CompSci516:DatabaseSystems 25

Sailors(sid,sname,rating,age)Boats(bid,bname,color)Reserves(sid,bid,day)

p ssname bid serves Sailors(( Re ) )=103 !"Sailors Reserves

σbid=103

⨝sid =sid

πsname

Alsocalledalogicalqueryplan

Findsailorswho’vereservedaredoragreenboat

• Canidentifyallredorgreenboats,thenfindsailorswho’vereservedoneoftheseboats:

r s( , ( ' ' ' ' ))Tempboats color red color green Boats= Ú =

p sname Tempboats serves Sailors( Re )!" !"

Can also define Tempboats using unionTry the “AND” version yourself

DukeCS,Fall2018 CompSci516:DatabaseSystems 26

Sailors(sid,sname,rating,age)Boats(bid,bname,color)Reserves(sid,bid,day)

Useofrenameoperation

Whataboutaggregates?

• Extendedrelationalalgebra• 𝝲age,avg(rating)→ avgr Sailors• Alsoextendedto“bagsemantic”:allowduplicates

– Takeintoaccountcardinality– RandShavetupletresp.mandntimes– R∪ Shastm+n times– R∩Shastmin(m,n)times– R– Shastmax(0,m-n)times– sorting(τ),duplicateremoval(ẟ)operators

DukeCS,Fall2018 CompSci516:DatabaseSystems 27

Sailors(sid,sname,rating,age)Boats(bid,bname,color)Reserves(sid,bid,day)

RelationalCalculus(RC)

DukeCS,Fall2018 CompSci516:DatabaseSystems 28

RelationalCalculus• RAisprocedural

– πA(σA=a R)andσA=a (πAR)areequivalentbutdifferentexpressions

• RC– non-proceduralanddeclarative– describesasetofanswerswithoutbeingexplicitabouthowthey

shouldbecomputed

• TRC(tuplerelationalcalculus)– variablestaketuplesasvalues– wewillprimarilydoTRC

• DRC(domainrelationalcalculus)– variablesrangeoverfieldvalues

DukeCS,Fall2018 CompSci516:DatabaseSystems 29

TRC:example

• Findthenameandageofallsailorswitharatingabove7

{P|∃ SϵSailors(S.rating >7⋀ P.sname =S.sname ⋀ P.age =S.age)}

• Pisatuplevariable– withexactlytwofieldssname andage(schemaoftheoutputrelation)– P.sname =S.sname ⋀ P.age =S.age givesvaluestothefieldsofananswer

tuple

• Useparentheses,∀ ∃ ⋁ ⋀ ><= ≠ ¬etc asnecessary• A⇒ Bisveryusefultoo

– nextslideDukeCS,Fall2018 CompSci516:DatabaseSystems 30

Sailors(sid,sname,rating,age)Boats(bid,bname,color)Reserves(sid,bid,day)

$ Thereexists

A⇒ B

• A“implies”B• Equivalently,ifAistrue,Bmustbetrue• Equivalently,¬A⋁ B,i.e.

– eitherAisfalse(thenBcanbeanything)– otherwise(i.e.Aistrue)Bmustbetrue

DukeCS,Fall2018 CompSci516:DatabaseSystems 31

UsefulLogicalEquivalences

• "xP(x) =¬$x [¬P(x)]

• ¬(P∨Q) =¬P∧ ¬Q• ¬(P∧ Q) =¬P∨ ¬Q

– Similarly,¬(¬P∨Q)=P∧ ¬Qetc.

• AÞ B=¬A∨ B

DukeCS,Fall2018 CompSci516:DatabaseSystems 32

$ Thereexists" Forall∧ LogicalAND∨ LogicalOR¬ NOT

deMorgan’slaws

TRC:example

• Findthenamesofsailorswhohavereservedatleasttwoboats

DukeCS,Fall2018 CompSci516:DatabaseSystems 33

Sailors(sid,sname,rating,age)Boats(bid,bname,color)Reserves(sid,bid,day)

TRC:example

• Findthenamesofsailorswhohavereservedatleasttwoboats

{P|∃ SϵSailors(∃ R1ϵ Reserves∃ R2ϵReserves(S.sid =R1.sid⋀ S.sid =R2.sid⋀ R1.bid≠R2.bid)⋀ P.sname =S.sname)}

DukeCS,Fall2018 CompSci516:DatabaseSystems 34

Sailors(sid,sname,rating,age)Boats(bid,bname,color)Reserves(sid,bid,day)

TRC:example

• Findthenamesofsailorswhohavereservedallboats• Calledthe“Division”operation

DukeCS,Fall2018 CompSci516:DatabaseSystems 35

Sailors(sid,sname,rating,age)Boats(bid,bname,color)Reserves(sid,bid,day)

TRC:example

• Findthenamesofsailorswhohavereservedallboats• DivisionoperationinRA!

{P|∃ SϵSailors[∀BϵBoats(∃ Rϵ Reserves(S.sid =R.sid ⋀R.bid =B.bid))]⋀ (P.sname =S.sname)}

DukeCS,Fall2018 CompSci516:DatabaseSystems 36

Sailors(sid,sname,rating,age)Boats(bid,bname,color)Reserves(sid,bid,day)

TRC:example

• Findthenamesofsailorswhohavereservedallred boats

DukeCS,Fall2018 CompSci516:DatabaseSystems 37

Sailors(sid,sname,rating,age)Boats(bid,bname,color)Reserves(sid,bid,day)

HowwillyouchangethepreviousTRCexpression?

TRC:example

• Findthenamesofsailorswhohavereservedallred boats{P|∃ SϵSailors(∀BϵBoats(B.color =‘red’⇒ (∃ RϵReserves(S.sid =R.sid ⋀ R.bid =B.bid)))⋀ P.sname =S.sname)}

RecallthatA⇒ Bislogicallyequivalentto¬A⋁ Bso⇒ canbeavoided,butitiscleanerandmoreintuitive

DukeCS,Fall2018 CompSci516:DatabaseSystems 38

Sailors(sid,sname,rating,age)Boats(bid,bname,color)Reserves(sid,bid,day)

DRC:example

• Findthenameandageofallsailorswitharatingabove7

TRC:{P|∃ SϵSailors(S.rating >7⋀ P.name =S.name ⋀ P.age =S.age)}

DRC:{<N,A>|∃ <I,N,T,A>ϵSailors⋀ T>7}

• Variablesarenowdomainvariables• WewilluseuseTRC

– bothareequivalent• Anotheroptiontowritecomingsoon!

DukeCS,Fall2018 CompSci516:DatabaseSystems 39

Sailors(sid,sname,rating,age)Boats(bid,bname,color)Reserves(sid,bid,day)

MoreExamples:RC

• Thefamous“Drinker-Beer-Bar”example!

DukeCS,Fall2018 CompSci516:DatabaseSystems 40

Acknowledgement:examplesandslidesbyProfs.BalazinskaandSuciu,andthe[GUW]book

UNDERSTANDTHEDIFFERENCEINANSWERSFORALLFOURDRINKERS

DrinkerCategory1

Find drinkers that frequent some bar that serves some beer they like.

Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)

41CompSci516:DatabaseSystemsDukeCS,Fall2018

DrinkerCategory1

Find drinkers that frequent some bar that serves some beer they like.

Q(x) = $y. $z. Frequents(x, y)∧Serves(y,z)∧Likes(x,z)

a shortcut for{x | $Y ϵ Frequents $ Z ϵ Serves $ W ϵ Likes ((T.drinker = x.drinker) ∧(T.bar = Z.bar) ∧ (W.beer =Z.beer) ∧ (Y.drinker =W.drinker) }

Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)

42CompSci516:DatabaseSystemsDukeCS,Fall2018

Thedifferenceisthatinthefirstone,onevariable=oneattributeinthesecondone,onevariable=onetuple(TupleRC)Bothareequivalentandfeelfreetousetheonethatisconvenienttoyou

DrinkerCategory2

Find drinkers that frequent some bar that serves some beer they like.

Find drinkers that frequent only bars that serves some beer they like.

Q(x) = $y. $z. Frequents(x, y)∧Serves(y,z)∧Likes(x,z)

Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)

43CompSci516:DatabaseSystems

Q(x) = …

DukeCS,Fall2018

DrinkerCategory2

Find drinkers that frequent some bar that serves some beer they like.

Find drinkers that frequent only bars that serves some beer they like.

Q(x) = $y. $z. Frequents(x, y)∧Serves(y,z)∧Likes(x,z)

Q(x) = "y. Frequents(x, y)Þ ($z. Serves(y,z)∧Likes(x,z))

Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)

44CompSci516:DatabaseSystemsDukeCS,Fall2018

DrinkerCategory3

Find drinkers that frequent some bar that serves some beer they like.

Find drinkers that frequent only bars that serves some beer they like.

Find drinkers that frequent some bar that serves only beers they like.

Q(x) = $y. $z. Frequents(x, y)∧Serves(y,z)∧Likes(x,z)

Q(x) = "y. Frequents(x, y)Þ ($z. Serves(y,z)∧Likes(x,z))

Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)

45CompSci516:DatabaseSystems

Q(x) = …

DukeCS,Fall2018

DrinkerCategory3

Find drinkers that frequent some bar that serves some beer they like.

Find drinkers that frequent only bars that serves some beer they like.

Find drinkers that frequent some bar that serves only beers they like.

Q(x) = $y. $z. Frequents(x, y)∧Serves(y,z)∧Likes(x,z)

Q(x) = "y. Frequents(x, y)Þ ($z. Serves(y,z)∧Likes(x,z))

Q(x) = $y. Frequents(x, y)∧"z.(Serves(y,z) Þ Likes(x,z))

Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)

46CompSci516:DatabaseSystemsDukeCS,Fall2018

DrinkerCategory4

Find drinkers that frequent some bar that serves some beer they like.

Find drinkers that frequent only bars that serves some beer they like.

Find drinkers that frequent only bars that serves only beer they like.

Find drinkers that frequent some bar that serves only beers they like.

Q(x) = $y. $z. Frequents(x, y)∧Serves(y,z)∧Likes(x,z)

Q(x) = "y. Frequents(x, y)Þ ($z. Serves(y,z)∧Likes(x,z))

Q(x) = $y. Frequents(x, y)∧"z.(Serves(y,z) Þ Likes(x,z))

Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)

47CompSci516:DatabaseSystems

Q(x) = …

DukeCS,Fall2018

DrinkerCategory4

Find drinkers that frequent some bar that serves some beer they like.

Find drinkers that frequent only bars that serves some beer they like.

Find drinkers that frequent only bars that serves only beer they like.

Find drinkers that frequent some bar that serves only beers they like.

Q(x) = $y. $z. Frequents(x, y)∧Serves(y,z)∧Likes(x,z)

Q(x) = "y. Frequents(x, y)Þ ($z. Serves(y,z)∧Likes(x,z))

Q(x) = "y. Frequents(x, y)Þ "z.(Serves(y,z) Þ Likes(x,z))

Q(x) = $y. Frequents(x, y)∧"z.(Serves(y,z) Þ Likes(x,z))

Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)

48DukeCS,Fall2018 CompSci516:DatabaseSystems

WhyshouldwecareaboutRC• RCisdeclarative,likeSQL,andunlikeRA(whichisoperational)

• Givesfoundationofdatabasequeriesinfirst-orderlogic– youcannotexpressallaggregatesinRC,e.g.cardinalityofarelationorsum(possibleinextendedRAandSQL)

– stillcanexpressconditionslike“atleasttwotuples”(oranyconstant)

• RCexpressionmaybemuchsimplerthanSQLqueries– andeasiertocheckforcorrectnessthanSQL– powertouse" and Þ– then you can systematically go to a “correct” SQL

query

DukeCS,Fall2018 CompSci516:DatabaseSystems 49

FromRCtoSQL

Q(x) = $y. Likes(x, y)∧"z.(Serves(z,y) Þ Frequents(x,z))

Query: Find drinkers that like some beer (so much) that they frequent all bars that serve it

CompSci516:DatabaseSystems 50

Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)

DukeCS,Fall2018

FromRCtoSQL

Q(x) = $y. Likes(x, y)∧"z.(Serves(z,y) Þ Frequents(x,z))

Query: Find drinkers that like some beer so much that they frequent all bars that serve it

Step 1: Replace " with $ using de Morgan’s Laws

Q(x) = $y. Likes(x, y)∧ ¬$z.(Serves(z,y) ∧ ¬Frequents(x,z))

CompSci516:DatabaseSystems 51

Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)

"x P(x) same as¬$x ¬P(x)

¬(¬P∨Q) same asP∧ ¬ Q

º Q(x) = $y. Likes(x, y)∧"z.(¬ Serves(z,y) ∨ Frequents(x,z))

DukeCS,Fall2018

FromRCtoSQL

SELECT DISTINCT L.drinkerFROM Likes LWHERE not exists

(SELECT S.barFROM Serves SWHERE L.beer=S.beer

AND not exists (SELECT * FROM Frequents FWHERE F.drinker=L.drinker

AND F.bar=S.bar))

CompSci516:DatabaseSystems 52

Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)

DukeCS,Fall2018

Q(x) = $y. Likes(x, y) ∧¬ $z.(Serves(z,y)∧¬Frequents(x,z))

Step 2: Translate into SQL

Query: Find drinkers that like some beer so much that they frequent all bars that serve it

Wewillseea“methodicalandcorrect”translationtrough“safequeries”inDatalog

Summary

• YoulearntthreequerylanguagesfortheRelationalDBmodel– SQL– RA– RC

• Allhavetheirownpurposes

• Youshouldbeabletowriteaqueryinallthreelanguagesandconvertfromonetoanother– However,youhavetobecareful,notall“valid”expressionsinonemay

beexpressedinanother– {S|¬(SϵSailors)}– infinitelymanytuples– an“unsafe”query– Morewhenwedo“Datalog”,alsoseeCh.4.4in[RG]

DukeCS,Fall2018 CompSci516:DatabaseSystems 53