Oracle Create Trigger

Oracle Create Trigger

To create a trigger uses the CREATE statement.
A Trigger is a stored PL/SQL block associated with a table, a schema, or the database.
The events that fire a trigger are:
DML statements
DDL statements
System events such as startup, shutdown, and error messages
User events such as logon and logoff

Create Trigger syntax

CREATE OR REPLACE TRIGGER trg_name 
  {BEFORE | AFTER | INSTEAD OF} 
  {INSERT | UPDATE | DELETE} 
    [OF column_name] 
ON table_name 
 [REFERENCING OLD AS old NEW AS new] 
[FOR EACH ROW] 
WHEN condition
DECLARE
	-- variables 
BEGIN
	-- DML statements 
	-- DDL statements 
EXCEPTION
   -- raise message
END;

Create Trigger example

CREATE TRIGGER cust_log
      BEFORE INSERT OR UPDATE OR DELETE ON customers
      FOR EACH ROW
BEGIN
INSERT INTO customers_log(ID, NAME, ADDRESS) 
VALUES (:NEW.id,:NEW.name,:NEW.address);
END;
/