Presentation is loading. Please wait.

Presentation is loading. Please wait.

Please use speaker notes for additional information!

Similar presentations


Presentation on theme: "Please use speaker notes for additional information!"— Presentation transcript:

1 Please use speaker notes for additional information!
More on Triggers Please use speaker notes for additional information! This slide show gives a little more information on triggers.

2 ------------------------------- -------- ---- IDNO NUMBER(4)
More on triggers SQL> DESC colstu; Name Null? Type IDNO NUMBER(4) NAME VARCHAR2(20) NUMCR NUMBER(3) CLASSLEV VARCHAR2(8) SQL> desc credit_range; YEAR_NAME NOT NULL VARCHAR2(8) MINCREDITS NUMBER(3) MAXCREDITS NUMBER(3) SQL> SELECT * FROM credit_range; YEAR_NAM MINCREDITS MAXCREDITS FRESHMAN SOPHMORE JUNIOR SENIOR The two tables being used are colstu and credit_range. The block of code we will be looking at is designed to insert records into colstu. However, the records are checked against credit_range to make sure that the credits are in the appropriate range for the student class level.

3 SQL> edit use_stu_trig
More on triggers SQL> edit use_stu_trig DECLARE v_idno colstu.idno%TYPE := &in_idno; v_name colstu.name%TYPE := '&in_name'; v_numcr colstu.numcr%TYPE := &in_numcr; v_classlev colstu.classlev%TYPE := '&in_classlev'; BEGIN INSERT INTO colstu VALUES (v_idno, v_name, v_numcr, v_classlev); END; / Note that there is no way of telling that there is a trigger associated with the insert into colstu when looking at this anonymous block. This code simply takes in user input and inserts the user input as a record in the table colstu.

4 CREATE OR REPLACE TRIGGER student_log BEFORE INSERT ON colstu
More on triggers SQL> edit stu_trig2 SET SERVEROUTPUT ON CREATE OR REPLACE TRIGGER student_log BEFORE INSERT ON colstu FOR EACH ROW WHEN (new.classlev != 'SENIOR') DECLARE v_mincredits NUMBER; v_maxcredits NUMBER; BEGIN SELECT mincredits, maxcredits into v_mincredits, v_maxcredits FROM credit_range WHERE year_name = :new.classlev; IF :new.numcr < v_mincredits or :new.numcr > v_maxcredits THEN RAISE_APPLICATION_ERROR (-20001, 'CREDITS OUT OF RANGE FOR ' || :new.idno || ' ' || :new.name); END IF; END; / SET SERVEROUTPUT OFF Note that this is done before INSERT as opposed to after and that it is done for each row. In addition it is done only where the classlev that was entered is not equal to SENIOR. This is a trigger that has been written to use before insert into the colstu table. It applies to all blocks of code that attempt to insert into colstu. Please read the notes for definitions of clauses like BEFORE INSERT and FOR EACH NEW ROW. Also please read about :old and :new. stu_trig2 Trigger created. The trigger must be created prior to use.

5 SQL> @ use_stu_trig Enter value for in_idno: 1234
More on triggers use_stu_trig Enter value for in_idno: 1234 old 2: v_idno colstu.idno%TYPE := &in_idno; new 2: v_idno colstu.idno%TYPE := 1234; Enter value for in_name: John Doe old 3: v_name colstu.name%TYPE := '&in_name'; new 3: v_name colstu.name%TYPE := 'John Doe'; Enter value for in_numcr: 15 old 4: v_numcr colstu.numcr%TYPE := &in_numcr; new 4: v_numcr colstu.numcr%TYPE := 15; Enter value for in_classlev: FRESHMAN old 5: v_classlev colstu.classlev%TYPE := '&in_classlev'; new 5: v_classlev colstu.classlev%TYPE := 'FRESHMAN'; PL/SQL procedure successfully completed. SQL> SELECT * FROM colstu; IDNO NAME NUMCR CLASSLEV 1234 John Doe FRESHMAN The user enters input. When the INSERT is encountered the trigger is executed to determine if the range is correct. For this record the range is appropriate so no error message is raised. 15 is in the range for Freshman which is 0 to 30.

6 SQL> @ use_stu_trig Enter value for in_idno: 2345
More on triggers use_stu_trig Enter value for in_idno: 2345 old 2: v_idno colstu.idno%TYPE := &in_idno; new 2: v_idno colstu.idno%TYPE := 2345; Enter value for in_name: Jane Doe old 3: v_name colstu.name%TYPE := '&in_name'; new 3: v_name colstu.name%TYPE := 'Jane Doe'; Enter value for in_numcr: 15 old 4: v_numcr colstu.numcr%TYPE := &in_numcr; new 4: v_numcr colstu.numcr%TYPE := 15; Enter value for in_classlev: JUNIOR old 5: v_classlev colstu.classlev%TYPE := '&in_classlev'; new 5: v_classlev colstu.classlev%TYPE := 'JUNIOR'; DECLARE * ERROR at line 1: ORA-20001: CREDITS OUT OF RANGE FOR 2345 Jane Doe ORA-06512: at "SCOTT.STUDENT_LOG", line 9 ORA-04088: error during execution of trigger 'SCOTT.STUDENT_LOG' ORA-06512: at line 7 SQL> SELECT * FROM colstu; IDNO NAME NUMCR CLASSLEV 1234 John Doe FRESHMAN 15 credits is not appropriate for a JUNIOR so the error was raised by the trigger. This attempt raised the error and the record was not inserted in the table. The BEFORE clause means the checking is done prior to the entry. RAISE_APPLICATION_ERROR is raising error with the message that the credits are out of range. The identification number and name are also shown. Notice that they are shown with the :new which is tied to the insert and the : is required. The record was not added to the table.

7 SQL> @ use_stu_trig Enter value for in_idno: 3456
More on triggers use_stu_trig Enter value for in_idno: 3456 old 2: v_idno colstu.idno%TYPE := &in_idno; new 2: v_idno colstu.idno%TYPE := 3456; Enter value for in_name: Ann Smith old 3: v_name colstu.name%TYPE := '&in_name'; new 3: v_name colstu.name%TYPE := 'Ann Smith'; Enter value for in_numcr: 60 old 4: v_numcr colstu.numcr%TYPE := &in_numcr; new 4: v_numcr colstu.numcr%TYPE := 60; Enter value for in_classlev: SENIOR old 5: v_classlev colstu.classlev%TYPE := '&in_classlev'; new 5: v_classlev colstu.classlev%TYPE := 'SENIOR'; PL/SQL procedure successfully completed. SQL> SELECT * FROM colstu; IDNO NAME NUMCR CLASSLEV 3456 Ann Smith SENIOR 1234 John Doe FRESHMAN Note that senior is eliminated from the test in the insert so even although the number of credits is not in the range of 91 to 999, no error was raised. Record added to table. Note range test not done on SENIOR.


Download ppt "Please use speaker notes for additional information!"

Similar presentations


Ads by Google