Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 19: Triggers1 Chapter Nineteen Triggers Objective: – Understanding Triggers – Types of Triggers – Applications of Triggers – How to work with Triggers.

Similar presentations


Presentation on theme: "Chapter 19: Triggers1 Chapter Nineteen Triggers Objective: – Understanding Triggers – Types of Triggers – Applications of Triggers – How to work with Triggers."— Presentation transcript:

1 Chapter 19: Triggers1 Chapter Nineteen Triggers Objective: – Understanding Triggers – Types of Triggers – Applications of Triggers – How to work with Triggers

2 Chapter 19: Triggers2 Triggers Introduction “PL/SQL programs that run automatically when an event occurs” Triggers are used for: 1.Maintaining complex integrity constraints 2.Auditing information in a table by recording the changes made and who made them 3.Automatically signaling to other programs that action needs to take place.

3 Chapter 19: Triggers3 Types of Triggers: Row-level Statement-level BEFORE AFTER INSTEAD OF…

4 Chapter 19: Triggers4 Types of Events that can have Trigger Code Attached to Them 1.DML 2.DDL 3.Database events (System Triggers) 4.INSTEAD OF

5 Chapter 19: Triggers5 Types of Triggers: 1 - DML Triggers –Fired by a DML statement –Defined for INSERT, UPDATE, DELETE –Fired before or after the operation –Fired on row or statement operations

6 Chapter 19: Triggers6 Example: CREATE TABLE Student_Stat( Major VARCHAR2(20),Total_cr NUMBER, Total_Student NUMBER); CREATE OR REPLACE TRIGGER exam1 AFTER INSERT OR DELETE OR UPDATE ON Students DECLARE CURSOR S_Cursor IS SELECT Major, count(*) Total_Student, sum(cr) Total_cr FROM Students GROUP BY Major; BEGIN DELETE FROM student_Stat; FOR I IN S_Cursor LOOP INSERT INTO Student_Stat (Major, Total_Student, Total_cr) VALUES (I.Major, I.Total_Student, I.Total_cr); END LOOP; END;

7 Chapter 19: Triggers7 Creating DMLTriggers Syntax: CREATE [OR REPLACE] TRIGGER TriggerName {BEFORE | AFTER} {INSERT | DELETE | UPDATE [OF columns]} ON tableName [FOR EACH ROW] [WHEN (…)] [DECLARE …] Body;

8 Chapter 19: Triggers8 Example CREATE TABLE date_Stat( User_idVARCHAR(30), Object_Type VARCHAR2(20), Object_Name VARCHAR2(30), Object_Owner VARCHAR2(30), Creation_Date Date); CREATE OR REPLACE TRIGGER DD_Stat AFTER CREATE ON SCHEMA BEGIN INSERT INTO date_Stat (User_id, Object_Type, Object_Name, Object_Owner, Creation_Date) VALUES (USER, SYS.DICTIONARY_OBJ_TYPE, SYS.DICTIONARY_OBJ_NAME, SYS.DICTIONARY_OBJ_OWNER, SYSDATE); END;

9 Chapter 19: Triggers9 Creating DML Triggers Possible types of triggers: CategoryValues StatementINSERT DELETE UPDATE TimingBEFORE AFTER LevelRow Statement

10 Chapter 19: Triggers10 Order of DML Trigger Firing 1.Execute the before Statement_level Trigger 2.For each row a.Execute the before row_level triggers b.Execute the statement itself c.Execute the after row_level 3.Execute the after statement_level triggers

11 Chapter 19: Triggers11 Example CREATE SEQUENCE Trig_Seq START WITH 1 INCREMENT BY 1; CREATE OR REPLACE PACKAGE Trig_Pack AS V_counter NUMBER; END; CREATE OR REPLACE TRIGGER classBase_Trig BEFORE UPDATE ON classes BEGIN Trig_Pack.V_counter := 0; INSERT INTO Temp (Num_col, Char_col) VALUES (Trig_Seq.NEXTVAL, ‘Before statement counter =’ || Trig_Pack.V_counter); Trig_Pack.V_counter := Trig_Pack.V_counter +1; END;

12 Chapter 19: Triggers12 Example (Continued) CREATE OR REPLACE TRIGGER class1_Trig AFTER UPDATE ON classes BEGIN INSERT INTO Temp (Num_col, Char_col) VALUES (Trig_Seq.NEXTVAL, ‘After statement 1 counter =‘ || Trig_Pack.V_counter); Trig_Pack.V_counter := Trig_Pack.V_counter+1; END;

13 Chapter 19: Triggers13 Example (Continued) CREATE OR REPLACE TRIGGER class2_Trig AFTER UPDATE ON classes BEGIN INSERT INTO Temp (Num_col, Char_col) VALUES (Trig_Seq.NEXTVAL, ‘After Row 2 counter =‘ || Trig_Pack.V_counter); Trig_Pack.V_counter := Trig_Pack.V_counter+1; END;

14 Chapter 19: Triggers14 Example (Continued) CREATE OR REPLACE TRIGGER classBRow1_Trig BEFORE UPDATE ON classes FOR EACH ROW BEGIN INSERT INTO Temp (Num_col, Char_col) VALUES (Trig_Seq.NEXTVAL, ‘Before Row 1 counter =‘ || Trig_Pack.V_counter); Trig_Pack.V_counter := Trig_Pack.V_counter+1; END;

15 Chapter 19: Triggers15 Example (Continued) CREATE OR REPLACE TRIGGER classBRow2_Trig BEFORE UPDATE ON classes FOR EACH ROW BEGIN INSERT INTO Temp (Num_col, Char_col) VALUES (Trig_Seq.NEXTVAL, ‘Before Row 2 counter =‘ || Trig_Pack.V_counter); Trig_Pack.V_counter := Trig_Pack.V_counter+1; END;

16 Chapter 19: Triggers16 Example (Continued) CREATE OR REPLACE TRIGGER classBRow3_Trig BEFORE UPDATE ON classes FOR EACH ROW BEGIN INSERT INTO Temp (Num_col, Char_col) VALUES (Trig_Seq.NEXTVAL, ‘Before Row 3 counter =‘ || Trig_Pack.V_counter); Trig_Pack.V_counter := Trig_Pack.V_counter+1; END;

17 Chapter 19: Triggers17 Example (Continued) CREATE OR REPLACE TRIGGER classARow_Trig AFTER UPDATE ON classes FOR EACH ROW BEGIN INSERT INTO Temp (Num_col, Char_col) VALUES (Trig_Seq.NEXTVAL, ‘After Row counter =‘ || Trig_Pack.V_counter); Trig_Pack.V_counter := Trig_Pack.V_counter+1; END;

18 Chapter 19: Triggers18 Example (Continued) UPDATE classes SET Num_cr = 4 WHERE dept IN (‘COSC’, ‘MATH’);

19 Chapter 19: Triggers19 Example (Continued) SQL> SELECT * FROM Temp ORDER BY num_col; Num_colChar_col 1BeforeStatementCounter = 0 2BeforeRow 3Counter = 1 3BeforeRow 2Counter = 2 4BeforeRow 1Counter = 3 5AfterRowCounter = 4 6BeforeRow 3Counter = 5 7878 Before Row 2 Row 1 Counter = 6 Counter = 7

20 Chapter 19: Triggers20 Example (Continued) Num_colChar_col 9AfterRowCounter = 8 10BeforeRow 3Counter = 9 11BeforeRow 2Counter = 10 12BeforeRow 1Counter = 11 13AfterRowCounter = 12 14BeforeRow 3Counter = 13 15BeforeRow 2Counter = 14 16BeforeRow 1Counter = 15 17AfterRowCounter = 16 18AfterStatement 2Counter = 17 19AfterStatement 1Counter = 18

21 Chapter 19: Triggers21 NEW & OLD When a row level Trigger is fired, two data structures (like records) are produced NEW: contains new values OLD: stores original values Structure is the same as %ROWTYPE in records No OLD structure for INSERT operation No NEW structure for DELETE operation ROWID is the same for NEW & OLD

22 Chapter 19: Triggers22 NEW & OLD Field of OLD can not be changed OLD & NEW structure cannot be used as a function or procedure parameters (a field of OLD & NEW can be used) Use Prefix colon if you use this in a block :OLD :NEW Cannot perform the record_level operations

23 Chapter 19: Triggers23 NEW & OLD Kind of bind variable Known as pseudocode Trigger_Table%ROWTYPE Referenced by :NEW.field :OLD.field

24 Chapter 19: Triggers24 Row level Triggers Trigger Statement:OLD:NEW INSERTNULLInserted Values UPDATEOriginal values before update Values after update DELETEOriginal values before a row is deleted NULL

25 Chapter 19: Triggers25 Example of DMLTriggers CREATE or REPLACE TRIGGER check_faculty AFTER INSERT or UPDATE ON faculty FOR EACH ROW BEGIN Check_degree(:NEW.Type_Degree); Check_degree_year(:NEW.D_Year); END;

26 Chapter 19: Triggers26 Example of DMLTriggers CREATE or REPLACE TRIGGER student_major AFTER INSERT or UPDATE ON student FOR EACH ROW BEGIN INSERT INTO student_log VALUES(:NEW.Name, :OLD.Major, :NEW.Major, SYSDATE); COMMIT; END;

27 Chapter 19: Triggers27 Example of DMLTriggers CREATE OR REPLACE TRIGGER student_major AFTER UPDATE ON student FOR EACH ROW WHEN ((OLD.Major != NEW.Major) OR (OLD.Major IS NULL AND NEW.Major IS NULL)) BEGIN UPDATE student_log VALUES ( ….. WHERE ( …… COMMIT; END;

28 Chapter 19: Triggers28 Example of Using :OLD & :NEW CREATE OR REPLACE TRIGGER Generate_ID BEFORE INSERT OR UPDATE ON Students FOR EACH ROW BEGIN SELECT Student_sequence.NEXTVAL INTO :NEW.id FROM DUAL; END; ….. INSERT INTO Students (First, Last) VALUES (‘Shawn’, ‘Sham’); INSERT INTO Students (id, First, Last) VALUES (-2, ‘Shawn’, ‘Sham’);

29 Chapter 19: Triggers29 Pseudorecodes CREATE OR REPLACE TRIGGER Temp_Trig BEFORE DELETE ON Temp FOR EACH ROW DECLARE V_Temp Temp%ROWTYPE; BEGIN …. V_Temp := :OLD; -- not legal

30 Chapter 19: Triggers30 Referencing Clause REFERENCING [OLD AS oldname] [NEW AS newname] Example: CREATE OR REPLACE TRIGGER Grad_id BEFORE INSERT OR UPDATE ON Students REFERENCING NEW AS new_student FOR EACH ROW BEGIN SELECT Student_Seq.NEXTVAL INTO :new_student.ID FROM DUAL; END;

31 Chapter 19: Triggers31 WHEN Clause Syntax: WHEN Trigger_condition Example: CREATE OR REPLACE TRIGGER ch_credit BEFORE INSERT OR UPDATE OF current_cr ON Students FOR EACH ROW WHEN (NEW.current_cr > 50) BEGIN …. END;

32 Chapter 19: Triggers32 Trigger Predicate PredicateAction INSERTINGTrue, if inserted, False otherwise UPDATINGTrue, if updated, False otherwise DELETINGTrue, if deleted, False otherwise

33 Chapter 19: Triggers33 Example CREATE OR REPLACE TRIGGER New_grade BEFORE INSERT OR DELETE OR UPDATE ON Student_Reg FOR EACH Row DECLARE V_Change CHAR(1); BEGIN IF INSERTING THEN V_Change := ‘I’; ELSIF DELETING THEN V_Change := ‘D’; ELSE V_Change := ‘U’; END IF;

34 Chapter 19: Triggers34 2 – DDL Triggers Create or modify database object such as Index or table CREATE TABLE ALTER TABLE CREATE INDEX CREATE TRIGGER DROP TRIGGER

35 Chapter 19: Triggers35 DDL Trigger Syntax CREATE [OR REPLACE ] TRIGGER name {BEFORE | AFTER} {DDL event} ON {DATABASE | SCHEMA} DECLARE ….. BEGIN ….. END;

36 Chapter 19: Triggers36 Example 1 CREATE OR REPLACE TRIGGER example1 AFTER CREATE ON SCHEMA BEGIN DBMS_OUTPUT.PUT_LINE(‘A’ || ORA_DICT_OBJ_TYPE || ‘is created called’ || ORA_DICT_OBJ_NAME); END;

37 Chapter 19: Triggers37 Example 1 (continued) SET SERVEROUTPUT ON CREATE TABLE student (Name VARCHAR2(50), GPA NUMBER); CREATE INDEX stud_index ON student (Name); CREATE FUNCTION stud_func (IN a VARCHAR2) RETURN NUMBER AS DECLARE Temp NUMBER; BEGIN SELECT GPA INTO Temp FROM student WHERE Name = UPPER(a); RETURN Temp; END;

38 Chapter 19: Triggers38 Example 1 (continued) Output: A TABLE is created called STUDENT A INDEX is created called STUD_INDEX A FUNCTION is created called STUD_FUNC

39 Chapter 19: Triggers39 DDL Trigger Event and Attribute Functions ORA_SYSEVENT ORA_DES_ENCRYPTED_PASSWORD ORA_DICT_OBJ_TYPE ORA_DICT_OBJ_NAME ORA_DICT_OBJ_OWNER ORA_DATABASE_NAME ORA_LOGIN_USER ORA_IS_DROP_COLUMN(col_name) ORA_IS_ALTER_COLUMN(col_name)

40 Chapter 19: Triggers40 3- Database Event Trigger Syntax (System Triggers) CREATE [OR REPLACE] TRIGGER name {BEFORE|AFTER} {Database event} ON {TABLE| SCHEMA} DECLARE … BEGIN …. END;

41 Chapter 19: Triggers41 System Triggers EventTrigger STARTUPAFTER SHUTDOWNBEFORE SERVERERRORAFTER LOGONAFTER LOGOFFBEFORE CREATEBEFORE/AFTER DROPBEFORE/AFTER ALTERBEFORE/AFTER

42 Chapter 19: Triggers42 Common Sense Use of Triggers BEFORE STARTUP AFTER SHUTDOWN BEFORE LOGON AFTER LOGOFF BEFORE SERVEREERROR

43 Chapter 19: Triggers43 Example 1 CREATE OR REPLACE TRIGGER collectData BEFORE SHUTDOWN ON DATABASE BEGIN call_your_procedure; … END;

44 Chapter 19: Triggers44 Example 2 CREATE OR REPLACE TRIGGER cal_error AFTER SERVERERROR ON SCHEMA DECLARE s_ErrorNumNUMBER; s_dateDATE := SYSDATE; s_counterNUMBER :=1; BEGIN LOOP--use error stack s_ErrorNum := ORA_SERVER_ERROR(s_counter); EXIT WHEN s_ErrorNum = 0; DBMS_OUTPUT.PUT_LINE(USER || TO_CHAR(s_ErrorNum) || TO_CHAR(s_counter) || TO_CHAR(s_Date)); s_counter := s_counter + 1; END LOOP; END;

45 Chapter 19: Triggers45 Example … FOR errorNum IN 20000.. 20050 LOOP IF ORA_IS_SERVERERROR(errorNum) THEN ….. END IF; END LOOP;

46 Chapter 19: Triggers46 Alter & Delete Triggers DROP TRIGGER name; ALTER TRIGGER name {DISABLE | ENABLE}; Example: SQL> ALTER TRIGGER grad_student DISABLE; SQL> ALTER TABLE Students DISABLE ALL TRIGGERS;

47 Chapter 19: Triggers47 Data Dictionary & Triggers USER_TRIGGERS TRIGGER_TYPE TRIGGER_EVENT TRIGGER_NAME

48 Chapter 19: Triggers48 4-INSTEAD OF TRIGGERS Control INSERT, DELETE, UPDATE operation on views Defined on view Fired at row level

49 Chapter 19: Triggers49 INSTEAD OF TRIGGERS Syntax: CREATE [OR REPLACE] TRIGGER name INSTEAD OF operation ON View_Name FOR EACH ROW BEGIN ….. END;

50 Chapter 19: Triggers50 Example CREATE OR REPLACE VIEW class_room AS SELECT dept, course, building, room_no FROM Room, Classes WHERE Room.id = Classes.id; CREATE TRIGGER class_room_Insert INSTEAD OF INSERT ON class_room DECLARE V_room_id Room.id%TYPE; BEGIN SELECTid INTO V_room_id FROM Room WHERE building = :New.building AND Room_no = :New.room_no; UPDATE Classes SET id = V_room.id WHERE dept =:New.dept AND Course =:New.course; END;

51 Chapter 19: Triggers51 Data Dictionary Views: DBA_TRIGGERS ALL_TRIGGERS USER_TRIGGERS

52 Chapter 19: Triggers52 TRIGGER_NAME TRIGGER_TYPE TRIGGER_EVENT TABLE_OWNER BASE_OBJECT_TYPE TABLE_NAME WHEN_CLAUSE STATUS TRIGGER_BODY

53 Chapter 19: Triggers53 Example SELECT Object_Name, Object_Type, Status FROMUSER_OBJECTS WHEREObject_Name = INVALID_TRIGGER’;

54 Chapter 19: Triggers54 Multiple Triggers of the Same Type CREATE or REPLACE TRIGGER status_trigger BEFORE INSERT ON student_status FOR EACH ROW BEGIN IF :NEW.Total_Cr < 30 THEN :NEW.Status := ‘Freshman’; ELSIF :NEW.Total_Cr > 30 AND :NEW.Total_Cr < 60 THEN :NEW.Status := ‘Sophmore’; ELSIF :NEW.Total_Cr > 60 AND :NEW.Total_Cr < 90 THEN :NEW.Status := ‘Junior’; ELSE :NEW.Status := ‘Senior’; END;


Download ppt "Chapter 19: Triggers1 Chapter Nineteen Triggers Objective: – Understanding Triggers – Types of Triggers – Applications of Triggers – How to work with Triggers."

Similar presentations


Ads by Google