ISYS 365 - Triggers.

Slides:



Advertisements
Similar presentations
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
Advertisements

Manipulating Data Schedule: Timing Topic 60 minutes Lecture
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.
Creating Triggers.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Oracle PL/SQL TRIGGERS
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Triggers in SQL’99 CS561.
Triggers.
Triggers. What is a trigger? A trigger defines an action that the database should take when some event occurs in the application. It is triggered by an.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
Cs3431 Triggers vs Constraints Section 7.5. cs3431 Triggers (Make DB Active) Trigger: A procedure that starts automatically if specified changes occur.
Stored Procedures PL/SQL code stored in the database and executed when called by the user. Called by procedure name from another PL/SQL block or using.
Bordoloi and Bock PROCEDURES, FUNCTIONS & TRIGGERS.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
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.
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.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
1 ISYS Triggers. 2 Agenda Triggers Review Correlation identifiers (pseudo records) Restrictions on triggers Trigger usage Mutating tables Enabling.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
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
Creating DDL and Database Event Triggers. 2 home back first prev next last What Will I Learn? Describe events that cause DDL and database event triggers.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Distributed Database Applications COSC 5050 Week Six.
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.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
SQL Server 2012 Session: 1 Session: 12 Triggers Data Management Using Microsoft SQL Server.
Chapter 19: Triggers1 Chapter Nineteen Triggers Objective: – Understanding Triggers – Types of Triggers – Applications of Triggers – How to work with Triggers.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
1 Procedural Extension to SQL using Triggers - Lecture 2 Dr Akhtar AlI.
C Copyright © 2004, Oracle. All rights reserved. Studies for Implementing Triggers.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
ISYS Triggers.
ITEC 224 Database Programming
SQL: Schema Definition and Constraints Chapter 6 week 6
Creating Database Triggers
Active Database Concepts
Instructor: Jason Carter
SQL Stored Triggers Presented by: Dr. Samir Tartir
Introduction to Triggers
Introduction to Database Systems, CS420
Agenda Triggers Review Correlation identifiers (pseudo records)
ISYS Built-in Functions
الأزنده أ.موضي المحرج.
PL/SQL Programing : Triggers
Advanced SQL: Views & Triggers
Instructor: Mohamed Eltabakh
Instructor: Mohamed Eltabakh
Unit I-2.
Manipulating Data.
Introduction to Triggers
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Triggers.
Prof. Arfaoui. COM390 Chapter 9
Triggers in SQL’99 CS561.
Triggers 7/11/2019 See scm-intranet.
TRIGGERS.
Presentation transcript:

ISYS 365 - Triggers

Agenda Triggers Review Correlation identifiers (pseudo records) Restrictions on triggers Trigger usage Mutating tables Enabling and disabling triggers Data Dictionary

Triggers: Syntax CREATE OR REPLACE TRIGGER trigger_Name {BEFORE|AFTER} {INSERT|UPDATE| DELETE} [OF column_name] ON table_name [FOR EACH ROW] [WHEN trigger_condition] DECLARE -- variable declaration goes here BEGIN IF INSERTING THEN -- statements here ELSIF UPDATING THEN ELSIF DELETING THEN END IF; END [trigger_Name];

Recommended Naming Convention Table_Name_[A|B][I|U|D][S|R] [A|B] AFTER or BEFORE [I|U|D] INSERT, UPDATE, DELETE [S|R] statement-level or row-level Example: Employee_AIUS Employee_BUR Department_BIUDR

Triggers: Correlation Identifiers Row-level triggers can access individual column values :old and :new (called pseudo records) Syntactically treated as records of type triggering_table%ROWTYPE Reference fields within the pseudorecords using dot notation (just like implicit records) :old.salary :new.salary

Triggers: Correlation Identifiers :old.column_name Contains the value prior to the change NULL for INSERT statements :new.column_name Contains the value after the change NULL for DELETE statements

Triggers: Correlation Identifiers Testing :old value against :new value will only work for UPDATE statement :old.salary <> :new.salary INSERT has only :new value DELETE has only :old value

Example of correlation identifiers /* Log any changes to employee salary where the increase is greater than 10% */ CREATE OR REPLACE TRIGGER employee_BUR BEFORE UPDATE ON employee FOR EACH ROW WHEN (NEW.amount/OLD.amount > 1.1) BEGIN INSERT INTO Employee_Big_Change VALUES (:NEW.actionDate,:OLD.sal,:NEW.sal); END employee_BUR;

Triggers: Correlation Identifiers Approach 2 Approach 1 CREATE OR REPLACE TRIGGER employee_BUR BEFORE UPDATE OF SALARY ON EMPLOYEE FOR EACH ROW WHEN (OLD.SALARY <> NEW.SALARY) DECLARE v_Sal_Difference NUMBER; BEGIN -- do something here END employee_BUR; / NOTE: When using Approach 2, do NOT include the colons before the words OLD and NEW in the WHEN clause. CREATE OR REPLACE TRIGGER employee_BUR BEFORE UPDATE OF SALARY ON EMPLOYEE FOR EACH ROW DECLARE v_Sal_Difference NUMBER; BEGIN IF :old.salary <> :new.salary THEN -- do something here END IF; END employee_BUR; /

Restrictions on Triggers Cannot have any transaction control statements (e.g. COMMIT, ROLLBACK or SAVEPOINT) Cannot call procedures or functions that issue any transaction control statements Cannot declare any LONG or LONG RAW variables :new and :old cannot refer to LONG or LONG RAW column

Trigger Usage: Summary data: use a statement-level trigger CREATE OR REPLACE TRIGGER patient_AIDUS AFTER INSERT OR DELETE OR UPDATE ON patient DECLARE CURSOR c_stat IS SELECT doctor_ID, COUNT(*) total_patients FROM patient GROUP BY doctor_ID; BEGIN FOR v_StatRec IN c_stat LOOP UPDATE doctor_stats SET total_patients = v_StatRec.total_patients WHERE Doctor_ID = v_StatRec.Doctor_ID; IF SQL%NOTFOUND THEN INSERT INTO doctor_stats VALUES(v_StatRec.Doctor_ID, v_StatRec.total_patients); END IF; END LOOP; END patient_AIDUS; / Another example would be in an order processing system….define the trigger on the orderDetail table to update the total on the OrderHeader table.

Trigger Usage: Overriding the values supplied in an INSERT/UPDATE statement by changing the :new correlation value Use BEFORE INSERT or BEFORE UPDATE row-level trigger CREATE OR REPLACE TRIGGER patient_BIR BEFORE INSERT ON PATIENT FOR EACH ROW BEGIN SELECT sequence_patientID.NEXTVAL INTO :new.patientID FROM DUAL; END patient_BIR; / INSERT INTO patient (Fname, lname) VALUES (‘Bob’, ‘Smith’); CREATE SEQUENCE SEQUENCE_PATIENTID INCREMENT BY 1 START WITH 1; /* Note that Patient ID is automatically generated by the trigger. */

Trigger Usage Auditing: Use AFTER Row-level trigger

Auditing Example CREATE OR REPLACE TRIGGER CALC_DOCTOR_STATS_AUIDS AFTER INSERT OR UPDATE OR DELETE ON PATIENT DECLARE v_Dname VARCHAR2(30); v_Tot_Patients NUMBER; CURSOR C_DOCTOR_STATS IS SELECT PHY_LNAME, COUNT(SSN) FROM PATIENT, PHYSICIAN WHERE PHY_ID = PRIMARY_PHY_ID GROUP BY PHY_LNAME; BEGIN DELETE FROM DOCTOR_STATS; -- wipe doctor_stats records OPEN c_DOCTOR_STATS; LOOP -- walk through cursor FETCH c_DOCTOR_STATS INTO v_Dname, v_tot_patients; EXIT WHEN c_DOCTOR_STATS%NOTFOUND; -- create new doctor stat record for each doctor INSERT INTO DOCTOR_STATS VALUES(v_Dname, v_Tot_Patients); END LOOP; END CALC_DOCTOR_STATS_AUIDS;

Using Raise Application Error in Triggers CREATE OR REPLACE TRIGGER patient_bds BEFORE DELETE ON patient DECLARE e_weekend_error EXCEPTION; e_not_supervisor EXCEPTION; BEGIN IF TO_CHAR(SYSDATE, 'DY') = 'SAT' OR TO_CHAR(SYSDATE, 'DY') = 'SUN' THEN RAISE e_weekend_error; END IF; IF SUBSTR(user, 1, 3) <> 'SUP' THEN RAISE e_not_supervisor; EXCEPTION WHEN e_weekend_error THEN RAISE_APPLICATION_ERROR (-20001, 'Deletions allowed Mon thru Fri only'); WHEN e_not_supervisor THEN -- INSERT INTO SECURITY_VIOLATIONS VALUES(USER,SYSDATE); RAISE_APPLICATION_ERROR (-20002, 'You '||user||' are not authorised to perform deletions'); END; /

Exercise Suppose we have a Worker table as follows: worker(workerID, lname, gender, salary, commission, deptID) Declare a sequence for workerID that begins from 1. Write a trigger that automatically inserts the primary key with a sequential number when inserting a record in the worker table.

Exercise Trigger: Suppose we have the following two tables: OrderHeader(OrderID, Odate, CustID, Total) Order_Item(OrderID,ItemID, Qty, Subtotal) Write a statement-level trigger that updates the Total in the orderHeader table with the total value of the order_item records whenever an insert, update or delete event occurs on the order_item table. For any update error, raise an exception.

Enabling & Disabling Triggers ALTER TRIGGER trigger_name {ENABLE | DISABLE} Ex: ALTER TRIGGER Employee_BIUR DISABLE; ALTER TABLE table_name {ENABLE | DISABLE} ALL TRIGGERS; Ex: ALTER TABLE emp ENABLE ALL TRIGGERS; DROP TRIGGER trigger_name; Ex: DROP TRIGGER Employee_BIUR;

Example - In order to disable, substitute DISABLE for ENABLE - By default, all triggers are enabled when they are created - When a trigger is disabled, it still exists in the DD but is never fired - The STATUS column of USER_TRIGGERS contain either DISABLED or ENABLED value

Dropping Triggers Dropping Triggers DROP TRIGGER emp_bur; Trigger Dependencies - When a table is dropped, all triggers for that table are deleted from the DD ( When a table is dropped, all packages are disabled)

Data Dictionary user_triggers view Other views that list triggers trigger_type, table_name, triggering_event, status To list all of the triggers that you have created: SELECT trigger_type, table_name, triggering_event, status FROM user_triggers; Other views that list triggers all_triggers  list triggers that are accessible to current user (but might be owned by a different user) dba_triggers  list all triggers in the database

USER_TRIGGERS SQL> select trigger_name, trigger_type, status from user_triggers; TRIGGER_NAME TRIGGER_TYPE STATUS ------------------------------ ---------------- -------- LOGEMPCHANGE AFTER EACH ROW ENABLED SQL> select trigger_name, table_name from user_triggers; TRIGGER_NAME TABLE_NAME ------------------------------ ------------------------------ LOGEMPCHANGE EMPLOYEE SQL> select description from user_triggers; DESCRIPTION -------------------------------------------------------------------------------- LogEmpChange AFTER INSERT OR DELETE OR UPDATE ON Employee FOR EACH ROW

Mutating Tables A table is mutating when Constraining table It is being modified by a DML statement (the table on which the trigger is defined) It is being updated to enforce DELETE CASCADE constraints It is being read to enforce referential integrity (RI) constraints Constraining table A table that may need to be read from for RI

Mutating Tables SQL statements in Row-level trigger may NOT Read from or modify any mutating table Read from or modify the PK, UNIQUE or FK of a constraining table. They can, however, modify other columns in the constraining table(s). Restrictions above apply to: all Row-level triggers Statement-level triggers ONLY when the trigger would be fired as a result of DELETE CASCADE Single row INSERT statements do not have this mutating table problem