Please use speaker notes for additional information!

Slides:



Advertisements
Similar presentations
PL/SQL User Defined Types Record and Table Please use speaker notes for additional information!
Advertisements

PL/SQL - Using IF statements Please use speaker notes for additional information!
Triggers. Triggers: Motivation Assertions are powerful, but the DBMS often can’t tell when they need to be checked. Attribute- and tuple-based checks.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
Triggers.
Table maintenance revisited (again) Please use speaker notes for additional information!
Cursors in PL/SQL Includes cursor example and continuation of first cursor example Please use speaker notes for additional information!
Bordoloi and Bock EXCEPTIONS. Bordoloi and Bock Errors Two types of errors can be found in a program: compilation errors and runtime errors. There is.
Exceptions Oracle Database PL/SQL 10g Programming Chapter 7.
DAY 20: ACCESS CHAPTER 5 Tazin Afrin October 29,
 Allows sophisticated data processing  Build complex business logic in a modular fashion  Use over and over  Execute rapidly – little network traffic.
 Allows sophisticated data processing  Build complex business logic in a modular fashion  Use over and over  Execute rapidly – little network traffic.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
ABC Insurance Co. Paul Barry Steve Randolph Jing Zhou CSC8490 Database Systems & File Management Dr. Goelman Villanova University August 2, 2004.
Relationships and Advanced Query Concepts Using Multiple Tables Please use speaker notes for additional information!
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
In Oracle.  A PL/SQL block stored in the database and fired in response to a specified event ◦ DML statements : insert, update, delete ◦ DDL statements.
Edit Programs Please use speaker notes for additional information. Example: payedit.cbl payedit.cbl.
Examples dealing with cursors and tables Please use speaker notes for additional information!
CSIT 313 DB PROGRAMMING EXCEPTION HANDLING. In PL/SQL, an error condition is called an exception. An exception can be either –internally defined (by the.
Manipulating data within PL/SQL Please use speaker notes for additional information!
PL/SQL Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:
Trapping Oracle Server Exceptions. 2 home back first prev next last What Will I Learn? Describe and provide an example of an error defined by the Oracle.
Exceptions in PL/SQL Please use speaker notes for additional information!
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
Indexes in Oracle An Introduction Please check speaker notes for additional information!
CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous.
Chapter 18: Exception Handling1 Chapter Eighteen Exception Handling Objective: – Define exceptions – List types of exception handlers – Trap errors – Exception.
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Oracle PL/SQL Loops Please use speaker notes for additional information!
Introduction to PL/SQL As usual, use speaker notes for additional information!
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Chapter 19: Triggers1 Chapter Nineteen Triggers Objective: – Understanding Triggers – Types of Triggers – Applications of Triggers – How to work with Triggers.
PHP with MYSQL Please use speaker notes for additional information!
More about maintaining a table Use speaker notes for additional information!
Sequential Processing to Update a File Please use speaker notes for additional information!
1 11g NEW FEATURES ByVIJAY. 2 AGENDA  RESULT CACHE  INVISIBLE INDEXES  READ ONLY TABLES  DDL WAIT OPTION  ADDING COLUMN TO A TABLE WITH DEFAULT VALUE.
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
CHAPTER 5 EXCEPTION HANDLING
Creating Stored Functions
Chapter 2 Anonymous Block
A Guide to SQL, Seventh Edition
SQL and SQL*Plus Interaction
Handling Exceptions.
Introduction to Triggers
More on Procedures (Internal/Local procedures)
Handling Exceptions.
Introduction to Procedures
MySQL - Creating donorof database offline
Please see speaker notes for additional information!
Introduction to Functions
Handling Exceptions.
PRACTICE OVERVIEW PL/SQL Part - 2.
Access and Condition Statements
PL/SQL Programing : Triggers
Please use speaker notes for additional information!
Handling Exceptions.
TOTAMT NUMBER(6,2) the log will be written. trigger1.
Introduction to Views and Reports
Database Management Systems 2
Handling Exceptions.
Please use speaker notes for additional information!
Please use speaker notes for additional information!
Done with SQL..
PRACTICE OVERVIEW PL/SQL Part - 1.
Using screens and adding two numbers - addda.cbl
More and Still More on Procedures and Functions
Oracle Stored Procedures and Functions
Triggers.
Presentation transcript:

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.

------------------------------- -------- ---- 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 0 30 SOPHMORE 31 60 JUNIOR 61 90 SENIOR 91 999 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.

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.

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. SQL> @ stu_trig2 Trigger created. The trigger must be created prior to use.

SQL> @ use_stu_trig Enter value for in_idno: 1234 More on triggers SQL> @ 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 15 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.

SQL> @ use_stu_trig Enter value for in_idno: 2345 More on triggers SQL> @ 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 15 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 20001 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.

SQL> @ use_stu_trig Enter value for in_idno: 3456 More on triggers SQL> @ 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 60 SENIOR 1234 John Doe 15 FRESHMAN Note that senior is eliminated from the test in the insert so even although the number of credits - 60 - is not in the range of 91 to 999, no error was raised. Record added to table. Note range test not done on SENIOR.