Introduction to Triggers

Slides:



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

SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
Relational example using donor, donation and drive tables For additional information see the speaker notes!
PL/SQL - Using IF statements Please use speaker notes for additional information!
More on IF statements Use speaker notes for additional information!
Advanced Package Concepts. 2 home back first prev next last What Will I Learn? Write packages that use the overloading feature Write packages that use.
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.
Relational example using donor, contrib and drive tables Please use speaker notes for additional information!
Table maintenance revisited (again) Please use speaker notes for additional information!
PL/SQL Bulk Collections in Oracle 9i and 10g Kent Crotty Burleson Consulting October 13, 2006.
Dr. James Dullea, CSC8490 Introduction to PL/SQLSlide 1 of 36 7From Prof. Dullea CSC8490 Introduction to PL/SQL Module 01-9 Revised: June 12, 2005 Dr.
PL/SQL and the Table API. Benefits of Server-Side Code Speedy Pizza MENU NAPOLITAINE PIZZA Reduced network traffic Maintainability Data integrity Triggers.
Cursors in PL/SQL Includes cursor example and continuation of first cursor example Please use speaker notes for additional information!
InvEasy (Project1) Please use speaker notes for additional information!
Relationships and Advanced Query Concepts Using Multiple Tables Please use speaker notes for additional information!
University of Sunderland COM 220 Lecture Six Slide 1 Building Interactive Forms Applications using Oracle.
Array - adding to array at run time Please see speaker notes for additional information!
Triggers A Quick Reference and Summary BIT 275. Triggers SQL code permits you to access only one table for an INSERT, UPDATE, or DELETE statement. The.
1. 1. Which type of argument passes a value from a procedure to the calling program? A. VARCHAR2 B. BOOLEAN C. OUT D. IN 2.
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.
Manipulating data within PL/SQL Please use speaker notes for additional information!
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Exceptions in PL/SQL Please use speaker notes for additional information!
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Advanced SQL: Triggers & Assertions
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
Indexes in Oracle An Introduction Please check speaker notes for additional information!
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Introduction to Oracle - SQL Additional information is available in speaker notes!
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Oracle PL/SQL Loops Please use speaker notes for additional information!
A procedure is a module performing one or more actions; it does not need to return any values. The syntax for creating a procedure is as follows: CREATE.
A database trigger is a stored PL/SQL program unit associated with a specific database table. ORACLE executes (fires) a database trigger automatically.
Introduction to PL/SQL As usual, use speaker notes for additional information!
Sequential Processing to Update a File Please use speaker notes for additional information!
SQL Triggers, Functions & Stored Procedures Programming Operations.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Chapter 2 Anonymous Block
A Guide to SQL, Seventh Edition
Introduction to PL/SQL Programing
Instructor: Jason Carter
PROCEDURES, CONDITIONAL LOGIC, EXCEPTION HANDLING, TRIGGERS
More on Procedures (Internal/Local procedures)
Introduction to Oracle9i: SQL
Error Handling Summary of the next few pages: Error Handling Cursors.
Introduction to Procedures
MySQL - Creating donorof database offline
Introduction to Functions
الأزنده أ.موضي المحرج.
Please use speaker notes for additional information!
PL/SQL Programing : Triggers
Please use speaker notes for additional information!
TOTAMT NUMBER(6,2) the log will be written. trigger1.
Introduction to Views and Reports
Manipulating Data.
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Environment/Report Tutorial
Please use speaker notes for additional information!
Please use speaker notes for additional information!
Using screens and adding two numbers - addda.cbl
More and Still More on Procedures and Functions
Triggers.
Prof. Arfaoui. COM390 Chapter 9
TRIGGERS.
Presentation transcript:

Introduction to Triggers Please use speaker notes for additional information! Triggers are named PL/SQL blocks. In that way, they are similar to procedures, however procedures are involved with an explicit call and a passing of arguments while a trigger event causes the implicit execution of the trigger code and arguments are not involved. A trigger event is associated with DML such as INSERT, UPDATE or DELETE and when it is executed it is referred to as the "firing" of the trigger.

Data in total_notes prior to execution. triggers SQL> select * from total_notes; CHANGEDAT TOTAMT --------- --------- 27-JUL-99 160 27-JUL-99 195 SQL> DESC total_notes Name Null? Type ------------------------------- -------- ---- CHANGEDATE DATE TOTAMT NUMBER(6,2) Data in total_notes prior to execution. SQL> edit trigger1 CREATE OR REPLACE TRIGGER trigger1 AFTER UPDATE ON new_donation_one DECLARE v_date total_notes.changedate%TYPE :='27-JUL-99'; v_totamt total_notes.totamt%TYPE; BEGIN SELECT SUM(contamt) INTO v_totamt FROM new_donation_one; INSERT INTO total_notes (changedate, totamt) VALUES(v_date, v_totamt); END trigger1; / This trigger, named trigger1, will be implicitly activated based on an update done to the table called new_donation_one. After each update, I am collecting the sum of the contributions and putting it into total_notes. Following the keep in simple approach as opposed to the professional approach, I hardcoded the date into the code. In fact it would be better to have the system date or something similar in the date. Note that after coding the trigger, it has to be created before use. I did that with @ trigger1. You need to be clear on the fact that a trigger is tired to an event. If any code that I write encounters an update for new_donation_one, this trigger will be fired. That can be tremendously beneficial in creating an event trail. Like other named blocks that are executed, the trigger must be created prior to use. Remember to that if you modify the trigger, you must recreate it. SQL> @ trigger1 Trigger created.

SQL> edit use_trigger1 triggers SQL> select * from new_donation_one; IDNO DRI CONTDATE CONTAMT ----- --- --------- --------- 11111 100 07-JAN-99 35 33333 300 10-MAR-99 30 22222 100 14-MAR-99 10 23456 300 14-JUN-99 45 12121 300 10-JUN-99 75 SQL> edit use_trigger1 SET VERIFY OFF DECLARE v_idno new_donation_one.idno%TYPE :='&input_idno'; v_driveno new_donation_one.driveno%TYPE :='&input_driveno'; v_contamt new_donation_one.contamt%TYPE :=&input_contamt; BEGIN UPDATE new_donation_one SET contamt = v_contamt WHERE idno = v_idno AND driveno = v_driveno; END; / SET VERIFY ON This is the code that does an update. The trigger shown on the previous slide is tied to updates on new_donation_one so when this update is encountered, the trigger is fired.

Enter value for input_idno: 44444 Enter value for input_driveno: 200 triggers SQL> select * from new_donation_one; IDNO DRI CONTDATE CONTAMT ----- --- --------- --------- 11111 100 07-JAN-99 35 33333 300 10-MAR-99 30 22222 100 14-MAR-99 10 23456 300 14-JUN-99 45 12121 300 10-JUN-99 75 SQL> @ use_trigger1 Enter value for input_idno: 44444 Enter value for input_driveno: 200 Enter value for input_contamt: 50 PL/SQL procedure successfully completed. SQL> SELECT * FROM new_donation_one; IDNO DRI CONTDATE CONTAMT ----- --- --------- --------- 11111 100 07-JAN-99 35 33333 300 10-MAR-99 30 22222 100 14-MAR-99 10 23456 300 14-JUN-99 45 12121 300 10-JUN-99 75 First I executed @ use_trigger1 with data that did not match so no update could take place. Note that the data in new_donation_one did not get changed. However because the update was attempted, the trigger was fired and total_notes had a line inserted. More complex code would be needed to prevent this. However note that the sum did not change. SQL> SELECT * FROM total_notes; CHANGEDAT TOTAMT --------- --------- 27-JUL-99 160 27-JUL-99 195

Enter value for input_idno: 23456 Enter value for input_driveno: 300 triggers @ use_trigger1 executes the routine that updates the new_donation_one table. In this case I am changing the contribution from person 23456 to 99. The update triggers the execution of the trigger event which adds a record containing the new sum to the total_notes table. SQL> @ use_trigger1 Enter value for input_idno: 23456 Enter value for input_driveno: 300 Enter value for input_contamt: 99 PL/SQL procedure successfully completed. SQL> SELECT * FROM new_donation_one; IDNO DRI CONTDATE CONTAMT ----- --- --------- --------- 11111 100 07-JAN-99 35 33333 300 10-MAR-99 30 22222 100 14-MAR-99 10 23456 300 14-JUN-99 99 12121 300 10-JUN-99 75 SQL> SELECT * FROM total_notes; CHANGEDAT TOTAMT --------- --------- 27-JUL-99 160 27-JUL-99 195 27-JUL-99 249 The amount for 23456 used to be 45 - it has now changed to 99 because of the update done on contamt.

CREATE OR REPLACE TRIGGER trigger1 AFTER UPDATE ON new_donation_one triggers SQL> edit trigger1 CREATE OR REPLACE TRIGGER trigger1 AFTER UPDATE ON new_donation_one DECLARE v_date total_notes.changedate%TYPE :=sysdate; v_totamt total_notes.totamt%TYPE; BEGIN SELECT SUM(contamt) INTO v_totamt FROM new_donation_one; INSERT INTO total_notes (changedate, totamt) VALUES(v_date, v_totamt); END trigger1; / In this example, I substituted sysdate for the hardcoded date to make a more realistic trigger. Note that because I changed the trigger, I needed to use @trigger1 to create it again. SQL> @ trigger1 Trigger created. The change to the trigger has now been made so when I update new_donation_one the trigger will use the system date.

Input truncated to 1 characters Enter value for input_idno: 22222 trigger SQL> @ use_trigger1 Input truncated to 1 characters Enter value for input_idno: 22222 Enter value for input_driveno: 100 Enter value for input_contamt: 50 PL/SQL procedure successfully completed. SQL> SELECT * FROM new_donation_one; IDNO DRI CONTDATE CONTAMT ----- --- --------- --------- 11111 100 07-JAN-99 35 33333 300 10-MAR-99 30 22222 100 14-MAR-99 50 23456 300 14-JUN-99 99 12121 300 10-JUN-99 75 SQL> SELECT * FROM total_notes; CHANGEDAT TOTAMT --------- --------- 27-JUL-99 160 27-JUL-99 195 27-JUL-99 249 02-AUG-00 289 SQL> SELECT * FROM new_donation_one; IDNO DRI CONTDATE CONTAMT ----- --- --------- --------- 11111 100 07-JAN-99 35 33333 300 10-MAR-99 30 22222 100 14-MAR-99 10 23456 300 14-JUN-99 99 12121 300 10-JUN-99 75 Data prior to the update. Data after the update. The trigger now uses the system date instead of the hard coded date so the latest update has August second as its date. Record 22222 used to have 10, it got updated to 50. Remember the update is based on a match to both idno and driveno which would make the records unique as long as two donations were not made by the same person to the same drive on the same date. When the update first the trigger, the new total is written with the system date. Note that the new total sums what is currently in new_donation_one. Since I changed 10 to 50, the total is 40 bigger.