Copyright © 2006 – Quality & Reliability SA PL/SQL TRIGGERS ΜΕΡΟΣ 1
Copyright © 2006 – Quality & Reliability SA ΧΡΗΣΕΙΣ ΔΙΑΤΗΡΗΣΗ ΑΚΕΡΑΙΟΤΗΤΑΣ ΠΑΡΑΚΟΛΟΥΘΗΣΗ ΑΛΛΑΓΩΝ ΕΝΗΜΕΡΩΣΗ ΑΛΛΩΝ ΠΡΟΓΡΑΜΜΑΤΩΝ ΔΗΜΟΣΙΕΥΣΗ ΠΛΗΡΟΦΟΡΙΩΝ
Copyright © 2006 – Quality & Reliability SA ΤΥΠΟΙ DML TRIGGER –Πυροδοτείται πριν ή μετά από μια εντολή DML (χειρισμού δεδομένων). INSTEAD OF TRIGGER –Υποκαθιστά μια εντολή DML SYSTEM TRIGGER –Πυροδοτείται αν επέλθει κάποιο συμβάν στο σύστημα.
Copyright © 2006 – Quality & Reliability SA ΣΥΝΤΑΞΗ CREATE OR REPLACE TRIGGER [Name] {BEFORE|AFTER|INSTEAD OF} {INSERT|UPDATE|DELETE} [συμβάν ενεργοποίησης] [WHEN συνθήκη] [FOR EACH ROW] DECLARE -- local variables here begin end [Name];
Copyright © 2006 – Quality & Reliability SA ΕΚΤΕΛΕΣΗ ΤΩΝ DML TRIGGERS ΠΡΩΤΑ ΟΙ BEFORE TRIGGERS ΕΠΙΠΕΔΟΥ ΕΝΤΟΛΗΣ META ΟΙ BEFORE TRIGGERS ΕΠΙΠΕΔΟΥ ΓΡΑΜΜΗΣ ΠΡΟΒΛΕΠΟΜΕΝΗ ΕΝΤΟΛΗ DML AFTER ΓΡΑΜΜΗΣ AFTER ΕΝΤΟΛΗΣ
Copyright © 2006 – Quality & Reliability SA :old ΚΑΙ :new CREATE TRIGGER PersonCheckAge AFTER INSERT OR UPDATE OF age ON Person FOR EACH ROW BEGIN IF (:new.age < 0) THEN RAISE_APPLICATION_ERROR( , 'no negative age allowed'); END IF; END;
Copyright © 2006 – Quality & Reliability SA Drop/alter
Copyright © 2006 – Quality & Reliability SA παράδειγμα Κλασικός Trigger για την χορήγηση ΡΚ
Copyright © 2006 – Quality & Reliability SA Audit Trail BEGIN /* Use 'I' for an INSERT, 'D' for DELETE, and 'U' for UPDATE. */ IF INSERTING THEN v_ChangeType := 'I'; ELSIF UPDATING THEN v_ChangeType := 'U'; ELSE v_ChangeType := 'D'; END IF; CREATE OR REPLACE TRIGGER LogRSChanges BEFORE INSERT OR DELETE OR UPDATE ON registered_students FOR EACH ROW DECLARE v_ChangeType CHAR(1); BEGIN /* Use 'I' for an INSERT, 'D' for DELETE, and 'U' for UPDATE. */ IF INSERTING THEN v_ChangeType := 'I'; ELSIF UPDATING THEN v_ChangeType := 'U'; ELSE v_ChangeType := 'D'; END IF; /* Record all the changes made to registered_students in RS_audit. Use SYSDATE to generate the timestamp, and USER to return the userid of the current user. */ INSERT INTO RS_audit (change_type, changed_by, timestamp, old_student_id, old_department, old_course, old_grade, new_student_id, new_department, new_course, new_grade) VALUES (v_ChangeType, USER, SYSDATE, :old.student_id, :old.department, :old.course, :old.grade, :new.student_id, :new.department, :new.course, :new.grade); END LogRSChanges;
Copyright © 2006 – Quality & Reliability SA INSERT INTO RS_audit (change_type, changed_by, timestamp, old_student_id, old_department, old_course, old_grade, new_student_id, new_department, new_course, new_grade) VALUES (v_ChangeType, USER, SYSDATE, :old.student_id, :old.department, :old.course, :old.grade, :new.student_id, :new.department, :new.course, :new.grade); END LogRSChanges; CREATE OR REPLACE TRIGGER LogRSChanges BEFORE INSERT OR DELETE OR UPDATE ON registered_students FOR EACH ROW DECLARE v_ChangeType CHAR(1); BEGIN /* Use 'I' for an INSERT, 'D' for DELETE, and 'U' for UPDATE. */ IF INSERTING THEN v_ChangeType := 'I'; ELSIF UPDATING THEN v_ChangeType := 'U'; ELSE v_ChangeType := 'D'; END IF; /* Record all the changes made to registered_students in RS_audit. Use SYSDATE to generate the timestamp, and USER to return the userid of the current user. */ INSERT INTO RS_audit (change_type, changed_by, timestamp, old_student_id, old_department, old_course, old_grade, new_student_id, new_department, new_course, new_grade) VALUES (v_ChangeType, USER, SYSDATE, :old.student_id, :old.department, :old.course, :old.grade, :new.student_id, :new.department, :new.course, :new.grade); END LogRSChanges;
Copyright © 2006 – Quality & Reliability SA