Data Modeling and Databases Advanced SQL

Post on 30-Oct-2021

11 views 0 download

transcript

DataModelingandDatabasesAdvancedSQL

GustavoAlonso,CeZhangSystemsGroupDepartmentofComputerScienceETHZürich

D-INFK,ETHZurich,DataModelingandDatabases 2

1. Wewillwritedowntheanswertoquestionsontheslides.

2. Keepsendingusyourquestionstothemailinglistsuchthatwecancompilethemandshareitwiththewholeclass.

Keepgivingusfeedback!

SQLWeSawsofarq SELECT… FROM… WHEREq Aggregationq Subquery

D-INFK,ETHZurich,DataModelingandDatabases 3

YoucandomoreinSQL!

q CorrelatedSubqueryq NULLValuesq Viewq Recursion

Acknowledgementq ManyexamplestakenfromJenniferWidom’sdatabaseclass:https://www.youtube.com/watch?v=uU7JBM3cAWU

D-INFK,ETHZurich,DataModelingandDatabases 4

Student

Assistant

Legi

PersNr

Semester

Name

Name

Area

Grade

attends

tests

Works-forProfessor

Lecture

teaches

requires

CP

Nr

Title

Room

Level

PersNr

follow-upprerequisite

Name

University Schema

1

N

11

N N

N

M

MM

N

5D-INFK,ETHZurich,DataModelingandDatabases

RelationalModelof UniversitySchemaProfessor

PersNr Name Level Room2125 Meyer FP 2262126 Kossmann FP 2322127 Roscoe AP 3102133 Perrig AP 522134 Sorkine AP 3092136 Welzl FP 362137 Norrie FP 7

StudentLegi Name Semester

24002 Gerber 1825403 Zollinger 1226120 Frey 1026830 Küng 827550 Fehr 628106 Lustenberger 329120 Schweizer 229555 Meier 2

LectureNr Title CP PersNr

5001 Databases 4 21375041 Networks 4 21255043 Operating Systems 3 21265049 Programming 2 21254052 Architecture 4 21255052 Theory 3 21265216 Graphics 2 21265259 Distributed Systems 2 21335022 Formal Methods 2 21344630 Probability 4 2137

requiresPrerequisite Follow-up

5001 50415001 50435001 50495041 52165043 50525041 50525052 5259

attendsLegi Nr

26120 500127550 500127550 405228106 504128106 505228106 521628106 525929120 500129120 504129120 504929555 502225403 5022

AssistantPerslNr Name Area Boss3002 Heinis Databases 21253003 Müller Theory 21253004 Kemme Networks 21263005 Frey Graphics 21273006 Peter Operating Systems 21273007 Kraska Formal Methods 2126

testsLegi Nr PersNr Grade

28106 5001 2126 1

25403 5041 2125 227550 4630 2137 2

6

NullValues(NULL=UNKNOWN)

select count (*)from Studentwhere (Semester<13) or (Semester>=13);

D-INFK,ETHZurich,DataModelingandDatabases 7

vs.

select count (*)from Student;

Arethese two queries equivalent?

NULLinSQLq InSQL,NULLrepresentsastate,notavalue

SELECT*FROMstudentsWHEREsemester=NULL;

q Thestandardisinconsistent,makingitdifficulttoapplyclearsemantics(examples:uniontreats“NULL”asbeingequaltoeachother).

q Implementationsofthestandardarealsoinconsistentamongthemselves(operationsinvolvingnulloverdifferentdatatypes)

q Nullvaluescreateanopenworldassumption(againsttheclosedworldassumptionoftherelationalmodel)

D-INFK,ETHZurich,DataModelingandDatabases 8

Workingwith NullValues1. Arithmetics:Propagatenull:If anoperand is null,the

result is null.Ø null +1->null

Ø null *0->null

2. Comparisons:Allcomparisons that involve anull value,evaluate to unknown.Ø null =null ->unknown

Ø null<13 -> unknown

Ø null >null ->unknown

3. Logic:Booleanoperators are evaluated using the followingtables (next slide):

D-INFK,ETHZurich,DataModelingandDatabases 9

¤ Omnibox entries now match saved profile names first. ¤ You can drag & drop text based content into the session now. ¤ OpenSSH upgraded to 7.6p1 (some older features dropped). ¤ The default terminal encoding has changed to UTF

SELECT(NULL>NULL)ISNULL;

nottrue falseunknown unknownfalse true

and true unknown falsetrue true unknown falseunknown unknown unknown falsefalse false false false

or true unknown falsetrue true true trueunknown true unknown unknownfalse true unknown false

D-INFK,ETHZurich,DataModelingandDatabases 10

4. where: Only tuples which evaluate to true are part of the query result. (unknown and false are equivalent here):

select count (*)from Studentwhere Semester<13 or Semester>=13;

5. group by: If exists, then there is a group for null.select count (*)

from Studentgroup by Semester;

Predicates with null:select count (*)from Studentwhere Semesteris null;

D-INFK,ETHZurich,DataModelingandDatabases 11

Workingwith NullValues

SyntacticSugar

D-INFK,ETHZurich,DataModelingandDatabases 12

select *from Studentwhere Semester > = 1 and Semester < = 6;

select *from Studentwhere Semester between 1 and 6;

select *from Studentwhere Semester in (2,4,6);

select Legi, ( case when Grade >= 5.5 then ´sehr gut´when Grade >= 5.0 then ´gut´when Grade >= 4.5 then ´befriedigend´when Grade >= 4.0 then ´ausreichend´else ´nicht bestanden´end)

from tests;

case

q Behaves like aswitch:evaluate from topto bottomq No „break“needed because at most one clauseexecuted.

13D-INFK,ETHZurich,DataModelingandDatabases

ExamplefromOracleSELECTcust_last_name,CASEcredit_limitWHEN100THEN'Low'WHEN5000THEN'High'ELSE'Medium'ENDFROMcustomers;

D-INFK,ETHZurich,DataModelingandDatabases 14

Comparisonswithlikeq "%”representsanysequenceofcharacters(0ton)q "_”representsexactlyonecharacterq N.B.:Forcomparisonswith= ,%and_arenormalchars.

D-INFK,ETHZurich,DataModelingandDatabases 15

select *from Studentwhere Name like ´Kossman%´;

select distinct Namefrom Lecture l, attends a, Student swhere s.Legi = a.Legi and a.Nr = l.Nr

and l.Title like ´%systems%´;

ExamplesfromOracleSELECTsalaryFROMemployeesWHERElast_name LIKE'R%';

SELECTsalaryFROMemployeesWHERElast_name ='R%';

D-INFK,ETHZurich,DataModelingandDatabases 16

Joins inSQLq cross join: Cartesian productq natural join:q join or inner join:Theta-Joinq left,right or full outer join:outer join variants

select *from R1,R2where R1.A=R2.B;

select *from R1join R2 on R1.A=R2.B;

D-INFK,ETHZurich,DataModelingandDatabases 17

DataManipulationLanguage

D-INFK,ETHZurich,DataModelingandDatabases 18

Insert tuplesinsert into attends

select Legi, Nrfrom Student, Lecturewhere Title= `Logik´;

insert into Student (Legi, Name)values (28121, `Kaufmann´);

Deletionoftuples,Update

D-INFK,ETHZurich,DataModelingandDatabases 19

delete Studentwhere Semester > 13;

update Studentset Semester= Semester + 1;

SnapshotSemantics1. Phase1:marktupleswhichareaffectedbytheupdate

2. Phase2:implementupdateonmarkedtuples

Otherwise,indeterministicexecutionofupdates:

deletefrom requireswhere prerequisitein (select follow-up

from requires);

D-INFK,ETHZurich,DataModelingandDatabases 20

delete from requrieswhere Prerequisite in (select Follow-up

from requires);

requiresPrerequisite Follow-up

5001 50415001 50435001 50495041 52165043 50525041 50525052 5229

D-INFK,ETHZurich,DataModelingandDatabases 21

Views:LogicalDataIndependence

D-INFK,ETHZurich,DataModelingandDatabases 22

User

View 1 View 2 View 3

Relation 1 Relation 2 Relation 3

Logicaldata independence

Physicaldata independence

DefiningaView

D-INFK,ETHZurich,DataModelingandDatabases 23

CREATE VIEW <NAME_OF_VIEW>AS<SQLQUERY>;

Thenyoucanuse<NAME_OF_VIEW>justlikeatable.

orig dest price

PEK ZRH 100

PEK MUC 10

MUC ZRH 20

MUC LUX 30

PEK SFO 1000

SFO ZRH 1000

Flight(orig,dest,price)CREATE VIEW FlightFromPEK ASSELECT *FROM FlightWHERE orig=PEK;

SELECT*FROMFlightFromPEKWHEREdest =ZRH; What’stheoutput?

That’sit.Whydoweevenneedthis?

Application1:Privacy

D-INFK,ETHZurich,DataModelingandDatabases 24

orig dest price

PEK ZRH 100

PEK MUC 10

MUC ZRH 20

MUC LUX 30

PEK SFO 1000

SFO ZRH 1000

Flight(orig,dest,price) CREATE VIEW FlightFromPEK ASSELECT *FROM FlightWHERE orig=PEK;

VS.

ThisusercanneverknowtheflightsfromMUC->ZRH.

(Informationgetsprotected,IfforsomereasonMUC->ZRHisa“secret”flight)

Application1:Usability

D-INFK,ETHZurich,DataModelingandDatabases 25

orig dest price

PEK ZRH 100

PEK MUC 10

MUC ZRH 20

MUC LUX 30

PEK SFO 1000

SFO ZRH 1000

Flight(orig,dest,price) CREATE VIEW FlightFromPEK ASSELECT *FROM FlightWHERE orig=PEK;

VS.

SELECT*FROMFlightFromPEKWHEREdest =ZRH;

SELECT*FROMFlightWHEREdest =ZRHandorig=PEK;

Youwillseemoresignificantsavingssoon!

Views...

D-INFK,ETHZurich,DataModelingandDatabases 26

for privacy

create view testView asselect Legi, Nr, PersNrfrom tests;

Views...

D-INFK,ETHZurich,DataModelingandDatabases 27

for simpler queriescreate view StudProf (Sname, Semester, Title, Pname) as

select s.Name, s.Semester, l.Title, p.Namefrom Student s, attends a, Lecture l, Professor pwhere s.Legi=a.Legi and a.Nr=l.Nr and

l.PersNr= p.PersNr;

select distinct Semesterfrom StudProfwhere PName=`Alonso ‘;

Viewsforis-arelationships

D-INFK,ETHZurich,DataModelingandDatabases 28

create table Employee(PersNr integer not null,Name varchar (30) not null);

create table ProfData(PersNr integer not null,Level character(2),Room integer);

create table AssiData(PersNr integer not null,area varchar(30),Boss integer);

Basetables

create view Professor asselect *from Employee e, ProfData dwhere e.PersNr=d.PersNr;

create view Assistant asselect *from Employee e, AssiData dwhere e.PersNr=d.PersNr;

create table Professor (PersNr integer not null,Name varchar (30) not null,Level character (2),Room integer);

create table Assistant(PersNr integer not null,Name varchar (30) not null,area varchar (30),Boss integer);

create table OtherEmps(PersNr integer not null,Name varchar (30) not null);

D-INFK,ETHZurich,DataModelingandDatabases 29

Basetables

create view Employee as(select PersNr, Name

from Professor)union

(select PersNr, Namefrom Assistant)

union(select*

from OtherEmps);

Howaviewgetsevaluatedq QueryRewriting

D-INFK,ETHZurich,DataModelingandDatabases 30

orig dest price

PEK ZRH 100

PEK MUC 10

MUC ZRH 20

MUC LUX 30

PEK SFO 1000

SFO ZRH 1000

Flight(orig,dest,price) CREATE VIEW FlightFromPEK ASSELECT *FROM FlightWHERE orig=PEK;

SELECT*FROMFlightFromPEKWHEREdest =ZRH;

SELECT*FROM(SELECT *FROM FlightWHERE orig=PEK)TWHERET.dest =ZRH;

SELECT*FROMFlightTWHERET.orig=PEKandT.dest =ZRH;

Let’sseesomerealexamples

D-INFK,ETHZurich,DataModelingandDatabases 31

cName state

S CA

B CA

M MA

C NY

College(cName,state,enroll)

sID sName GPA

1 A 3.9

2 B 3.6

3 C 3.5

4 D 3.9

5 E 2.9

6 F 3.8

Student(sID,sName,GPA)

sID cName major decision

1 S CS Y

1 S EE N

1 B CS Y

1 C EE Y

2 B BIO N

3 M BIOE Y

3 C BIOE N

3 C CS Y

3 C EE N

Apply(sID,cName,major,decision)

GraphRepr.

Let’sseesomerealexamples

D-INFK,ETHZurich,DataModelingandDatabases 32

cName state

S CA

B CA

M MA

C NY

College(cName,state,enroll)

sID sName GPA

1 A 3.9

2 B 3.6

3 C 3.5

4 D 3.9

5 E 2.9

6 F 3.8

Student(sID,sName,GPA)

sID cName major decision

1 S CS Y

1 S EE N

1 B CS Y

1 C EE Y

2 B BIO N

3 M BIOE Y

3 C BIOE N

3 C CS Y

3 C EE N

Apply(sID,cName,major,decision)

SELECT *FROMCsaccept;

Let’sseesomerealexamples

D-INFK,ETHZurich,DataModelingandDatabases 33

cName state

S CA

B CA

M MA

C NY

College(cName,state,enroll)

sID sName GPA

1 A 3.9

2 B 3.6

3 C 3.5

4 D 3.9

5 E 2.9

6 F 3.8

Student(sID,sName,GPA)

sID cName major decision

1 S CS Y

1 S EE N

1 B CS Y

1 C EE Y

2 B BIO N

3 M BIOE Y

3 C BIOE N

3 S CS Y

3 C EE N

Apply(sID,cName,major,decision)

Let’sseesomerealexamples

D-INFK,ETHZurich,DataModelingandDatabases 34

cName state

S CA

B CA

M MA

C NY

College(cName,state,enroll)

sID sName GPA

1 A 3.9

2 B 3.6

3 C 3.5

4 D 3.9

5 E 2.9

6 F 3.8

Student(sID,sName,GPA)

sID cName major decision

1 S CS Y

1 S EE N

1 B CS Y

1 C EE Y

2 B BIO N

3 M BIOE Y

3 C BIOE N

3 S CS Y

3 C EE N

Apply(sID,cName,major,decision)

SELECT*FROMCSberk WHERE

sID<3;

Let’sseesomerealexamples

D-INFK,ETHZurich,DataModelingandDatabases 35

cName state

S CA

B CA

M MA

C NY

College(cName,state,enroll)

sID sName GPA

1 A 3.9

2 B 3.6

3 C 3.5

4 D 3.9

5 E 2.9

6 F 3.8

Student(sID,sName,GPA)

sID cName major decision

1 S CS Y

1 S EE N

1 B CS Y

1 C EE Y

2 B BIO N

3 M BIOE Y

3 C BIOE N

3 S CS Y

3 C EE N

Apply(sID,cName,major,decision)

DROP VIEWCSaccept?

Views

q Canweupdateaview?

D-INFK,ETHZurich,DataModelingandDatabases 36

Whatwewant…

D-INFK,ETHZurich,DataModelingandDatabases 37

V

R1,…Rn R1’,…Rn’

V’

FindR1’,…Rn’suchthatV’isconsistentwithyour“updatequery”onV

Isthisalwayspossible?

Limitation1:Projection

D-INFK,ETHZurich,DataModelingandDatabases 38

sID cName major decision

1 S CS Y

1 S EE N

1 B CS Y

1 C EE Y

2 B BIO N

3 M BIOE Y

3 C BIOE N

3 S CS Y

3 C EE N

Apply(sID,cName,major,decision)

Whatwouldhappenifwe

INSERTINTOCsacceptVALUES(2)

Limitation2:Aggregation

D-INFK,ETHZurich,DataModelingandDatabases 39

r

1

2

3

4

5

6

7

8

9

R(r)

Whatwouldhappenifwe

UPDATEVSETv=v+1;

CREATEViewV(v)ASSELECTSUM(r)FROMR;

Notallarebadnews

D-INFK,ETHZurich,DataModelingandDatabases 40

r c

1 A

2 B

3 C

4 D

5 E

6 F

7 G

8 H

9 I

R(r,c)

Whatwouldhappenifwe

DELETEFROMVWHEREv=5;

CREATEViewV(v)ASSELECTrFROMR;

UpdatableViews

D-INFK,ETHZurich,DataModelingandDatabases 41

Example view which is not updatablecreate view ToughProf (PersNr, AvgGrade) as

select PersNr, avg(Grade)from testsgroup by PersNr;

update ToughProf set AvgGrade= 6.0where PersNr = 4711;

insert into ToughProfvalues (4711, 6.0);

SQL tries to avoid indeterminism.

Whataboutthis?

D-INFK,ETHZurich,DataModelingandDatabases 42

create view ToughProf (PersNr, AvgGrade) asselect PersNr, avg(Grade)from testsgroup by PersNr;

delete ToughProfwhere PersNr = 4711;

ViewsandUpdates

D-INFK,ETHZurich,DataModelingandDatabases 43

Example view which is not updatablecreate view LectureView as

select Title, CP, Namefrom Lecture l, Professor pwhere l.PersNr = p.PersNr;

insert into LectureViewvalues (`Nihilismus‘, 2, `Nobody‘);

There are scenarios in which the „insert“ is meaningful. There are scenarios in which SQL would have to guess. SQL is conservative and does not allow any scenario.

ViewsandUpdatesinSQL

q ASQLview is updatable iffØ Theview involves only one base relationØ Theview involves the key of that base relationØ Theview does NOTinvolve aggregates,group by,or duplicate-elimination

D-INFK,ETHZurich,DataModelingandDatabases 44

All views

Updatable views (theoretically)

Updatable views in SQL

D-INFK,ETHZurich,DataModelingandDatabases 45

Recursion

D-INFK,ETHZurich,DataModelingandDatabases 46

SQLWeSawsofarq SELECT… FROM… WHEREq Aggregationq Subqueryq NULLq SyntaxSugarsq Views

D-INFK,ETHZurich,DataModelingandDatabases 47

Aretheyenough?

ExampleofLimitation

D-INFK,ETHZurich,DataModelingandDatabases 48

parent child

A B

B C

C D

A B’

C D’

ParentOf(parent,child)

A

B

C

D

B’

D’

Q1:Whoistheparent ofD?

SELECT parentFROM ParentOfWHERE child=D

parent??

Q2:Whoisthegrandparent ofD?SELECT p2.parentasgparentFROM ParentOf p1,ParentOf p2WHERE p1.child=DAND

p1.parent=p2.child

gparent??Q3:Whoisthegreatgrandparent ofD?

SELECT p3.parentasggparentFROM ParentOf p1,ParentOf p2,ParentOf p3WHERE p1.child=DAND

p1.parent=p2.childANDp2.parent=p3.child

ggparent??

Q4:WhoarealltheancestorsofD?

Howmanyqueriesdoweneed?(=Depthofthetree)

WhatiftheDepthisnotknown?

AnotherExample

D-INFK,ETHZurich,DataModelingandDatabases 49

orig dest price

PEK ZRH 100

PEK MUC 10

MUC ZRH 20

MUC LUX 30

PEK SFO 1000

SFO ZRH 1000

Flight(orig,dest,price)

SFOPEK

ZRH

MUC

100

1020

1000

1000

Q1:WhatisthecheapestpriceofdirectflightfromPEKtoZRH?SELECTmin(price)FROM FlightWHERE orig =PEKANDdest =ZRH

price??

Q2:Whatisthecheapestpriceof1-stopflightfromPEKtoZRH?

SELECTmin(f1.price+f2.price)FROM Flightf1,Flightf2WHERE f1.orig=PEKANDf2.dest=ZRH

ANDf1.dest=f2.orig

price??

Q3:Whatisthecheapest priceFromPEKtoZRH?

HowtowritethisinSQL?

Limitsoftherelationalmodelq Recursionisanaturaloperationinprogramming

q Itisalsoanaturalwaytosearchsomedatamodelsbutnottherelationalmodel

q RecursionispossibleinSQL(butcomplexandinvolved)

q Considergraphdatabases…q …anexcursionintomoderndataprocessing.

D-INFK,ETHZurich,DataModelingandDatabases 50

Friendsq Alotofdatainsocialmediaisagraph:

Ø FriendsØ LikesØ RecommendsØ CommunicatesØ ContactlistsØ …

q Processingsuchdatainvolvesgraphtraversals,DFSandBFS,graphisomorphism,…

D-INFK,ETHZurich,DataModelingandDatabases 51

Weneedsomethingnew.

D-INFK,ETHZurich,DataModelingandDatabases 52

parent child

A B

B C

C D

A B’

C D’

ParentOf(parent,child)

A

B

C

D

B’

D’

Q4:WhoarealltheancestorsofD?

ancesterC

ancesterCB

ancesterCBA

Ancester1 Ancester2 Ancester3

SELECTp2.parentFROMAncester1p1,ParentOf p2WHEREp1.ancester=p2.child

SELECTp2.parentFROMAncester2p1,ParentOf p2WHEREp1.ancester=p2.child

SELECTparentFROMParentOfWHERED=child

1.Startingpoint

2.Samequeryagainandagain

3.Untilnochanges

WITH(DB2/SQL99)

D-INFK,ETHZurich,DataModelingandDatabases 53

ancesterC

ancesterCB

ancesterCBA

Ancester1 Ancester2 Ancester3

SELECTp2.parentFROMAncester1p1,ParentOf p2WHEREp1.ancester=p2.child

SELECTp2.parentFROMAncester2p1,ParentOf p2WHEREp1.ancester=p2.child

SELECTparentFROMParentOfWHERED=child

1.Startingpoint

2.Samequeryagainandagain(andUNIONtheresult)

3.Untilnochanges

WITHRAS

(basequeryUNIONrecursivequery)

<queryinvolvingR(andothertables)>

WITHAncesterOfD(ancester)AS(SELECTparentFROMParentOf WHERED=child

UNIONSELECTp2.parentFROMAncesterOfD p1,

ParentOf p2WHEREp1.ancester=p2.child)SELECT*FROMAncesterOfD;

UNIONvs.UNIONALL

D-INFK,ETHZurich,DataModelingandDatabases 54

ancesterC

ancesterCB

ancesterCBA

Ancester1 Ancester2 Ancester3

SELECTp2.parentFROMAncester1p1,ParentOf p2WHEREp1.ancester=p2.child

SELECTp2.parentFROMAncester2p1,ParentOf p2WHEREp1.ancester=p2.child

SELECTparentFROMParentOfWHERED=child

1.Startingpoint

2.Samequeryagainandagain(andUNIONtheresult)

3.Untilnochanges

WITHRAS

(basequeryUNIONrecursivequery)

<queryinvolvingR(andothertables)>

WITHAncesterOfD(ancester)AS(SELECTparentFROMParentOf WHERED=child

UNIONSELECTp2.parentFROMAncesterOfD p1,

ParentOf p2WHEREp1.ancester=p2.child)SELECT*FROMAncesterOfD;

MoreAbstractInterpretation

D-INFK,ETHZurich,DataModelingandDatabases 55

parent child

A B

B C

C D

A B’

C D’

ParentOf(parent,child)

A

B

C

D

B’

D’

Q4:WhoarealltheancestorsofD?

{(P,D)|(p,c)Î ParentOf L p = P L c = D}{(P,D)|(p1,c1),(p2,c2)Î ParentOf L p1=c2 L p2 = P L c1 = D}{(P,D)|(p1,c1),(p2,c2),(p3,c3)Î ParentOf L p1=c2 L p2=c3 L p3 = P L c1 = D}…

TransitiveClosure ofRelationParentOf Whyisitcalledtransitiveclosure?

UU

EvenMoreAbstractDefinition

D-INFK,ETHZurich,DataModelingandDatabases 56

A B

1 2

1 3

1 4

4 5

5 6

R(A,B)

1 2 3 4 5 6

transA,B(R)= {(a,b) | $k Î IN ($G1, ..., Gk Î R (

G1.A= G2.B L

!

Gk-1.A= Gk.B L

G1.A= a L

Gk.B= b))}

HowtowritethisinSQL?

D-INFK,ETHZurich,DataModelingandDatabases 57

A B

1 2

1 3

1 4

4 5

5 6

R(A,B)1 2 3 4 5 6

transA,B(R)= {(a,b) | $k Î IN ($G1, ..., Gk Î R (

G1.A= G2.B L

!

Gk-1.A= Gk.B L

G1.A= a L

Gk.B= b))}

WITHTransR(B)AS

(SELECT BFROM RWHEREA=“a”UNIONSELECT p2.BFROMTransR p1,Rp2WHERE p1.B=p2.A)

SELECT*FROMTransR ;

select Bfrom Rconnect by A = prior Bstart with A = (select B

from R where A=“a”));

OracleCONNECTBYClause

Example

D-INFK,ETHZurich,DataModelingandDatabases 58

select l1.prerequisite

from requires l1, requires l2, Lecture lwhere l1.Follow-up = l2.prerequisite and

l2.Follow-up = l.Nr andl.Title=`Databases´;

requiresPrerequisite Follow-up

5001 50415001 50435001 50495041 52165043 50525041 50525052 5259

Q:Allprerequisitesclassof“Database”

Doesthisanswerthequery?

connect by Clause (Oracle)

D-INFK,ETHZurich,DataModelingandDatabases 59

select Titlefrom Lecturewhere Nr in (select prerequisite

from requiresconnect by follow-up = prior prerequisitestart with follow-up = (select Nr

from Lecturewhere Title = ... ));

Recursion inDB2/SQL99

D-INFK,ETHZurich,DataModelingandDatabases 60

with TransLecture (First, Next)as (select prerequisite, follow-up from requires

union allselect t.First, r.follow-upfrom TransLecture t, requires rwhere t.Next= r.prerequisite)

select Title from Lecture where Nr in(select First from TransLecture where Next in

(select Nr from Lecturewhere Title = `Der Wiener Kreis´) )

Will“WITH”AlwaysTerminate?

D-INFK,ETHZurich,DataModelingandDatabases 61

WITHRECURSIVER(r)AS

(SELECT 1UNION

SELECT rFROM R)SELECT *FROM R

WITHRECURSIVER(r)AS

(SELECT 1UNION

SELECT r+1 FROM R)SELECT *FROM R

WITHRECURSIVER(r)AS

(SELECT 1UNION

SELECT r+1 FROM R)SELECT *FROM RLIMIT10;

PostgreSQLSyntax

Howaboutthisone?

D-INFK,ETHZurich,DataModelingandDatabases 62

WITHRECURSIVER(r)AS

(SELECT 1UNION

SELECT r+1 FROM R)SELECTmin(r)FROM RLIMIT10;

PostgreSQLSyntax

Howaboutthisone?

D-INFK,ETHZurich,DataModelingandDatabases 63

WITHRECURSIVER(r)AS

(SELECT 1UNION

SELECT r+1 FROM RWHERE r<20)SELECTmin(r)FROM RLIMIT10;

PostgreSQLSyntax

Howaboutthisone?

D-INFK,ETHZurich,DataModelingandDatabases 64

WITHRECURSIVER(r)AS

(SELECT 1UNIONALL

SELECT*FROMR)SELECT *FROMR;

PostgreSQLSyntax

WITHRECURSIVER(r)AS

(SELECT 1UNION

SELECT*FROMR)SELECT *FROMR;

Example1

D-INFK,ETHZurich,DataModelingandDatabases 65

parent child

A B

B C

C D

A B’

C D’

ParentOf(parent,child)

A

B

C

D

B’

D’

WITHAncestor(a,d)AS(SELECT parentAS a,childAS dFROM ParentOf

UNIONSELECT Ancestor.a,ParentOf.child AS dFROM Ancestor,ParentOfWHERE Ancestor.d =ParentOf.parent)

SELECT aFROM AncestorWHERE d=“D”.

WhoarealltheancestorsofD?

WITHAncestor(a,d)AS(SELECT parentAS a,childAS dFROM ParentOf

UNIONSELECT Ancestor.a,ParentOf.child AS dFROM Ancestor,ParentOfWHERE Ancestor.d =ParentOf.parent)

SELECT aFROM AncestorWHERE d=“B”.

WITHAncestor(a,d)AS(SELECT parentAS a,childAS dFROM ParentOf

UNIONSELECT Ancestor.a,ParentOf.child AS dFROM Ancestor,ParentOfWHERE Ancestor.d =ParentOf.parent)

SELECT aFROM AncestorWHERE d=“A”.

Example2

D-INFK,ETHZurich,DataModelingandDatabases 66

parent child

1 $100

2 $90

3 $80

4 $70

5 $60

Employee(id,salary)

mid eid

1 2

2 3

2 4

3 4

Manager(mid,eid)

name mgrid

X 1

Project(name,mgrid)

Example2

D-INFK,ETHZurich,DataModelingandDatabases 67

parent child

1 $100

2 $90

3 $80

4 $70

5 $60

Employee(id,salary)

mid eid

1 2

2 3

2 4

3 4

Manager(mid,eid)

name mgrid

X 1

Project(name,mgrid)

PostgreSQLSyntax

Example2

D-INFK,ETHZurich,DataModelingandDatabases 68

parent child

1 $100

2 $90

3 $80

4 $70

5 $60

Employee(id,salary)

mid eid

1 2

2 3

2 4

3 4

Manager(mid,eid)

name mgrid

X 1

Project(name,mgrid)

PostgreSQLSyntax

Example2

D-INFK,ETHZurich,DataModelingandDatabases 69

parent child

1 $100

2 $90

3 $80

4 $70

5 $60

Employee(id,salary)

mid eid

1 2

2 3

2 4

3 4

Manager(mid,eid)

name mgrid

X 1

Y 2

Z 4

Project(name,mgrid)

PostgreSQLSyntax

Example3

D-INFK,ETHZurich,DataModelingandDatabases 70PostgreSQLSyntax

orig dest airline price

A ORD UA 200

ORD B AA 100

A PHX SW 25

PHX LAS SW 30

LAS CMH FT 60

CMH B FT 60

A B JB 195

Flight(orig,dest,airline,price)

Drawgraphontheboard

Example3

D-INFK,ETHZurich,DataModelingandDatabases 71PostgreSQLSyntax

orig dest airline price

A ORD UA 200

ORD B AA 100

A PHX SW 25

PHX LAS SW 30

LAS CMH FT 60

CMH B FT 60

A B JB 195

Flight(orig,dest,airline,price)

Example3

D-INFK,ETHZurich,DataModelingandDatabases 72PostgreSQLSyntax

orig dest airline price

A ORD UA 200

ORD B AA 100

A PHX SW 25

PHX LAS SW 30

LAS CMH FT 60

CMH B FT 60

A B JB 195

Flight(orig,dest,airline,price)

Example3

D-INFK,ETHZurich,DataModelingandDatabases 73PostgreSQLSyntax

orig dest airline price

A ORD UA 200

ORD B AA 100

A PHX SW 25

PHX LAS SW 30

LAS CMH FT 60

CMH B FT 60

A B JB 195

Flight(orig,dest,airline,price)

WhatifweonlycareaboutstartingfromA?

Example3

D-INFK,ETHZurich,DataModelingandDatabases 74PostgreSQLSyntax

orig dest airline price

A ORD UA 200

ORD B AA 100

A PHX SW 25

PHX LAS SW 30

LAS CMH FT 60

CMH B FT 60

A B JB 195

Flight(orig,dest,airline,price)

WhatifweonlycareaboutstartingfromA->B?

WHEREdest =“B”

Example3

D-INFK,ETHZurich,DataModelingandDatabases 75PostgreSQLSyntax

orig dest airline price

A ORD UA 200

ORD B AA 100

A PHX SW 25

PHX LAS SW 30

LAS CMH FT 60

CMH B FT 60

A B JB 195

Flight(orig,dest,airline,price)

YoucandosimilarthingsforB

Example3(Whatifthereisloop)

D-INFK,ETHZurich,DataModelingandDatabases 76PostgreSQLSyntax

orig dest airline price

A ORD UA 200

ORD B AA 100

A PHX SW 25

PHX LAS SW 30

LAS CMH FT 60

CMH B FT 60

A B JB 195

CMH PHX FT 80

Flight(orig,dest,airline,price)

Drawgraphontheboard

Example3(Whatifthereisloop)

D-INFK,ETHZurich,DataModelingandDatabases 77PostgreSQLSyntax

orig dest airline price

A ORD UA 200

ORD B AA 100

A PHX SW 25

PHX LAS SW 30

LAS CMH FT 60

CMH B FT 60

A B JB 195

CMH PHX FT 80

Flight(orig,dest,airline,price)

Whatwillhappenifwerunthis?

Example3(Whatifthereisloop)

D-INFK,ETHZurich,DataModelingandDatabases 78PostgreSQLSyntax

orig dest airline price

A ORD UA 200

ORD B AA 100

A PHX SW 25

PHX LAS SW 30

LAS CMH FT 60

CMH B FT 60

A B JB 195

CMH PHX FT 80

Flight(orig,dest,airline,price)

Example3(Whatifthereisloop)

D-INFK,ETHZurich,DataModelingandDatabases 79PostgreSQLSyntax

orig dest airline price

A ORD UA 200

ORD B AA 100

A PHX SW 25

PHX LAS SW 30

LAS CMH FT 60

CMH B FT 60

A B JB 195

CMH PHX FT 80

Flight(orig,dest,airline,price)

max(total) Whatwillhappenifwerunthis?

Example3(Whatifthereisloop)

D-INFK,ETHZurich,DataModelingandDatabases 80PostgreSQLSyntax

orig dest airline price

A ORD UA 200

ORD B AA 100

A PHX SW 25

PHX LAS SW 30

LAS CMH FT 60

CMH B FT 60

A B JB 195

CMH PHX FT 80

Flight(orig,dest,airline,price)

Example3(Whatifthereisloop)

D-INFK,ETHZurich,DataModelingandDatabases 81PostgreSQLSyntax

orig dest airline price

A ORD UA 200

ORD B AA 100

A PHX SW 25

PHX LAS SW 30

LAS CMH FT 60

CMH B FT 60

A B JB 195

CMH PHX FT 80

Flight(orig,dest,airline,price)

max(total) Whatwillhappenifwerunthis?

Example3(Whatifthereisloop)

D-INFK,ETHZurich,DataModelingandDatabases 82PostgreSQLSyntax

orig dest airline price

A ORD UA 200

ORD B AA 100

A PHX SW 25

PHX LAS SW 30

LAS CMH FT 60

CMH B FT 60

A B JB 195

CMH PHX FT 80

Flight(orig,dest,airline,price)

max(total) Whatwillhappenifwerunthis?1000000000