Sub topic 9
Making Views Modifiable with INSTEAD OF Triggers
When a View Needs a Trigger
This chapter assumes you know basic SQL, PL/SQL blocks, tables, views, and transactions.
A view is a stored query that presents data from one or more tables. Simple views are often automatically modifiable. Oracle can translate an INSERT, UPDATE, or DELETE on the view into an operation on its base table.
Complex views are different. A view that joins tables, groups rows, or uses calculated columns may not be directly modifiable. An INSTEAD OF trigger tells Oracle what to do instead of applying the requested change directly to the view.
For example, this view combines employees and departments:
CREATE OR REPLACE VIEW emp_dept_v AS
SELECT e.employee_id,
e.last_name,
e.department_id,
d.department_name
FROM employees e
JOIN departments d
ON d.department_id = e.department_id;An insert into this view cannot insert into both tables automatically. An INSTEAD OF trigger can redirect the insert to employees.
CREATE OR REPLACE TRIGGER emp_dept_v_ioi
INSTEAD OF INSERT ON emp_dept_v
FOR EACH ROW
BEGIN
INSERT INTO employees
(employee_id, last_name, department_id)
VALUES
(:NEW.employee_id, :NEW.last_name, :NEW.department_id);
END emp_dept_v_ioi;
/The trigger fires instead of the original insert on emp_dept_v. The :NEW values represent the row that the client attempted to insert into the view. The trigger ignores :NEW.department_name because that value belongs to the departments table and is not needed when the department already exists.
The client can now use the view as an insert target:
INSERT INTO emp_dept_v
(employee_id, last_name, department_id)
VALUES
(employees_seq.NEXTVAL, 'Nguyen', 10);The trigger inserts the employee into the base table. A later query of the view displays the department name through the join.
Choosing the Operation
You can create separate INSTEAD OF triggers for INSERT, UPDATE, and DELETE, or one trigger that handles several events:
CREATE OR REPLACE TRIGGER emp_dept_v_iud
INSTEAD OF INSERT OR UPDATE OR DELETE ON emp_dept_v
FOR EACH ROW
BEGIN
IF INSERTING THEN
INSERT INTO employees
(employee_id, last_name, department_id)
VALUES
(:NEW.employee_id, :NEW.last_name, :NEW.department_id);
ELSIF UPDATING THEN
UPDATE employees
SET last_name = :NEW.last_name,
department_id = :NEW.department_id
WHERE employee_id = :OLD.employee_id;
ELSIF DELETING THEN
DELETE FROM employees
WHERE employee_id = :OLD.employee_id;
END IF;
END emp_dept_v_iud;
/For inserts, use :NEW. For updates, compare :OLD and :NEW. For deletes, use :OLD, because the row no longer has new values.
| Operation | Useful correlation values | Typical action |
|---|---|---|
| `INSERT` | `:NEW` | Insert into one or more base tables |
| `UPDATE` | `:OLD`, `:NEW` | Update base rows using the old key |
| `DELETE` | `:OLD` | Delete base rows using the old key |
Use these triggers when a view provides a useful data-entry interface. Keep the trigger's mapping explicit, and validate that referenced base-table rows exist.
Exercise
Which correlation value should a DELETE branch use in an INSTEAD OF trigger, and why?
Show answer
It should use :OLD because the deleted view row has no :NEW value.