data modeling and databases advanced sql
TRANSCRIPT
![Page 1: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/1.jpg)
DataModelingandDatabasesAdvancedSQL
GustavoAlonso,CeZhangSystemsGroupDepartmentofComputerScienceETHZürich
![Page 2: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/2.jpg)
D-INFK,ETHZurich,DataModelingandDatabases 2
1. Wewillwritedowntheanswertoquestionsontheslides.
2. Keepsendingusyourquestionstothemailinglistsuchthatwecancompilethemandshareitwiththewholeclass.
Keepgivingusfeedback!
![Page 3: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/3.jpg)
SQLWeSawsofarq SELECT… FROM… WHEREq Aggregationq Subquery
D-INFK,ETHZurich,DataModelingandDatabases 3
YoucandomoreinSQL!
q CorrelatedSubqueryq NULLValuesq Viewq Recursion
![Page 4: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/4.jpg)
Acknowledgementq ManyexamplestakenfromJenniferWidom’sdatabaseclass:https://www.youtube.com/watch?v=uU7JBM3cAWU
D-INFK,ETHZurich,DataModelingandDatabases 4
![Page 5: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/5.jpg)
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
![Page 6: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/6.jpg)
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
![Page 7: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/7.jpg)
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?
![Page 8: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/8.jpg)
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
![Page 9: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/9.jpg)
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;
![Page 10: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/10.jpg)
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
![Page 11: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/11.jpg)
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
![Page 12: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/12.jpg)
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);
![Page 13: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/13.jpg)
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
![Page 14: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/14.jpg)
ExamplefromOracleSELECTcust_last_name,CASEcredit_limitWHEN100THEN'Low'WHEN5000THEN'High'ELSE'Medium'ENDFROMcustomers;
D-INFK,ETHZurich,DataModelingandDatabases 14
![Page 15: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/15.jpg)
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%´;
![Page 16: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/16.jpg)
ExamplesfromOracleSELECTsalaryFROMemployeesWHERElast_name LIKE'R%';
SELECTsalaryFROMemployeesWHERElast_name ='R%';
D-INFK,ETHZurich,DataModelingandDatabases 16
![Page 17: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/17.jpg)
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
![Page 18: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/18.jpg)
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´);
![Page 19: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/19.jpg)
Deletionoftuples,Update
D-INFK,ETHZurich,DataModelingandDatabases 19
delete Studentwhere Semester > 13;
update Studentset Semester= Semester + 1;
![Page 20: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/20.jpg)
SnapshotSemantics1. Phase1:marktupleswhichareaffectedbytheupdate
2. Phase2:implementupdateonmarkedtuples
Otherwise,indeterministicexecutionofupdates:
deletefrom requireswhere prerequisitein (select follow-up
from requires);
D-INFK,ETHZurich,DataModelingandDatabases 20
![Page 21: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/21.jpg)
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
![Page 22: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/22.jpg)
Views:LogicalDataIndependence
D-INFK,ETHZurich,DataModelingandDatabases 22
User
View 1 View 2 View 3
Relation 1 Relation 2 Relation 3
Logicaldata independence
Physicaldata independence
![Page 23: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/23.jpg)
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?
![Page 24: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/24.jpg)
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)
![Page 25: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/25.jpg)
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!
![Page 26: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/26.jpg)
Views...
D-INFK,ETHZurich,DataModelingandDatabases 26
for privacy
create view testView asselect Legi, Nr, PersNrfrom tests;
![Page 27: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/27.jpg)
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 ‘;
![Page 28: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/28.jpg)
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;
![Page 29: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/29.jpg)
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);
![Page 30: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/30.jpg)
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;
![Page 31: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/31.jpg)
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.
![Page 32: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/32.jpg)
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;
![Page 33: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/33.jpg)
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)
![Page 34: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/34.jpg)
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;
![Page 35: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/35.jpg)
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?
![Page 36: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/36.jpg)
Views
q Canweupdateaview?
D-INFK,ETHZurich,DataModelingandDatabases 36
![Page 37: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/37.jpg)
Whatwewant…
D-INFK,ETHZurich,DataModelingandDatabases 37
V
R1,…Rn R1’,…Rn’
V’
FindR1’,…Rn’suchthatV’isconsistentwithyour“updatequery”onV
Isthisalwayspossible?
![Page 38: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/38.jpg)
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)
![Page 39: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/39.jpg)
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;
![Page 40: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/40.jpg)
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;
![Page 41: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/41.jpg)
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.
![Page 42: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/42.jpg)
Whataboutthis?
D-INFK,ETHZurich,DataModelingandDatabases 42
create view ToughProf (PersNr, AvgGrade) asselect PersNr, avg(Grade)from testsgroup by PersNr;
delete ToughProfwhere PersNr = 4711;
![Page 43: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/43.jpg)
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.
![Page 44: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/44.jpg)
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
![Page 45: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/45.jpg)
D-INFK,ETHZurich,DataModelingandDatabases 45
![Page 46: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/46.jpg)
Recursion
D-INFK,ETHZurich,DataModelingandDatabases 46
![Page 47: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/47.jpg)
SQLWeSawsofarq SELECT… FROM… WHEREq Aggregationq Subqueryq NULLq SyntaxSugarsq Views
D-INFK,ETHZurich,DataModelingandDatabases 47
Aretheyenough?
![Page 48: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/48.jpg)
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?
![Page 49: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/49.jpg)
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?
![Page 50: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/50.jpg)
Limitsoftherelationalmodelq Recursionisanaturaloperationinprogramming
q Itisalsoanaturalwaytosearchsomedatamodelsbutnottherelationalmodel
q RecursionispossibleinSQL(butcomplexandinvolved)
q Considergraphdatabases…q …anexcursionintomoderndataprocessing.
D-INFK,ETHZurich,DataModelingandDatabases 50
![Page 51: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/51.jpg)
Friendsq Alotofdatainsocialmediaisagraph:
Ø FriendsØ LikesØ RecommendsØ CommunicatesØ ContactlistsØ …
q Processingsuchdatainvolvesgraphtraversals,DFSandBFS,graphisomorphism,…
D-INFK,ETHZurich,DataModelingandDatabases 51
![Page 52: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/52.jpg)
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
![Page 53: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/53.jpg)
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;
![Page 54: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/54.jpg)
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;
![Page 55: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/55.jpg)
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
![Page 56: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/56.jpg)
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))}
![Page 57: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/57.jpg)
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
![Page 58: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/58.jpg)
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?
![Page 59: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/59.jpg)
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 = ... ));
![Page 60: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/60.jpg)
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´) )
![Page 61: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/61.jpg)
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
![Page 62: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/62.jpg)
Howaboutthisone?
D-INFK,ETHZurich,DataModelingandDatabases 62
WITHRECURSIVER(r)AS
(SELECT 1UNION
SELECT r+1 FROM R)SELECTmin(r)FROM RLIMIT10;
PostgreSQLSyntax
![Page 63: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/63.jpg)
Howaboutthisone?
D-INFK,ETHZurich,DataModelingandDatabases 63
WITHRECURSIVER(r)AS
(SELECT 1UNION
SELECT r+1 FROM RWHERE r<20)SELECTmin(r)FROM RLIMIT10;
PostgreSQLSyntax
![Page 64: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/64.jpg)
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;
![Page 65: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/65.jpg)
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”.
![Page 66: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/66.jpg)
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)
![Page 67: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/67.jpg)
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
![Page 68: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/68.jpg)
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
![Page 69: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/69.jpg)
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
![Page 70: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/70.jpg)
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
![Page 71: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/71.jpg)
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)
![Page 72: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/72.jpg)
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)
![Page 73: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/73.jpg)
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?
![Page 74: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/74.jpg)
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”
![Page 75: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/75.jpg)
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
![Page 76: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/76.jpg)
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
![Page 77: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/77.jpg)
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?
![Page 78: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/78.jpg)
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)
![Page 79: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/79.jpg)
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?
![Page 80: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/80.jpg)
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)
![Page 81: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/81.jpg)
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?
![Page 82: Data Modeling and Databases Advanced SQL](https://reader030.vdocument.in/reader030/viewer/2022012503/617ceb8830401f13b93d4d88/html5/thumbnails/82.jpg)
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