Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Triggers

Similar presentations


Presentation on theme: "Introduction to Triggers"— Presentation transcript:

1 Introduction to Triggers
Please use speaker notes for additional information! Triggers are named PL/SQL blocks. In that way, they are similar to procedures, however procedures are involved with an explicit call and a passing of arguments while a trigger event causes the implicit execution of the trigger code and arguments are not involved. A trigger event is associated with DML such as INSERT, UPDATE or DELETE and when it is executed it is referred to as the "firing" of the trigger.

2 Data in total_notes prior to execution.
triggers SQL> select * from total_notes; CHANGEDAT TOTAMT 27-JUL 27-JUL SQL> DESC total_notes Name Null? Type CHANGEDATE DATE TOTAMT NUMBER(6,2) Data in total_notes prior to execution. SQL> edit trigger1 CREATE OR REPLACE TRIGGER trigger1 AFTER UPDATE ON new_donation_one DECLARE v_date total_notes.changedate%TYPE :='27-JUL-99'; v_totamt total_notes.totamt%TYPE; BEGIN SELECT SUM(contamt) INTO v_totamt FROM new_donation_one; INSERT INTO total_notes (changedate, totamt) VALUES(v_date, v_totamt); END trigger1; / This trigger, named trigger1, will be implicitly activated based on an update done to the table called new_donation_one. After each update, I am collecting the sum of the contributions and putting it into total_notes. Following the keep in simple approach as opposed to the professional approach, I hardcoded the date into the code. In fact it would be better to have the system date or something similar in the date. Note that after coding the trigger, it has to be created before use. I did that trigger1. You need to be clear on the fact that a trigger is tired to an event. If any code that I write encounters an update for new_donation_one, this trigger will be fired. That can be tremendously beneficial in creating an event trail. Like other named blocks that are executed, the trigger must be created prior to use. Remember to that if you modify the trigger, you must recreate it. trigger1 Trigger created.

3 SQL> edit use_trigger1
triggers SQL> select * from new_donation_one; IDNO DRI CONTDATE CONTAMT JAN MAR MAR JUN JUN SQL> edit use_trigger1 SET VERIFY OFF DECLARE v_idno new_donation_one.idno%TYPE :='&input_idno'; v_driveno new_donation_one.driveno%TYPE :='&input_driveno'; v_contamt new_donation_one.contamt%TYPE :=&input_contamt; BEGIN UPDATE new_donation_one SET contamt = v_contamt WHERE idno = v_idno AND driveno = v_driveno; END; / SET VERIFY ON This is the code that does an update. The trigger shown on the previous slide is tied to updates on new_donation_one so when this update is encountered, the trigger is fired.

4 Enter value for input_idno: 44444 Enter value for input_driveno: 200
triggers SQL> select * from new_donation_one; IDNO DRI CONTDATE CONTAMT JAN MAR MAR JUN JUN use_trigger1 Enter value for input_idno: 44444 Enter value for input_driveno: 200 Enter value for input_contamt: 50 PL/SQL procedure successfully completed. SQL> SELECT * FROM new_donation_one; IDNO DRI CONTDATE CONTAMT JAN MAR MAR JUN JUN First I use_trigger1 with data that did not match so no update could take place. Note that the data in new_donation_one did not get changed. However because the update was attempted, the trigger was fired and total_notes had a line inserted. More complex code would be needed to prevent this. However note that the sum did not change. SQL> SELECT * FROM total_notes; CHANGEDAT TOTAMT 27-JUL 27-JUL

5 Enter value for input_idno: 23456 Enter value for input_driveno: 300
triggers @ use_trigger1 executes the routine that updates the new_donation_one table. In this case I am changing the contribution from person to 99. The update triggers the execution of the trigger event which adds a record containing the new sum to the total_notes table. use_trigger1 Enter value for input_idno: 23456 Enter value for input_driveno: 300 Enter value for input_contamt: 99 PL/SQL procedure successfully completed. SQL> SELECT * FROM new_donation_one; IDNO DRI CONTDATE CONTAMT JAN MAR MAR JUN JUN SQL> SELECT * FROM total_notes; CHANGEDAT TOTAMT 27-JUL 27-JUL 27-JUL The amount for used to be 45 - it has now changed to 99 because of the update done on contamt.

6 CREATE OR REPLACE TRIGGER trigger1 AFTER UPDATE ON new_donation_one
triggers SQL> edit trigger1 CREATE OR REPLACE TRIGGER trigger1 AFTER UPDATE ON new_donation_one DECLARE v_date total_notes.changedate%TYPE :=sysdate; v_totamt total_notes.totamt%TYPE; BEGIN SELECT SUM(contamt) INTO v_totamt FROM new_donation_one; INSERT INTO total_notes (changedate, totamt) VALUES(v_date, v_totamt); END trigger1; / In this example, I substituted sysdate for the hardcoded date to make a more realistic trigger. Note that because I changed the trigger, I needed to to create it again. trigger1 Trigger created. The change to the trigger has now been made so when I update new_donation_one the trigger will use the system date.

7 Input truncated to 1 characters Enter value for input_idno: 22222
trigger use_trigger1 Input truncated to 1 characters Enter value for input_idno: 22222 Enter value for input_driveno: 100 Enter value for input_contamt: 50 PL/SQL procedure successfully completed. SQL> SELECT * FROM new_donation_one; IDNO DRI CONTDATE CONTAMT JAN MAR MAR JUN JUN SQL> SELECT * FROM total_notes; CHANGEDAT TOTAMT 27-JUL 27-JUL 27-JUL 02-AUG SQL> SELECT * FROM new_donation_one; IDNO DRI CONTDATE CONTAMT JAN MAR MAR JUN JUN Data prior to the update. Data after the update. The trigger now uses the system date instead of the hard coded date so the latest update has August second as its date. Record used to have 10, it got updated to 50. Remember the update is based on a match to both idno and driveno which would make the records unique as long as two donations were not made by the same person to the same drive on the same date. When the update first the trigger, the new total is written with the system date. Note that the new total sums what is currently in new_donation_one. Since I changed 10 to 50, the total is 40 bigger.


Download ppt "Introduction to Triggers"

Similar presentations


Ads by Google