This Oracle tutorial explains how to create an AFTER DELETE Trigger in Oracle with syntax and examples.
An AFTER DELETE Trigger means that Oracle will fire this trigger after the DELETE operation is executed.
The syntax to create an AFTER DELETE Trigger in Oracle/PLSQL is:
CREATE [ OR REPLACE ] TRIGGER <strong>trigger_name</strong>
AFTER DELETE
ON <strong>table_name</strong>
[ FOR EACH ROW ]
DECLARE
-- variable declarations
BEGIN
-- trigger code
EXCEPTION
WHEN ...
-- exception handling
END;
Optional. If specified, it allows you to re-create the trigger is it already exists so that you can change the trigger definition without issuing a drop a trigger statement.
The name of the trigger to create.
It indicates that the trigger will fire after the DELETE operation is executed.
The name of the table that the trigger is created on.
Let's look at an example of how to create an AFTER DELETE trigger using the CREATE TRIGGER statement.
If you had a table created as follows:
CREATE TABLE orders
( order_id number(5),
quantity number(4),
cost_per_item number(6,2),
total_cost number(8,2)
);
We could then use the CREATE TRIGGER statement to create an AFTER DELETE trigger as follows:
CREATE OR REPLACE TRIGGER orders_after_delete
AFTER DELETE
ON orders
FOR EACH ROW
DECLARE
v_username varchar2(10);
BEGIN
-- Find username of person performing the DELETE on the table
SELECT user INTO v_username
FROM dual;
-- Insert record into audit table
INSERT INTO orders_audit
( order_id,
quantity,
cost_per_item,
total_cost,
delete_date,
deleted_by)
VALUES
( :old.order_id,
:old.quantity,
:old.cost_per_item,
:old.total_cost,
sysdate,
v_username );
END;
/