COSC344 Database Theory and Applications. Lecture 11 Triggers

Similar documents
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

Course 492 Supplementary Materials. Mutating Tables

Studies for Implementing Triggers

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

Programming the Database

Trigger is a stored procedure which is called implicitly by oracle engine whenever a insert, update or delete statement is fired.

Lecture 08. Spring 2018 Borough of Manhattan Community College

DATABASE DESIGN II - 1DL400

5 Integrity Constraints and Triggers

Slides by: Ms. Shree Jaswal

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

Database Technology. Topic 6: Triggers and Stored Procedures

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

Assertions, Views, and Programming. CS157A Chris Pollett Oct. 31, 2005.

Enhanced Data Models for Advanced Applications. Active Database Concepts

Misc. Triggers Views Roles Sequences - Synonyms. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering

Chapter 14. Active Databases. Database Systems(Part 2) p. 200/286

Database Management Systems Triggers

5. Single-row function

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

SQL Server. Lecture3 Cascading referential integrity constraint

Overview. Data Integrity. Three basic types of data integrity. Integrity implementation and enforcement. Database constraints Transaction Trigger

Lab # 4. Data Definition Language (DDL)

Triggers- View-Sequence

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

UNIT-IV (Relational Database Language, PL/SQL)

Summary Modifications. Database Construction (and Usage) Explicit attribute lists. Insertions with queries. Quiz. Default values

Data Definition Language (DDL)

Introduction to Computer Science and Business

Introduction p. 1 The Logical and Physical View of Tables p. 1 Database Types p. 4 NULLs p. 6 DDL and DML Statements p. 7 Column and Table Constraint

COSC Assignment 2

ADVANTAGES. Via PL/SQL, all sorts of calculations can be done quickly and efficiently without use of Oracle engine.

Oracle Database: Program with PL/SQL

Sql Server Syllabus. Overview

Chapter 1 SQL and Data

Oracle Database 11g: SQL and PL/SQL Fundamentals

1Z0-144 Q&As Oracle Database 11g: Program with PL/ SQL

CSC 261/461 Database Systems Lecture 6. Fall 2017

SQL STRUCTURED QUERY LANGUAGE

Oracle 1Z Oracle9i: Program with PL/SQL. Download Full Version :

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

Oracle PLSQL Training Syllabus

Database design process

INDEX. 1 Introduction. 2 Types of Cursors 2.1 Explicit cursor 2.2 Attributes 2.3 Implicit cursor 2.4 Attributes. 3 Parameterized cursor

Basic SQL. Basic SQL. Basic SQL

CS2300: File Structures and Introduction to Database Systems

Oracle Database: SQL and PL/SQL Fundamentals NEW

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague

Oracle Database 12c: Program with PL/SQL Duration: 5 Days Method: Instructor-Led

Active Databases Part 1: Introduction CS561

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Actual4Test. Actual4test - actual test exam dumps-pass for IT exams

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Vendor: Oracle. Exam Code: 1Z Exam Name: Oracle Database 11g: Program with PL/ SQL. Version: Demo

SQL Data Definition and Data Manipulation Languages (DDL and DML)

Oracle Exam 1z0-144 Oracle Database 11g: Program with PL/SQL Version: 8.5 [ Total Questions: 103 ]

Module 9: Managing Schema Objects

Lesson 14 Transcript: Triggers

Slides by: Ms. Shree Jaswal

Oracle Database: Introduction to SQL/PLSQL Accelerated

Question Bank PL/SQL Fundamentals-I

An Introduction to Structured Query Language

Contents I Introduction 1 Introduction to PL/SQL iii

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Oracle Database 11g: Program with PL/SQL Release 2

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Oracle Database: Program with PL/SQL

PL/SQL. Exception. When the PL/SQL engine cannot execute the PLSQL block it raise an error. Every Oracle error has an error number

Oracle SQL & PL SQL Course

Oracle Database: Program with PL/SQL Ed 2

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

1 Prepared By Heena Patel (Asst. Prof)

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

DATABASDESIGN FÖR INGENJÖRER - 1DL124

Ders # 7. Veri Bütünlüğü Programlama ve Güvenlik. From Elmasri/Navathe textbook Ch9,26 Sciore textbook, Ch 9-10

Oracle PLSQL. Course Summary. Duration. Objectives

Question: Which statement would you use to invoke a stored procedure in isql*plus?

Conditionally control code flow (loops, control structures). Create stored procedures and functions.

Overview Relational data model

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Oracle Database 11g: Program with PL/SQL

2. Programming written ( main theme is to test our data structure knowledge, proficiency

ORACLE Job Placement Paper. Paper Type : General - other

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

Database Application Programs PL/SQL, Java and the Web

Intermediate SQL ( )

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

IZ0-144Oracle 11g PL/SQL Certification (OCA) training

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

Chapter 7 Constraints and Triggers. Spring 2011 Instructor: Hassan Khosravi

An Introduction to Structured Query Language

Oracle PL SQL Training & Certification

Assorted Topics Stored Procedures and Triggers Pg 1

ORACLE TRAINING. ORACLE Training Course syllabus ORACLE SQL ORACLE PLSQL. Oracle SQL Training Syllabus

An Introduction to Structured Query Language

Practical Project Report

Transcription:

COSC344 Database Theory and Applications Lecture 11 Triggers COSC344 Lecture 11 1

Overview Last Lecture - PL/SQL This Lecture - Triggers - Source: Lecture notes, Oracle documentation Next Lecture - Java & SQL - Source: Chapter 10 JDBC documentation Program examples COSC344 Lecture 11 2

What Is a Trigger A trigger is a PL/SQL stored subprogram associated with a table, and is automatically invoked by the DBMS in response to specified changes to the database. Triggers are commonly used to - Enforcement of complex business rules e.g. deleted customer records must be moved to a customer history table. - Enforcement of some types of referential integrity e.g. Oracle does not support ON UPDATE CASCADE - Auditing purpose (creating audit log) Who did what to my table? when? - Automatic generation the values for derived attributes - Creation of replica tables and backup files COSC344 Lecture 11 3

How does a trigger work Follows the Event-Condition-Action model - triggering event: the statement that causes the trigger to execute Triggering statement: INSERT, UPDATE, or DELETE Triggering timing: when the trigger is fired - BEFORE - Fire before the triggering SQL statement is executed - AFTER - Fire after the triggering SQL statement is executed - triggering condition: determines whether the action should be executed Condition is optional - action: a block of PL/SQL statements to be executed COSC344 Lecture 11 4

How to define a Trigger Trigger Syntax CREATE [OR REPLACE] TRIGGER name {BEFORE AFTER INSTEAD OF} {DELETE INSERT UPDATE [OF column [,column]...]} [OR {DELETE INSERT UPDATE [OF column [,column]...]}]... ON {TABLE VIEW} tablename [ [REFERENCING {OLD [AS] old NEW [AS] new}...] FOR EACH {ROW STATEMENT} [WHEN (condition)] ] PL/SQL block COSC344 Lecture 11 5

Types of Triggers Row-level triggers - Execute once for each row affected by the triggering event Statement-level triggers - Execute only once even multiple rows are affected by the triggering event. COSC344 Lecture 11 6

Correlation Values - NEW and OLD OLD.<attribute name> - The value of the attribute before a change from an UPDATE statement or before a DELETE statement. This value is NULL for INSERT statements. NEW.<attribute name> - The value of the attribute after an UPDATE statement or after an INSERT statement. This value is NULL for DELETE statements. Can be aliased - NEW AS newname - OLD AS oldname In the trigger body, NEW and OLD must be preceded by a colon (":"), but in the WHEN clause, they do not have a preceding colon! COSC344 Lecture 11 7

How to define a Trigger Trigger Syntax CREATE [OR REPLACE] TRIGGER name {BEFORE AFTER INSTEAD OF} {DELETE INSERT UPDATE [OF column [,column]...]} [OR {DELETE INSERT UPDATE [OF column [,column]...]}]... ON {TABLE VIEW} tablename [ [REFERENCING {OLD [AS] old NEW [AS] new}...] FOR EACH {ROW STATEMENT} [WHEN (condition)] ] PL/SQL block COSC344 Lecture 11 8

Row-level Trigger Example Deleted customer records must be moved to a customer history table. CREATE OR REPLACE TRIGGER after_delete_customer AFTER DELETE ON customers FOR EACH ROW BEGIN INSERT INTO cust_history(cno, cname, address) VALUES (:old.cnum,:old.cname,:old.city); END; / COSC344 Lecture 11 9

Statement-level Trigger Example Customer tables can only be modified between 8am to 6pm. CREATE OR REPLACE TRIGGER modify_customer BEFORE DELETE OR UPDATE OR INSERT ON customers BEGIN If to_char(sysdate, hh24 )< 08 OR to_char(sysdate, hh24 )> 18 THEN RAISE_APPLICATION_ERROR (-20001, Data can not be modified at this time ); END IF; END; / COSC344 Lecture 11 10

Derived Value Trigger Example The department relation has a derived attribute total_salary to store the total salary paid to the employees in each department. CREATE OR REPLACE TRIGGER modify_salary AFTER UPDATE OF salary ON employee FOR EACH ROW BEGIN UPDATE department SET total_salary = total_salary + :NEW.salary - :OLD.salary WHERE dnumber = :OLD.dno; END; / What is wrong with this trigger definition? COSC344 Lecture 11 11

Trigger Attributes Three Boolean trigger attributes that allow us to determine what DML activity has caused the trigger to execute - INSERTING True if the trigger is fired due to an INSERT operation - UPDATING True if the trigger is fired due to an UPDATE operation - DELETING True if the trigger is fired due to an DELETE operation Trigger attributes can be used in both row and statement triggers COSC344 Lecture 11 12

Derived Value Trigger Example (revisit) The department relation has a derived attribute total_salary to store the total salary paid to the employees in each department. CREATE OR REPLACE TRIGGER modify_salary AFTER INSERT OR UPDATE OR DELETE OF salary ON employee FOR EACH ROW BEGIN IF INSERTING THEN UPDATE department SET total_salary = total_salary + :NEW.salary WHERE dnumber = :NEW.dno; ELSIF UPDATING THEN UPDATE department SET total_salary = total_salary + :NEW.salary - :OLD.salary WHERE dnumber = :OLD.dno; ELSE -- deleting UPDATE department SET total_salary = total_salary - :OLD.salary WHERE dnumber = :OLD.dno; END IF; END; / COSC344 Lecture 11 13

Triggers Can Call Procedures CREATE OR REPLACE PROCEDURE sumit AS sal_sum employee.salary%type; BEGIN SELECT SUM(salary) INTO sal_sum FROM employee; UPDATE department SET total_salary = sal_sum; END; / CREATE OR REPLACE TRIGGER modify_salary AFTER UPDATE OF salary ON employee BEGIN sumit(); END; / COSC344 Lecture 11 14

Exceptions CREATE OR REPLACE TRIGGER modify_salary BEFORE UPDATE OF salary ON employee FOR EACH ROW DECLARE too_much EXCEPTION; BEGIN IF :NEW.salary>99000 THEN RAISE too_much; END IF; EXCEPTION WHEN too_much THEN RAISE_APPLICATION_ERROR (-20001, 'Cannot pay that much'); END; / COSC344 Lecture 11 15

Oracle s Execution Model Execute all BEFORE statement triggers that apply to the SQL statement. Loop for each row affected by the SQL statement - Execute all BEFORE row triggers that apply. - Change the row. Perform integrity constraint checking. - Execute all AFTER row triggers that apply. Complete integrity constraint checking. Execute all AFTER statement triggers that apply to the SQL statement. COSC344 Lecture 11 16

Referential Integrity Example Oracle does not support - ON UPDATE CASCADE - ON UPDATE SET NULL - ON UPDATE SET DEFAULT FOREIGN KEY (dno) REFERENCES department(dnumber)) ON DELETE CASCADE ON UPDATE CASCADE, CREATE OR REPLACE TRIGGER update_dno AFTER UPDATE OF dnumber ON department FOR EACH ROW BEGIN UPDATE employee SET dno = :new.dnumber WHERE dno =:old.dnumber; END; / Will this work? COSC344 Lecture 11 17

Mutating/Constraining Table Error (1) A mutating table is a table that is - currently being modified by an UPDATE, DELETE, or INSERT statement, - or a table that might need to be updated by the effects of a declarative DELETE CASCADE referential integrity constraint. Mutating error occurs when - the SQL statements of a trigger read from (query) or modify a mutating table of the triggering statement. COSC344 Lecture 11 18

Mutating/Constraining Error (2) CREATE OR REPLACE TRIGGER get_salary AFTER UPDATE ON employee FOR EACH ROW DECLARE total_salary NUMBER; BEGIN SELECT sum(salary) INTO total_salary FROM employee WHERE dno = :old.dno; DBMS_OUTPUT.PUT_LINE(total_salary); END; / COSC344 Lecture 11 19

Mutating/Constraining Table Error (4) A mutating table is a table that is - currently being modified by an UPDATE, DELETE, or INSERT statement, - or a table that might need to be updated by the effects of a declarative DELETE CASCADE referential integrity constraint. A constraining table is a table that a triggering statement might need to read - either directly, for a SQL statement, - or indirectly, for a declarative referential integrity constraint. Restriction on constraining table - The statements of a trigger cannot change the PRIMARY, FOREIGN, or UNIQUE KEY columns of a constraining table of the triggering COSC344 Lecture 11 20

Mutating/Constraining Error (5) create table P ( p1 number PRIMARY KEY); create table F ( f1 number references P (p1) on delete cascade); insert into p values (1); insert into p values (2); insert into p values (3); insert into f values (1); insert into f values (2); insert into f values (3); create trigger pf after update on P for each row begin if (:new.p1!= :old.p1) then update f set f1 = :new.p1 where f1 = :old.p1; end if; end; / update p set p1 = p1+1; SQL> select * from p; 2 3 4 SQL> select * from f; 4 4 4 COSC344 Lecture 11 21

Other Points Do not create recursive triggers SHOW ERRORS; SHOW ERRORS TRIGGER name; DROP TRIGGER name; ALTER TRIGGER name ENABLE; ALTER TRIGGER name DISABLE; ALTER TABLE name ENABLE ALL TRIGGERS; ALTER TABLE name DISABLE ALL TRIGGERS; COSC344 Lecture 11 22

See the course webpage Documentation Oracle 12 Server Application Development Database PL/SQL Language Reference PL/SQL Triggers COSC344 Lecture 11 23