sql: aggregation, joins, and triggers csc 343 winter 2018 · grouping we may follow a select...

53
SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 MICHAEL LIUT ( [email protected] ) DEPARTMENT OF MATHEMATICAL AND COMPUTATIONAL SCIENCES UNIVERSITY OF TORONTO MISSISSAUGA

Upload: others

Post on 06-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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

Page 2: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

AggregationOperators

Columnvaluesarecalculated,andasingle valueisreturned.

SUM,AVG,COUNT,MIN,andMAX.

These operationscanbeapplied onaSELECTclause inaquerytoproducetheaggregationonthecolumn.

e.g.COUNT(*)à countsthenumberoftuples inatable.

2

AggregationOperationsareColumnOperations!

Page 3: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

Example:Aggregation

FromSells(bar, beer,price), findtheaveragepriceofBud.

SELECTAVG(price)

FROMSells

WHEREbeer=‘Bud’;

3

Page 4: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

AggregationOperators

Anaggregationoperatormaynotappear intheWHEREclause unless itisinasubquerycontained inaHAVINGclauseoraSELECT list,andthecolumnbeingaggregated isanouterreference.

4

Anexamplewillshortlyfollow.

Page 5: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

DuplicatesinanAggregationToeliminate duplicates useDISTINCT insideanaggregation.

Example: Findthenumberofdifferent priceschargedforBud.

SELECT COUNT(DISTINCTprice)

FROMSells

WHERE beer=‘Bud’;

5

Page 6: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

NULLValuesinAggregation

NULLvaluesareNEVER contributed toaSUM,AVG,orCOUNT.

NULLvaluescanNEVER betheMINorMAXofacolumn.

However,ifallthevalues inthecolumnareNULL,thentheresultoftheaggregation isalsoNULL.

6

Exception: The COUNTofanemptyset is0.

Page 7: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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

Page 8: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

Example:ASimpleQuery

Findtheageoftheyoungestemployeeateachratinglevel.

SELECT MIN(age)

FROMEmployees

WHERErating=i;

Note: “i”represents aratingvalue.

8

Page 9: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

Grouping

WemayfollowaSELECT-FROM-WHERE expression byGROUPBYandalistofattributes.

Therelationthatresults fromtheSELECT-FROM-WHERE isgroupedaccordingtothevaluesofallthose attributes, andanyaggregation isapplied onlywithineachgroup.

9

SELECT rating,MIN(age)FROM EmployeesGROUPBYrating;

Page 10: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

Example:Grouping

FindtheaveragepriceforeachbeerfromSells(bar, beer,price).

SELECT beer,AVG(price)

FROMSells

GROUPBY beer;

10

beer AVG(price)Bud 2.33Miller 4.55… …

Result:

Page 11: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

SELECTclauseRestrictionswithAggregation

Tocontinuewiththeparagraphonslide#4:◦ Ifanyaggregation isused,theneachelement oftheSELECT listmustbeeither:

1. Aggregated,or2. AnattributeontheGROUPBY list.

11

Page 12: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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.

Page 13: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

IllegalQueryExample

SELECT bar,beer,MIN(price)

FROMSells

GROUPBY bar;

Thisqueryisillegal inSQL.

There isonlyonetuple outputforeachbar.Thus,nouniquewaytoselectwhichbeertooutput

13

Page 14: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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.

Page 15: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

HAVINGClauses

HAVING<condition> mayfollowaGROUPBYclause.

Ifso,thecondition applies toeachgroup,andgroupsnotsatisfying theconditionareeliminated.

15

Page 16: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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.

Page 17: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

RequirementsofHAVINGConditions

Anythinggoes inasubquery.

Outside subqueries, theymayrefertoattributes onlyiftheyareeither:

1. Agroupingattribute,or

2. Aggregated.

(samecondition asinSELECTclauseswithaggregation)

17

Page 18: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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!

Page 19: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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:

Page 20: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

CrossProduct

A.K.A.CartesianProduct.Denotedby:X

Evaluating joinsinvolvescombiningtwoormorerelations.

Giventworelations, S andR,eachrowofS ispairedwitheachrowofR.

Resulting Schema:oneattributefromeachattribute ofS andR.

20

Page 21: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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:

Page 22: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

CrossProduct

Ingeneral:

22

TableA TableB AXBRows N M N*M

Columns C K C+K

Page 23: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

23

Page 24: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

JoinedRelations

Joinoperations taketworelations andreturnsasaresultanotherrelation.

Ajoinoperation isaCartesian productwhichrequiresthattuples inthetworelationsmatch(undersomecondition). Italsospecifies theattributesthatarepresent intheresultofthejoin.

24

Page 25: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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.

Page 26: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

TypesofJoins

26

Page 27: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

27

Page 28: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

OuterJoin

Anextension ofthejoinoperationthatavoids lossofinformation.

SupposeyouhavetworelationsR andS.AtupleofR thathasnotuple ofSwithwhichitjoins issaidtobedangling.◦ Similarlyforatuple ofS.

Computes the joinandthenaddstuplesfromonerelationthatdoesnotmatchtuples intheotherrelationtotheresultofthe join.

Outerjoin preserves danglingtuples bypaddingthemwithNULL.

28

Page 29: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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:

Page 30: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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:

Page 31: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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:

Page 32: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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

Page 33: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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

Page 34: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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!

Page 35: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

Triggers

Aprocedurethatisautomatically invokedbytheDBMSinresponse tospecified changestothedatabase.◦ Typically invokedbytheDBA.

Adatabasethathasasetofassociated triggersiscalledanactivedatabase.

35

Page 36: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

Triggers

Atriggerdescription contains threeparts:

1. Event:achangetothedatabase activatesthetrigger.2. Condition: aqueryortestthatisrunwhenthetriggerisactivated.3. Action:aprocedurethat isexecutedwhenthetrigger isactivatedand

itscondition istrue.

36

Page 37: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

Triggers

Atriggercanbethoughtofasa‘daemon’whichmonitorsadatabase, andisexecuted whenthedatabase ismodified inawaythatmatchestheeventspecifications.

i.e.INSERT,UPDATE,andDELETE statements canactivateatrigger.

Usersareoftenunawarethatatriggerwasexecutes asasideeffectoftheirprogram.

37

Page 38: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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

Page 39: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

Triggers

Acondition inatriggercanbeaBoolean expression oraquery.

e.g.Allemployees salaries arelessthan$100,000.

Aqueryis interpretedasTRUEiftheanswerset isnon-empty.◦ This invokestheactionassociated withthetrigger.

Aqueryis interpretedasFALSEifithasnoanswers.◦ Noaction isinvoked.

39

Page 40: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

ImportantIssuesAssociatedWithTriggers

Whenatriggeraction executes inarelation,wemustspecifyatwhatpointinthesequence ofevents itoccurs.◦ BECAREFUL!

Depending onwhatthetriggerdoes,wemaywanttomodifyifitsactionoccursbefore orafter changesaremadetorelations.

40

Page 41: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

ImportantIssuesAssociatedWithTriggers

Example: let’ssaythatwehaveastatement thatINSERTsrecordsintoaStudent’stable.◦ Thisactivates atriggerthatmaintains statistics onhowmanystudentsyoungerthan18yearsofageareinserted atonetime.

Doesthistriggerhappenbefore orafterthechange?

41

Page 42: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

ImportantIssuesAssociatedWithTriggers

Doesthistriggerhappenbefore orafterthechange?

Youhavedifferentoptions, forexample:

1. Atriggerthatinitializes avariableusedtocountthenumberofqualifying insertions shouldbeexecuted before.

2. Atriggerthatexecutes onceafter eachrecordis inserted.◦ Maybewerequirethevalues inthenewrecordtodeterminetheaction.

42

Page 43: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

Example:Triggers

Let’ssaythatwehaveastatement thatINSERTsrecordsintoaStudent’stable.◦ Thisactivates atriggerthatmaintains statistics onhowmanystudentsyoungerthan18yearsofageareinserted atonetime.

Nowlet’sassume thatwemustexamine theage attributeoftheStudenttabletodecidewhethertoincrement thecount.◦ Thetriggeringeventherewouldoccurforeachmodifiedrecord.

43

Page 44: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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.

Page 45: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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

Page 46: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

ImportantNoteonTriggers

Watchoutforchainactivations!!!

Depending onyourexecution oftheaction,youcouldpotentially activateanothertrigger.Youcouldpotentially evenactivatethesametrigger.◦ These arecalledrecursivetriggers.

Recursivetriggersaremostlyunpredictable because ofthecombination ofchainactivationsandorderwhichtheDBMSprocesses thissequence canbedifficulttounderstand.

46

Page 47: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

Constraintsvs. TriggersTriggersarecommonlyusedtomaintaindatabase consistency.◦ MustensurethatanIntegrityConstraint isnotmoreproperlysuited.

Constraints areeasiertounderstandastheyarenotoperationally defined.

Constraints preventinconsistency byanykindofstatement.

Constraints affordtheopportunityfortheDBMStooptimize.

Triggersallowustoimposegeneral (flexible) constraints

Triggersalertuserstounusualevents(reflected inupdatestothedatabase).

Triggerscangeneratealogofevents tosupportauditing andsecuritychecks.

47

Page 48: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

TriggerFlexibility

48

Page 49: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

TriggerFlexibility

49

Page 50: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

AlerttoUnusualEvents

Wemaywanttocheck ifacustomerplacinganorderhasmadeenoughpurchases inthepastmonthtoqualifyforanadditional discount.

◦ How?Implement atriggertocheckrecentpurchasesanddisplaysmessage.◦ IFTRUE: InformtheSalesClerk.◦ Purpose?ToUPSELLorXSELLadditional productsastheyarereceiving adiscount.

50

Page 51: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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

Page 52: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

Questions?

52

I’LL BE ANSWERING QUESTIONS NOW

Q A&

THANKS FOR LISTENING

Page 53: SQL: Aggregation, Joins, and Triggers CSC 343 Winter 2018 · Grouping We may follow a SELECT -FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results

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