Creating INSTEAD OF INSERT, UPDATE and DELETE Trigger
Syntax
CREATE [ OR REPLACE] TRIGGER trihgger_name
INSTEADOF
event1 [OR event2 OR event3]
ON view_name
[REFERENCING OLD AS old | NEW AS new]
[FOR EACH ROW]
trigger_body
Table
CREATETABLE Emp AS
SELECT Emp_id, name, salary, depart_id, email, job_id
FROM Employees;
CREATETABLE depart AS
SELECT d,depart_id, d.depart_name, d. location_id,sum(e.salary) tot_dept_sal
FROM Employees e, department d
WHERE e.depart_id = d.depart_id
GROUPBY d.depart_id, d. depart_name, d. lpcation_id;
View
CREATEVIEW Emp_details AS
SELECT e.EMP_id, e.name, e.salary, e.depart_id, e.email, e.job_id, d.depart_name,
d.location_id
FROM Employees e, department d
WHERE e.depart_id = d.depart_id;
Example
CREATEOR REPLACETRIGGER NEW_Emp_depart
INSTEADOF INSERTOR UPDATEOR DELETEON Emp_details
FOR EACH ROW
BEGIN
IF INSERTING THEN
INSERTINTO new_emps INSERT
VALUES(:NEW.Emp_id,:NEW.name,:NEW.salary,
:NEW.depart_id,:NEW.email,:NEW.job_id, SYSDATE);
UPDATE depart UPDATE
SET tot_depart_sal = tot_depart_sal+ :NEW.salary
WHERE depart_id = :NEW. depart_id;
ELSEIF DELETING THEN
DELETEFROM EMP DELETE
WHERE Emp_id = :OLD.Emp_id;
UPDATE depart
SET tot_depart_sal = tot_depart_sal- :OLD.salary
WHERE depart_id = :OLD.depart_id
ENDIF;
END;