sql: aggregation, joins, and triggers csc 343 winter 2018 · grouping we may follow a select...
TRANSCRIPT
SQL:Aggregation,Joins,andTriggersCSC343Winter2018MICHAEL L IUT(MICHAEL.L [email protected])DEPARTMENT OF MATH EMAT ICA L ANDCOMPUTAT IONA L SC IENCE SUN IV E RS IT Y OF TORONTO M ISS ISSAUGA
AggregationOperators
Columnvaluesarecalculated,andasingle valueisreturned.
SUM,AVG,COUNT,MIN,andMAX.
These operationscanbeapplied onaSELECTclause inaquerytoproducetheaggregationonthecolumn.
e.g.COUNT(*)à countsthenumberoftuples inatable.
2
AggregationOperationsareColumnOperations!
Example:Aggregation
FromSells(bar, beer,price), findtheaveragepriceofBud.
SELECTAVG(price)
FROMSells
WHEREbeer=‘Bud’;
3
AggregationOperators
Anaggregationoperatormaynotappear intheWHEREclause unless itisinasubquerycontained inaHAVINGclauseoraSELECT list,andthecolumnbeingaggregated isanouterreference.
4
Anexamplewillshortlyfollow.
DuplicatesinanAggregationToeliminate duplicates useDISTINCT insideanaggregation.
Example: Findthenumberofdifferent priceschargedforBud.
SELECT COUNT(DISTINCTprice)
FROMSells
WHERE beer=‘Bud’;
5
NULLValuesinAggregation
NULLvaluesareNEVER contributed toaSUM,AVG,orCOUNT.
NULLvaluescanNEVER betheMINorMAXofacolumn.
However,ifallthevalues inthecolumnareNULL,thentheresultoftheaggregation isalsoNULL.
6
Exception: The COUNTofanemptyset is0.
Example:TheNULLEffectSELECTCOUNT(*)
FROMSells
WHEREbeer=‘Bud’;
SELECTCOUNT(price)
FROMSells
WHEREbeer=‘Bud’;
7
Thenumber of barsthatsellBud.
Thenumber of barsthatsellBudataknown price.
i.e.where theprice isNOTNULL.
Recall: Sells(bar,beer,price).
Example:ASimpleQuery
Findtheageoftheyoungestemployeeateachratinglevel.
SELECT MIN(age)
FROMEmployees
WHERErating=i;
Note: “i”represents aratingvalue.
8
Grouping
WemayfollowaSELECT-FROM-WHERE expression byGROUPBYandalistofattributes.
Therelationthatresults fromtheSELECT-FROM-WHERE isgroupedaccordingtothevaluesofallthose attributes, andanyaggregation isapplied onlywithineachgroup.
9
SELECT rating,MIN(age)FROM EmployeesGROUPBYrating;
Example:Grouping
FindtheaveragepriceforeachbeerfromSells(bar, beer,price).
SELECT beer,AVG(price)
FROMSells
GROUPBY beer;
10
beer AVG(price)Bud 2.33Miller 4.55… …
Result:
SELECTclauseRestrictionswithAggregation
Tocontinuewiththeparagraphonslide#4:◦ Ifanyaggregation isused,theneachelement oftheSELECT listmustbeeither:
1. Aggregated,or2. AnattributeontheGROUPBY list.
11
Example:GroupingForeachdrinker, findtheaveragepriceofBudatthebarstheyfrequent.Youwilluse:Sells(bar,beer,price) andFrequents(drinker, bar) forthisquery.
SELECT drinker, AVG(price)
FROMFrequents, Sells
WHEREbeer=‘Bud’ANDFrequents.bar =Sells.bar
GROUPBYdrinker;
12
Compute alldrinker-bar-pricetriples for ‘Bud’.
Then group themby drinker.
IllegalQueryExample
SELECT bar,beer,MIN(price)
FROMSells
GROUPBY bar;
Thisqueryisillegal inSQL.
There isonlyonetuple outputforeachbar.Thus,nouniquewaytoselectwhichbeertooutput
13
IllegalQueryExampleInDetail
SELECT bar,beer,MIN(price) ASminPriceFROMSellsGROUPBY bar;
14
bar beer minPrice
Joe ? 3.00
Tom ? 3.50
Jane ? 3.25
Result:
{Bud, Miller,Coors}?
bar beer price
Joe Bud 3.00
Joe Miller 4.00
Tom Bud 3.50
Tom Miller 4.25
Jane Bud 3.25
Jane Miller 4.75
Jane Coors 4.00
Only one tuple output foreachbar,no unique waytoselect
which beer tooutput.
Sells:
Ideally, wewouldwanttoGROUPBY
beer.
HAVINGClauses
HAVING<condition> mayfollowaGROUPBYclause.
Ifso,thecondition applies toeachgroup,andgroupsnotsatisfying theconditionareeliminated.
15
Example:HavingFromSells(bar,beer,price) and Beers(name,manf),findtheaveragepriceofthebeersthatareeitherservedinatleastthreebarsoraremanufacturesbyPete’s.
SELECTbeer,AVG(price)FROMSellsGROUPBYbeerHAVINGCOUNT(bar)>=3ORbeerIN(SELECTname
FROMBeersWHEREmanf =‘Pete”s’
);
16
Beersmanufactured byPete’s.
Beergroupswith atleast3non-NULLbars.
RequirementsofHAVINGConditions
Anythinggoes inasubquery.
Outside subqueries, theymayrefertoattributes onlyiftheyareeither:
1. Agroupingattribute,or
2. Aggregated.
(samecondition asinSELECTclauseswithaggregation)
17
AggregationOperatorsAnaggregationoperatormaynotappear intheWHEREclause unless itisinasubquerycontained inaHAVINGclause oraSELECT list,andthecolumnbeingaggregated isanouterreference.
18
SELECT empID,SUM(amount)FROM SalesGROUPBYEmployeeHAVING SUM(amount)>20000;
OR
SELECT empID,SUM(amount)FROM SalesGROUPBYEmployeeWHEREempID IN(
SELECTMAX(empID)FROMEmployees);
Recall: AggregationOperationsareColumnOperations!
AFinalExample
SELECT bar,SUM(qty)ASsumQFROMSellsGROUPBY barHAVINGSUM(qty)>4;
19
bar sumQ
Tom 5
Jane 6
Result:
bar beer price qty
Joe Bud 3.00 2
Joe Miller 4.00 2
Tom Bud 3.50 1
Tom Miller 4.25 4
Jane Bud 3.25 1
Jane Miller 4.75 3
Jane Coors 4.00 2
Sells:
CrossProduct
A.K.A.CartesianProduct.Denotedby:X
Evaluating joinsinvolvescombiningtwoormorerelations.
Giventworelations, S andR,eachrowofS ispairedwitheachrowofR.
Resulting Schema:oneattributefromeachattribute ofS andR.
20
Example:CrossProduct
SELECT drinkerFROMFrequents,SellsWHERE beer=‘Bud’ANDFrequents.bar =Sells.bar;
21
bar beer price
Joe Bud 3.00
Tom Miller 4.00
Jane Lite 3.25
Sells:
drinker bar
Aaron Joe
Mary Jane
Frequents: (bar) beer price drinker (bar)
Joe Bud 3.00 Aaron Joe
Joe Bud 3.00 Mary Jane
Tom Miller 4.00 Aaron Joe
Tom Miller 4.00 Mary Jane
Jane Lite 3.25 Aaron Joe
Jane Lite 3.25 Mary Jane
Sells X Frequents
drinker
Aaron
Result:
CrossProduct
Ingeneral:
22
TableA TableB AXBRows N M N*M
Columns C K C+K
23
JoinedRelations
Joinoperations taketworelations andreturnsasaresultanotherrelation.
Ajoinoperation isaCartesian productwhichrequiresthattuples inthetworelationsmatch(undersomecondition). Italsospecifies theattributesthatarepresent intheresultofthejoin.
24
Example:JoinOperations
25
course_id title dept_name credits
BIO-301 Genetics Biology 4
CS-190 Game Design Comp Sci 4
CS-315 Robotics Comp Sci 3
Course:
course_id prereq_id
BIO-301 BIO-101
CS-190 CS-101
CS-347 CS-101
Prereq:
Observe:1. Theprerequisite information ismissing for CS-315.2. Thecourse information ismissing forCS-347.
TypesofJoins
26
27
OuterJoin
Anextension ofthejoinoperationthatavoids lossofinformation.
SupposeyouhavetworelationsR andS.AtupleofR thathasnotuple ofSwithwhichitjoins issaidtobedangling.◦ Similarlyforatuple ofS.
Computes the joinandthenaddstuplesfromonerelationthatdoesnotmatchtuples intheotherrelationtotheresultofthe join.
Outerjoin preserves danglingtuples bypaddingthemwithNULL.
28
LeftOuterJoin
29
course_id title dept_name credits
BIO-301 Genetics Biology 4
CS-190 Game Design Comp Sci 4
CS-315 Robotics Comp Sci 3
Course:
course_id prereq_id
BIO-301 BIO-101
CS-190 CS-101
CS-347 CS-101
Prereq:
course_id title dept_name credits prereq_id
BIO-301 Genetics Biology 4 BIO-101
CS-190 Game Design Comp Sci 4 CS-101
CS-315 Robotics Comp Sci 3 NULL
Result:
RightOuterJoin
30
course_id title dept_name credits
BIO-301 Genetics Biology 4
CS-190 Game Design Comp Sci 4
CS-315 Robotics Comp Sci 3
Course:
course_id prereq_id
BIO-301 BIO-101
CS-190 CS-101
CS-347 CS-101
Prereq:
course_id title dept_name credits prereq_id
BIO-301 Genetics Biology 4 BIO-101
CS-190 Game Design Comp Sci 4 CS-101
CS-347 NULL NULL NULL CS-101
Result:
FullOuterJoin
31
course_id title dept_name credits
BIO-301 Genetics Biology 4
CS-190 Game Design Comp Sci 4
CS-315 Robotics Comp Sci 3
Course:
course_id prereq_id
BIO-301 BIO-101
CS-190 CS-101
CS-347 CS-101
Prereq:
course_id title dept_name credits prereq_id
BIO-301 Genetics Biology 4 BIO-101
CS-190 Game Design Comp Sci 4 CS-101
CS-315 Robotics Comp Sci 3 NULL
CS-347 NULL NULL NULL CS-101
Result:
InnerJoin
32
course_id title dept_name credits
BIO-301 Genetics Biology 4
CS-190 Game Design Comp Sci 4
CS-315 Robotics Comp Sci 3
Course:
course_id prereq_id
BIO-301 BIO-101
CS-190 CS-101
CS-347 CS-101
Prereq:
course_id title dept_name credits prereq_id
BIO-301 Genetics Biology 4 BIO-101
CS-190 Game Design Comp Sci 4 CS-101
Result:
Course INNER JOIN Prereq ONCourse.course_id =Prereq.course_id
OuterjoinsR OUTERJOINS isthecoreofanouter joinexpression.
Modified bythefollowing:1. OptionalNATURAL infrontofOUTER.
◦ Checkequality onallcommonattributes.◦ Notwoattributeswiththesame nameintheoutput.
2. OptionalON<condition> afterJOIN.3. OptionalLEFT,RIGHT,orFULLinfrontofOUTER.
◦ LEFT=paddanglingtuples ofR only.◦ RIGHT=paddanglingtuples ofS only.◦ FULL=padboth;this istheDEFAULTchoice.
33
Example:OuterJoin
A B1 24 5
34
A B C1 2 34 5 NULL
NULL 6 7
B C2 36 7
R: S:
Result:
RNATURALFULLOUTERJOINS
NOTE: (1,2) joins with(2,3), but theothertwotuples aredangling!
Triggers
Aprocedurethatisautomatically invokedbytheDBMSinresponse tospecified changestothedatabase.◦ Typically invokedbytheDBA.
Adatabasethathasasetofassociated triggersiscalledanactivedatabase.
35
Triggers
Atriggerdescription contains threeparts:
1. Event:achangetothedatabase activatesthetrigger.2. Condition: aqueryortestthatisrunwhenthetriggerisactivated.3. Action:aprocedurethat isexecutedwhenthetrigger isactivatedand
itscondition istrue.
36
Triggers
Atriggercanbethoughtofasa‘daemon’whichmonitorsadatabase, andisexecuted whenthedatabase ismodified inawaythatmatchestheeventspecifications.
i.e.INSERT,UPDATE,andDELETE statements canactivateatrigger.
Usersareoftenunawarethatatriggerwasexecutes asasideeffectoftheirprogram.
37
DaemonAcomputerprogramthatrunsasabackgroundprocess insteadofbeingcontrolledbyaninteractive user.
e.g.SSHD isthedaemonthatserves incomingSSH connections.
This isseen incomputeroperatingsystems, specifically inmultitasking.
InUNIX,theparentprocessofadaemon isusuallythe init process.
NowbacktoTriggers…
38
No, not the ancient Greek word thatrefers to benevolent or benignnatural spirits… I am talking aboutthem in the computing sense! J
Triggers
Acondition inatriggercanbeaBoolean expression oraquery.
e.g.Allemployees salaries arelessthan$100,000.
Aqueryis interpretedasTRUEiftheanswerset isnon-empty.◦ This invokestheactionassociated withthetrigger.
Aqueryis interpretedasFALSEifithasnoanswers.◦ Noaction isinvoked.
39
ImportantIssuesAssociatedWithTriggers
Whenatriggeraction executes inarelation,wemustspecifyatwhatpointinthesequence ofevents itoccurs.◦ BECAREFUL!
Depending onwhatthetriggerdoes,wemaywanttomodifyifitsactionoccursbefore orafter changesaremadetorelations.
40
ImportantIssuesAssociatedWithTriggers
Example: let’ssaythatwehaveastatement thatINSERTsrecordsintoaStudent’stable.◦ Thisactivates atriggerthatmaintains statistics onhowmanystudentsyoungerthan18yearsofageareinserted atonetime.
Doesthistriggerhappenbefore orafterthechange?
41
ImportantIssuesAssociatedWithTriggers
Doesthistriggerhappenbefore orafterthechange?
Youhavedifferentoptions, forexample:
1. Atriggerthatinitializes avariableusedtocountthenumberofqualifying insertions shouldbeexecuted before.
2. Atriggerthatexecutes onceafter eachrecordis inserted.◦ Maybewerequirethevalues inthenewrecordtodeterminetheaction.
42
Example:Triggers
Let’ssaythatwehaveastatement thatINSERTsrecordsintoaStudent’stable.◦ Thisactivates atriggerthatmaintains statistics onhowmanystudentsyoungerthan18yearsofageareinserted atonetime.
Nowlet’sassume thatwemustexamine theage attributeoftheStudenttabletodecidewhethertoincrement thecount.◦ Thetriggeringeventherewouldoccurforeachmodifiedrecord.
43
Example:TriggersCREATE TRIGGER init_count BEFORE INSERTONStudents /*Event*/
DECLAREcountINTEGER;
BEGIN /*Action*/count:=0;
END
CREATE TRIGGER incr_count AFTER INSERTONStudents /*Event*/WHEN (new.age <18) /*Condition*/FOREACHROWBEGIN /*Action*/
count:=count+1;END
44
Called arow-level trigger.
“new” isjust aninsertedtuple.
Note: Theinit_count triggerisexecutedonceperINSERT,thus FOREACHROWwasomitted, however, itwould be
considered astatement-leveltrigger.
Row-LevelTriggervs.Statement-LevelTrigger
Astatement-level triggerisactivatedonce.Arow-leveltrigger isactivated onceperiterationoftheloop.
e.g.UPDATEattribute
SETcolumnOne =columnOne +1;
Thestatement-level triggerwillbeactivatedonetime(evenifnorowsareupdated).Therow-leveltriggerwillbeactivatedmillions oftimes (dependent onthesize/iters).
45
ImportantNoteonTriggers
Watchoutforchainactivations!!!
Depending onyourexecution oftheaction,youcouldpotentially activateanothertrigger.Youcouldpotentially evenactivatethesametrigger.◦ These arecalledrecursivetriggers.
Recursivetriggersaremostlyunpredictable because ofthecombination ofchainactivationsandorderwhichtheDBMSprocesses thissequence canbedifficulttounderstand.
46
Constraintsvs. TriggersTriggersarecommonlyusedtomaintaindatabase consistency.◦ MustensurethatanIntegrityConstraint isnotmoreproperlysuited.
Constraints areeasiertounderstandastheyarenotoperationally defined.
Constraints preventinconsistency byanykindofstatement.
Constraints affordtheopportunityfortheDBMStooptimize.
Triggersallowustoimposegeneral (flexible) constraints
Triggersalertuserstounusualevents(reflected inupdatestothedatabase).
Triggerscangeneratealogofevents tosupportauditing andsecuritychecks.
47
TriggerFlexibility
48
TriggerFlexibility
49
AlerttoUnusualEvents
Wemaywanttocheck ifacustomerplacinganorderhasmadeenoughpurchases inthepastmonthtoqualifyforanadditional discount.
◦ How?Implement atriggertocheckrecentpurchasesanddisplaysmessage.◦ IFTRUE: InformtheSalesClerk.◦ Purpose?ToUPSELLorXSELLadditional productsastheyarereceiving adiscount.
50
SomeReviewQuestions1. Whatarenested queries?HowandwhenwouldyouusetheoperatorsIN,
EXISTS,UNIQUE,ANY,andALL?
2. WhatareNULLvalues?Aretheysupported intherelationalmodel?Howdotheyaffectthemeaningofqueries? CanprimarykeyfieldsofatablecontainNULLvalues?
3. Whatisatrigger?Whatareits3parts?Whatisthedifferencebetween arow-levelandstatement-level trigger?
4. Whatisgrouping?Whatisits interaction oftheHAVINGandWHEREclauses?Mention anyrestrictions thatmustbesatisfies bythefieldswhichappear intheGROUPBYclause.
51
Questions?
52
I’LL BE ANSWERING QUESTIONS NOW
Q A&
THANKS FOR LISTENING
Citations,ImagesandResourcesDatabaseManagement Systems(3rd Ed.),Ramakrishnan &Gehrke
Somecontentisbased offtheslidesofDr.Fei Chiang- http://www.cas.mcmaster.ca/~fchiang/
http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/06112016031910AM/sql.png
https://lukaseder.files.wordpress.com/2015/10/venn.png?w=662
http://stackoverflow.com/questions/6319183/aggregate-function-in-sql-where-clause
http://www.sql-server-performance.com/2007/aggregate-may-not-appear-in-where-clause/
http://bioinfo.mbb.yale.edu/course/projects/talk-1/db07.html
53