Triggers 1 What is a Trigger A trigger
![Triggers 1 Triggers 1](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-1.jpg)
![What is a Trigger? • A trigger is a PL/SQL block that is automatically What is a Trigger? • A trigger is a PL/SQL block that is automatically](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-2.jpg)
![General Form of a Trigger CREATE [or REPLACE] TRIGGER trig_name {BEFORE | AFTER | General Form of a Trigger CREATE [or REPLACE] TRIGGER trig_name {BEFORE | AFTER |](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-3.jpg)
![Creating Triggers • Trigger timing – For table: BEFORE, AFTER – For view: INSTEAD Creating Triggers • Trigger timing – For table: BEFORE, AFTER – For view: INSTEAD](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-4.jpg)
![Before/After/Instead of Trigger • Trigger timing: When should the trigger fire? • BEFORE: Execute Before/After/Instead of Trigger • Trigger timing: When should the trigger fire? • BEFORE: Execute](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-5.jpg)
![Row/Statement Trigger • Should the trigger body execute for each row the statement affects Row/Statement Trigger • Should the trigger body execute for each row the statement affects](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-6.jpg)
![Backing Up Data create table sailors( sid number, sname VARCHAR 2(30), rating number check(rating Backing Up Data create table sailors( sid number, sname VARCHAR 2(30), rating number check(rating](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-7.jpg)
![Backing Up Data CREATE or REPLACE TRIGGER backup_sailors_trig AFTER UPDATE of Rating on Sailors Backing Up Data CREATE or REPLACE TRIGGER backup_sailors_trig AFTER UPDATE of Rating on Sailors](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-8.jpg)
![Ensuring Upper Case CREATE or REPLACE TRIGGER sname_trig BEFORE INSERT or UPDATE of sname Ensuring Upper Case CREATE or REPLACE TRIGGER sname_trig BEFORE INSERT or UPDATE of sname](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-9.jpg)
![Instead Of Trigger create view sailors_reserves as select sailors. *, reserves. bid, reserves. day Instead Of Trigger create view sailors_reserves as select sailors. *, reserves. bid, reserves. day](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-10.jpg)
![Statement Trigger CREATE or REPLACE TRIGGER shabbat_trig BEFORE INSERT or DELETE or UPDATE on Statement Trigger CREATE or REPLACE TRIGGER shabbat_trig BEFORE INSERT or DELETE or UPDATE on](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-11.jpg)
![Mutating Table • You cannot select/insert/delete/update a table in a trigger, that is currently Mutating Table • You cannot select/insert/delete/update a table in a trigger, that is currently](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-12.jpg)
![Bad Trigger CREATE or REPLACE TRIGGER mutating_trig BEFORE DELETE reserves • on. You cannot Bad Trigger CREATE or REPLACE TRIGGER mutating_trig BEFORE DELETE reserves • on. You cannot](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-13.jpg)
![Additional Types of Triggers • Can also define triggers for – logging in and Additional Types of Triggers • Can also define triggers for – logging in and](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-14.jpg)
- Slides: 14
![Triggers 1 Triggers 1](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-1.jpg)
Triggers 1
![What is a Trigger A trigger is a PLSQL block that is automatically What is a Trigger? • A trigger is a PL/SQL block that is automatically](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-2.jpg)
What is a Trigger? • A trigger is a PL/SQL block that is automatically called when certain events occur in the database. • Triggers can be made to run when rows are inserted, deleted or updated. • Triggers can be run before or after the action. • Triggers can be run once per statement or once per row affected. 2
![General Form of a Trigger CREATE or REPLACE TRIGGER trigname BEFORE AFTER General Form of a Trigger CREATE [or REPLACE] TRIGGER trig_name {BEFORE | AFTER |](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-3.jpg)
General Form of a Trigger CREATE [or REPLACE] TRIGGER trig_name {BEFORE | AFTER | INSTEAD OF} {DELETE | INSERT | UPDATE [of column [, column]. . . ] } [or {DELETE | INSERT | UPDATE [of column [, column]. . . ] }. . . ] on {table_name | view_name} [FOR EACH ROW] [WHEN (condition)] PL/SQL block 3
![Creating Triggers Trigger timing For table BEFORE AFTER For view INSTEAD Creating Triggers • Trigger timing – For table: BEFORE, AFTER – For view: INSTEAD](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-4.jpg)
Creating Triggers • Trigger timing – For table: BEFORE, AFTER – For view: INSTEAD OF • Triggering event: INSERT, DELETE or UPDATE (possibly of specific columns) • On table or view • Trigger type: Row or statement • When clause: Restricts when trigger is run • Trigger body: PL/SQL block 4
![BeforeAfterInstead of Trigger Trigger timing When should the trigger fire BEFORE Execute Before/After/Instead of Trigger • Trigger timing: When should the trigger fire? • BEFORE: Execute](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-5.jpg)
Before/After/Instead of Trigger • Trigger timing: When should the trigger fire? • BEFORE: Execute the trigger body before the triggering DML event on a table. • AFTER: Execute the trigger body after the triggering DML event on a table. • INSTEAD OF: Execute the trigger body instead of the triggering statement. Used for VIEWS that are not otherwise modifiable. 5
![RowStatement Trigger Should the trigger body execute for each row the statement affects Row/Statement Trigger • Should the trigger body execute for each row the statement affects](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-6.jpg)
Row/Statement Trigger • Should the trigger body execute for each row the statement affects or only once? • Statement: The trigger body executes once for the triggering event. This is the default. • Row: The trigger body executes once for each row affected by the triggering event. • Is it possible that a Statement trigger will run more times than a row trigger? 6
![Backing Up Data create table sailors sid number sname VARCHAR 230 rating number checkrating Backing Up Data create table sailors( sid number, sname VARCHAR 2(30), rating number check(rating](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-7.jpg)
Backing Up Data create table sailors( sid number, sname VARCHAR 2(30), rating number check(rating <= 10), age number ); create table sailors_audit( who varchar 2(30), when_changed date, sid number, old_rating number, new_rating number ); 7
![Backing Up Data CREATE or REPLACE TRIGGER backupsailorstrig AFTER UPDATE of Rating on Sailors Backing Up Data CREATE or REPLACE TRIGGER backup_sailors_trig AFTER UPDATE of Rating on Sailors](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-8.jpg)
Backing Up Data CREATE or REPLACE TRIGGER backup_sailors_trig AFTER UPDATE of Rating on Sailors FOR EACH ROW WHEN (old. rating < new. rating) BEGIN INSERT INTO sailors_audit VALUES (USER, SYSDATE, : old. sid, : old. rating, : new. rating); END; / • What happens if update fails? • Why AFTER Trigger? 8
![Ensuring Upper Case CREATE or REPLACE TRIGGER snametrig BEFORE INSERT or UPDATE of sname Ensuring Upper Case CREATE or REPLACE TRIGGER sname_trig BEFORE INSERT or UPDATE of sname](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-9.jpg)
Ensuring Upper Case CREATE or REPLACE TRIGGER sname_trig BEFORE INSERT or UPDATE of sname on Sailors FOR EACH ROW BEGIN : new. sname : = UPPER(: new. sname); END; / • Why BEFORE Trigger? 9
![Instead Of Trigger create view sailorsreserves as select sailors reserves bid reserves day Instead Of Trigger create view sailors_reserves as select sailors. *, reserves. bid, reserves. day](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-10.jpg)
Instead Of Trigger create view sailors_reserves as select sailors. *, reserves. bid, reserves. day from sailors, reserves where sailors. sid = reserves. sid; CREATE or REPLACE TRIGGER view_trig INSTEAD OF INSERT on sailors_reserves FOR EACH ROW BEGIN INSERT INTO sailors values(: new. sname, : new. sid, : new. rating, : new. age); INSERT INTO reserves values(: new. sid, : new. bid, : new. day); END; / 10
![Statement Trigger CREATE or REPLACE TRIGGER shabbattrig BEFORE INSERT or DELETE or UPDATE on Statement Trigger CREATE or REPLACE TRIGGER shabbat_trig BEFORE INSERT or DELETE or UPDATE on](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-11.jpg)
Statement Trigger CREATE or REPLACE TRIGGER shabbat_trig BEFORE INSERT or DELETE or UPDATE on reserves DECLARE shabbat_exception EXCEPTION; BEGIN if (TO_CHAR (sysdate, 'DY')='SAT') then raise shabbat_exception; end if; END; / • What happens if exception is thrown? • Why BEFORE Trigger? 11
![Mutating Table You cannot selectinsertdeleteupdate a table in a trigger that is currently Mutating Table • You cannot select/insert/delete/update a table in a trigger, that is currently](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-12.jpg)
Mutating Table • You cannot select/insert/delete/update a table in a trigger, that is currently being affected by the DML operation • Such a table (that is currently affected) is said to be "mutating" 12
![Bad Trigger CREATE or REPLACE TRIGGER mutatingtrig BEFORE DELETE reserves on You cannot Bad Trigger CREATE or REPLACE TRIGGER mutating_trig BEFORE DELETE reserves • on. You cannot](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-13.jpg)
Bad Trigger CREATE or REPLACE TRIGGER mutating_trig BEFORE DELETE reserves • on. You cannot select/insert/delete/update a DECLARE empty_table_exception table in a trigger, that EXCEPTION; is currently being cnt NUMBER; affected by the DML operation BEGIN SELECT count(*) INTO cnt FROM reserves; if (cnt = 1) then raise empty_table_exception; end if; END; / 13
![Additional Types of Triggers Can also define triggers for logging in and Additional Types of Triggers • Can also define triggers for – logging in and](https://slidetodoc.com/presentation_image_h2/7743502ba532317e11b31600cea70617/image-14.jpg)
Additional Types of Triggers • Can also define triggers for – logging in and off – create/drop table events – system errors – etc. 14
Chocolate migraine mnemonic
Synectics art
Triggers of mass movement
Triggers and active database in dbms
Wide open triggers
Presupposition examples
Epilepsy trigger
How to identify anger triggers
Azure logic app multiple triggers
Presupposition triggers
Synectic triggers
Presupposition triggers
What triggers a dot audit
Triggers and routines in sql
Triggers and assertions