The document discusses database triggers. It defines a trigger as a database object that is attached to a table and fires when an INSERT, UPDATE or DELETE occurs on that table. It provides the basic CREATE TRIGGER syntax and examples of row-level triggers that run code or output messages for data modifications. Triggers can be used to log changes, summarize audit data, or enforce business rules for DML statements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
24 views19 pages
Triggers
The document discusses database triggers. It defines a trigger as a database object that is attached to a table and fires when an INSERT, UPDATE or DELETE occurs on that table. It provides the basic CREATE TRIGGER syntax and examples of row-level triggers that run code or output messages for data modifications. Triggers can be used to log changes, summarize audit data, or enforce business rules for DML statements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19
Triggers
S.Naji Trigger
A trigger is a database object that is attached to
a table. In many aspects it is similar to a stored procedure. As a matter of fact, triggers are often referred to as a "special kind of stored procedure." The main difference between a trigger and a stored procedure is that the former is attached to a table and is only fired when an INSERT, UPDATE or DELETE occurs. You specify the modification action(s) that fire the trigger when it is created Trigger Format CREATE or REPLACE TRIGGER trigger_name BEFORE INSERT ON table_name [ FOR EACH ROW ] DECLARE -- variable declarations BEGIN -- trigger code EXCEPTION WHEN ... -- exception handling END; Simple example
CREATE TABLE to_table (col1 NUMBER);
Set serveroutput on;
Simple example
CREATE OR REPLACE TRIGGER before_row_trigger
BEFORE INSERT ON to_table FOR EACH ROW BEGIN DBMS_OUTPUT.PUT_LINE('Before Insert Row Level'); END; / Checking the trigger
Insert into to_table values(5);
(detail will be printed)
Creating error messages using output
CREATE OR REPLACE TRIGGER emp_dept_del
AFTER DELETE ON employee FOR EACH ROW BEGIN DBMS_OUTPUT.PUT_LINE (':OLD.id' || :OLD.id); END; / Example 1 (create table1)
create table company(product_id number(4)
not null, company_id NUMBER(8) not null, company_short_name varchar2(30) not null, company_long_name varchar2(60) );
Insert records
insert into company values(11,1001,'A Inc.','Long Name A Inc.');
(Create new table)
create table product_audit(
product_id number(4) not null, num_rows number(8) not null ); Trigger
CREATE OR REPLACE TRIGGER myTrigger
AFTER INSERT ON company FOR EACH ROW BEGIN UPDATE product_audit SET num_rows =num_rows+1 WHERE product_id =:NEW.product_id; IF (SQL%NOTFOUND) THEN INSERT INTO product_audit VALUES (:NEW.product_id,1); END IF; END; / Checking trigger
Insert records
insert into company values(2,1006,'F Inc.','Long Name F Inc.');
Out put
Select * from product_audit;
Example 2
CREATE TABLE employee_history (
name VARCHAR2(100), description VARCHAR2(255), occurred_on DATE);
CREATE TABLE employee_compensation (
company VARCHAR2(100), name VARCHAR2(100), compensation NUMBER, layoffs NUMBER); Procedure CREATE OR REPLACE PROCEDURE employee_audit ( name IN VARCHAR2, description IN VARCHAR2, occurred_on IN DATE ) IS PRAGMA AUTONOMOUS_TRANSACTION; BEGIN INSERT INTO employee_history VALUES ( employee_audit.name, employee_audit.description, employee_audit.occurred_on ); COMMIT; END; / Trigger
CREATE OR REPLACE TRIGGER bef_ins_ceo_comp
BEFORE INSERT ON employee_compensation FOR EACH ROW DECLARE ok BOOLEAN := TRUE; BEGIN IF ok THEN employee_audit ( :new.name, 'BEFORE INSERT', SYSDATE); END IF; END; / Executing
BEGIN INSERT INTO employee_compensation VALUES ('M', 'J', 9100000, 2700);
INSERT INTO employee_compensation VALUES ('A', 'H',
33200000, 3300);
INSERT INTO employee_compensation VALUES ('E', 'G',
NOT NULL primary key, First_Name VARCHAR2(10 BYTE),Last_Name VARCHAR2(10 BYTE), Start_Date DATE, End_Date DATE,Salary Number(8,2), City VARCHAR2(10 BYTE),Description VARCHAR2(15 BYTE) ) / Trigger create or replace trigger emp_bid before insert or delete on employee referencing new as new old as old begin if to_char(sysdate,'Dy') in ('Sat','Sun') then raise_application_error(-20999,'No create/delete employees on weekend!'); end if; end; / checking