0% found this document useful (0 votes)
34 views3 pages

Triggers and Exceptions

Triggers are PL/SQL blocks that are automatically executed in response to events like data manipulation language (DML) statements or data definition language (DDL) statements. There are different types of triggers based on the triggering event like DML, DDL, or system triggers. A mutating trigger error occurs when a row-level trigger tries to query or modify the table being modified by the triggering statement. This can be avoided by using statement-level triggers or compound triggers which allow accessing the table in the before statement portion.

Uploaded by

Gajanand Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

Triggers and Exceptions

Triggers are PL/SQL blocks that are automatically executed in response to events like data manipulation language (DML) statements or data definition language (DDL) statements. There are different types of triggers based on the triggering event like DML, DDL, or system triggers. A mutating trigger error occurs when a row-level trigger tries to query or modify the table being modified by the triggering statement. This can be avoided by using statement-level triggers or compound triggers which allow accessing the table in the before statement portion.

Uploaded by

Gajanand Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Triggers and Exceptions

Q1. What is a Trigger?


A Trigger is a PL/SQL block structure which is automatically fired when an event occurs in the
database. The event can be DDL, DML or system events.

Types of Triggers basis on events:


1. DML Triggers – INSERT, UPDATE, DELETE
2. DDL Triggers – ALTER, DROP, TRUNCATE
3. System Triggers- LOGON, LOGOFF, SHUTDOWN,STARTUP
Others are:
4. Instead of Triggers : Triggers on Views.
5. Compound Trigger:- Similar to Compound Triggers but all the timing events, all the
information about the DML triggers can be written in single compound trigger.

DML Triggers are written over the top of a Table, Instead of Triggers are written over the top of
a view.

Purpose of Using Triggers:


a) Auditing
b) Enforcing Complex Referential Integrity
c) Logging
d) Enforcing Security of Transactions
e) Data Replicating to Multiple Tables
f) Preventing Invalid Data or Transactions
g) DDL Triggers for Logging and Auditing of DDL Statements
h) DDL Triggers to block unwanted DDL Statements
i) System Triggers to do some initial cleanup activity or to initiate jobs based on
Logon/Logoff or Shutdown/Up.

Q2. What is Mutating Trigger? How to avoid Mutating Trigger Error? How
Compound trigger is useful in avoiding “Mutating Trigger Error”?

Mutating Trigger:: A DML statement that fires a trigger which in turn tries to read or do a write
from the same table then we gets the Mutating Trigger.

Mutating error(ORA-04091) occurs whenever a row level trigger tries to modify or select data
from the table that is already undergoing change. Mutating error get raised from row level trigger
only, and not statement level trigger.
“ORA-04091: table emp_t is mutating, trigger/function may not see it.”

SELECT * FROM emp_t;

CREATE OR REPLACE TRIGGER trig_validate_sal


BEFORE UPDATE OF sal ON emp_t
FOR EACH ROW
DECLARE
-- lv_max_sal NUMBER:=100000;
lv_ceo_sal NUMBER;

BEGIN
SELECT sal INTO lv_ceo_sal
FROM emp_t
WHERE job='CEO' AND deptno=:new.deptno; /* Reason for Mutating Error*/

IF (:NEW.sal<lv_ceo_sal AND :old.job<>'CEO') OR (:OLD.job='CEO') THEN


INSERT INTO emp_sal_log VALUES(:NEW.empno,'Salary Updates Successfully:'|| 'OLD
SAL='||:old.sal||', NEW SAL= '||:new.sal);
ELSE
:NEW.sal:=:old.sal;
INSERT INTO emp_sal_log VALUES(:NEW.empno,'Salary NOT UPDATED: Employee salary
cannot be more than ' ||lv_ceo_sal');
END IF;
END;
/

After running below Update statement we will get the error:

UPDATE emp_t SET sal=45000 WHERE empno=1005;

HOW TO AVOID MUTATING ERROR?

Solution 1:
By creating Statement Level Trigger to get the value and that value can be stored in a
package where variable is defined i.e by Global Variable.

CREATE OR REPLACE PACKAGE PKG1 AS


lv_ceo_sal NUMBER;
END;
/

CREATE OR REPLACE TRIGGER trig_before_up_sal


BEFORE UPDATE OF sal ON emp_t
DECLARE
lv_max_sal NUMBER;
BEGIN
SELECT sal INTO PKG1.lv_ceo_sal
FROM emp_t
WHERE job='CEO';
END;
/

CREATE OR REPLACE TRIGGER trig_validate_sal


BEFORE UPDATE OF sal ON emp_t
FOR EACH ROW
DECLARE
lv_ceo_sal NUMBER;

BEGIN

IF (:NEW.sal<PKG1.lv_ceo_sal AND :old.job<>'CEO') OR (:OLD.job='CEO') THEN


INSERT INTO emp_sal_log VALUES(:NEW.empno,'Salary Updates Successfully:'|| 'OLD
SAL='||:old.sal||', NEW SAL= '||:new.sal);
ELSE
:NEW.sal:=:old.sal;
INSERT INTO emp_sal_log VALUES(:NEW.empno,'Salary NOT UPDATED: Employee salary
cannot be more than ' ||PKG1.lv_ceo_sal');
END IF;
END;
/

Solution2
Using Compound Trigger

CREATE OR REPLACE TRIGGER trig_validate_sal


FOR UPDATE ON EMP_T COMPUND TRIGGER
lv_ceo_sal NUMBER;
BEFORE STATEMENT IS
BEGIN
SELECT sal INTO lv_ceo_sal FROM emp_t WHERE job='CEO';
END BEFORE STATEMENT;

BEFORE EACH ROW IS


BEGIN

IF (:NEW.sal<lv_ceo_sal AND :old.job<>'CEO') OR (:OLD.job='CEO') THEN


INSERT INTO emp_sal_log VALUES(:NEW.empno,'Salary Updates Successfully:'|| 'OLD
SAL='||:old.sal||', NEW SAL= '||:new.sal);
ELSE
:NEW.sal:=:old.sal;
INSERT INTO emp_sal_log VALUES(:NEW.empno,'Salary NOT UPDATED: Employee salary
cannot be more than ' ||lv_ceo_sal');
END IF;
END BEFORE EACH ROW;
END trig_validate_sal;
/

You might also like