Oracle PL/SQL/Trigger/Trigger Firing

Материал из SQL эксперт
Перейти к: навигация, поиск

Avoid the mutating table error.

 
SQL>
SQL> CREATE TABLE lecturer (
  2    id               NUMBER(5) PRIMARY KEY,
  3    first_name       VARCHAR2(20),
  4    last_name        VARCHAR2(20),
  5    major            VARCHAR2(30),
  6    current_credits  NUMBER(3)
  7    );
Table created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10001, "Scott", "Lawson","Computer Science", 11);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major, current_credits)
  2                VALUES (10002, "Mar", "Wells","History", 4);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10003, "Jone", "Bliss","Computer Science", 8);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10004, "Man", "Kyte","Economics", 8);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10005, "Pat", "Poll","History", 4);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10006, "Tim", "Viper","History", 4);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10007, "Barbara", "Blues","Economics", 7);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10008, "David", "Large","Music", 4);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10009, "Chris", "Elegant","Nutrition", 8);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10010, "Rose", "Bond","Music", 7);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10011, "Rita", "Johnson","Nutrition", 8);
1 row created.
SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10012, "Sharon", "Clear","Computer Science", 3);
1 row created.
SQL>
SQL>
SQL> CREATE OR REPLACE PACKAGE StudentData AS
  2    TYPE t_Majors IS TABLE OF lecturer.major%TYPE
  3      INDEX BY BINARY_INTEGER;
  4    TYPE t_IDs IS TABLE OF lecturer.ID%TYPE
  5      INDEX BY BINARY_INTEGER;
  6
  7    myLecturerMajors t_Majors;
  8    myLecturerIDs    t_IDs;
  9    v_NumEntries    BINARY_INTEGER := 0;
 10  END StudentData;
 11  /
Package created.
SQL>
SQL> CREATE OR REPLACE TRIGGER RLimitMajors
  2    BEFORE INSERT OR UPDATE OF major ON lecturer
  3    FOR EACH ROW
  4  BEGIN
  5    StudentData.v_NumEntries := StudentData.v_NumEntries + 1;
  6    StudentData.myLecturerMajors(StudentData.v_NumEntries) :=
  7      :new.major;
  8    StudentData.myLecturerIDs(StudentData.v_NumEntries) := :new.id;
  9  END RLimitMajors;
 10  /
Trigger created.
SQL>
SQL> CREATE OR REPLACE TRIGGER SLimitMajors
  2    AFTER INSERT OR UPDATE OF major ON lecturer
  3  DECLARE
  4    studentMax     CONSTANT NUMBER := 5;
  5    studentCount      NUMBER;
  6    myLecturerID       lecturer.ID%TYPE;
  7    v_Major           lecturer.major%TYPE;
  8  BEGIN
  9    FOR v_LoopIndex IN 1..StudentData.v_NumEntries LOOP
 10      myLecturerID := StudentData.myLecturerIDs(v_LoopIndex);
 11      v_Major := StudentData.myLecturerMajors(v_LoopIndex);
 12
 13      SELECT COUNT(*)
 14        INTO studentCount
 15        FROM lecturer
 16        WHERE major = v_Major;
 17
 18      IF studentCount > studentMax THEN
 19        RAISE_APPLICATION_ERROR(-20000,
 20          "Too many lecturer for major " || v_Major ||
 21          " because of student " || myLecturerID);
 22      END IF;
 23    END LOOP;
 24
 25    StudentData.v_NumEntries := 0;
 26  END SLimitMajors;
 27  /
Trigger created.
SQL>
SQL> UPDATE lecturer
  2    SET major = "History"
  3    WHERE ID = 10003;
1 row updated.
SQL>
SQL> UPDATE lecturer
  2    SET major = "History"
  3    WHERE ID = 10002;
1 row updated.
SQL>
SQL> UPDATE lecturer
  2    SET major = "History"
  3    WHERE ID = 10009;
1 row updated.
SQL>
SQL>
SQL> drop table lecturer;
Table dropped.
SQL>



Order of trigger firing.

 
SQL>
SQL> CREATE TABLE session (
  2    department       CHAR(3),
  3    course           NUMBER(3),
  4    description      VARCHAR2(2000),
  5    max_lecturer     NUMBER(3),
  6    current_lecturer NUMBER(3),
  7    num_credits      NUMBER(1),
  8    room_id          NUMBER(5)
  9    );
Table created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("HIS", 101, "History 101", 30, 11, 4, 20000);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("HIS", 301, "History 301", 30, 0, 4, 20004);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("CS", 101, "Computer Science 101", 50, 0, 4, 20001);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("ECN", 203, "Economics 203", 15, 0, 3, 20002);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("CS", 102, "Computer Science 102", 35, 3, 4, 20003);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("MUS", 410, "Music 410", 5, 4, 3, 20005);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("ECN", 101, "Economics 101", 50, 0, 4, 20007);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("NUT", 307, "Nutrition 307", 20, 2, 4, 20008);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("MUS", 100, "Music 100", 100, 0, 3, NULL);
1 row created.
SQL>
SQL>
SQL>
SQL> CREATE TABLE MyTable (
  2    num_col    NUMBER,
  3    char_col   VARCHAR2(60)
  4    );
Table created.
SQL>
SQL>
SQL> CREATE SEQUENCE trig_seq
  2    START WITH 1
  3    INCREMENT BY 1;
Sequence created.
SQL>
SQL> CREATE OR REPLACE PACKAGE TrigPackage AS
  2    v_Counter NUMBER;
  3  END TrigPackage;
  4  /
Package created.
SQL>
SQL> CREATE OR REPLACE TRIGGER sessionBeforeUpdate
  2    BEFORE UPDATE ON session
  3  BEGIN
  4    TrigPackage.v_Counter := 0;
  5
  6    INSERT INTO MyTable (num_col, char_col)
  7      VALUES (trig_seq.NEXTVAL,
  8        "Before Statement: counter = " || TrigPackage.v_Counter);
  9
 10    TrigPackage.v_Counter := TrigPackage.v_Counter + 1;
 11  END sessionBeforeUpdate;
 12  /
Trigger created.
SQL>
SQL> CREATE OR REPLACE TRIGGER sessionAfterStatement1
  2    AFTER UPDATE ON session
  3  BEGIN
  4    INSERT INTO MyTable (num_col, char_col)
  5      VALUES (trig_seq.NEXTVAL,
  6        "After Statement 1: counter = " || TrigPackage.v_Counter);
  7
  8    TrigPackage.v_Counter := TrigPackage.v_Counter + 1;
  9  END sessionAfterStatement1;
 10  /
Trigger created.
SQL>
SQL> CREATE OR REPLACE TRIGGER sessionAfterStatement2
  2    AFTER UPDATE ON session
  3  BEGIN
  4    INSERT INTO MyTable (num_col, char_col)
  5      VALUES (trig_seq.NEXTVAL,"After Statement 2: counter = " || TrigPackage.v_Counter);
  6
  7    TrigPackage.v_Counter := TrigPackage.v_Counter + 1;
  8  END sessionAfterStatement2;
  9  /
Trigger created.
SQL>
SQL> CREATE OR REPLACE TRIGGER sessionBeforeRow1
  2    BEFORE UPDATE ON session
  3    FOR EACH ROW
  4  BEGIN
  5    INSERT INTO MyTable (num_col, char_col)
  6      VALUES (trig_seq.NEXTVAL,
  7        "Before Row 1: counter = " || TrigPackage.v_Counter);
  8
  9    -- Increment for the next trigger.
 10    TrigPackage.v_Counter := TrigPackage.v_Counter + 1;
 11  END sessionBeforeRow1;
 12  /
Trigger created.
SQL>
SQL> CREATE OR REPLACE TRIGGER sessionBeforeRow2
  2    BEFORE UPDATE ON session
  3    FOR EACH ROW
  4  BEGIN
  5    INSERT INTO MyTable (num_col, char_col)
  6      VALUES (trig_seq.NEXTVAL,
  7        "Before Row 2: counter = " || TrigPackage.v_Counter);
  8
  9    TrigPackage.v_Counter := TrigPackage.v_Counter + 1;
 10  END sessionBeforeRow2;
 11  /
Trigger created.
SQL>
SQL> CREATE OR REPLACE TRIGGER sessionBeforeRow3
  2    BEFORE UPDATE ON session
  3    FOR EACH ROW
  4  BEGIN
  5    INSERT INTO MyTable (num_col, char_col)
  6      VALUES (trig_seq.NEXTVAL,
  7        "Before Row 3: counter = " || TrigPackage.v_Counter);
  8
  9    TrigPackage.v_Counter := TrigPackage.v_Counter + 1;
 10  END sessionBeforeRow3;
 11  /
Trigger created.
SQL>
SQL> CREATE OR REPLACE TRIGGER sessionAfterRow
  2    AFTER UPDATE ON session
  3    FOR EACH ROW
  4  BEGIN
  5    INSERT INTO MyTable (num_col, char_col)
  6      VALUES (trig_seq.NEXTVAL,
  7        "After Row: counter = " || TrigPackage.v_Counter);
  8
  9    TrigPackage.v_Counter := TrigPackage.v_Counter + 1;
 10  END sessionAfterRow;
 11  /
Trigger created.
SQL>
SQL> DELETE FROM MyTable;
0 rows deleted.
SQL>
SQL> UPDATE session
  2    SET num_credits = 4
  3    WHERE department IN ("HIS", "CS");
4 rows updated.
SQL>
SQL> SELECT *
  2    FROM MyTable
  3    ORDER BY num_col;
   NUM_COL CHAR_COL
---------- ------------------------------------------------------------
         1 Before Statement: counter = 0
         2 Before Row 3: counter = 1
         3 Before Row 2: counter = 2
         4 Before Row 1: counter = 3
         5 After Row: counter = 4
         6 Before Row 3: counter = 5
         7 Before Row 2: counter = 6
         8 Before Row 1: counter = 7
         9 After Row: counter = 8
        10 Before Row 3: counter = 9
        11 Before Row 2: counter = 10
   NUM_COL CHAR_COL
---------- ------------------------------------------------------------
        12 Before Row 1: counter = 11
        13 After Row: counter = 12
        14 Before Row 3: counter = 13
        15 Before Row 2: counter = 14
        16 Before Row 1: counter = 15
        17 After Row: counter = 16
        18 After Statement 2: counter = 17
        19 After Statement 1: counter = 18
19 rows selected.
SQL>
SQL>
SQL> DROP SEQUENCE trig_seq;
Sequence dropped.
SQL>
SQL> drop table mytable;
Table dropped.
SQL>
SQL> drop table session;
Table dropped.
SQL>
SQL>