Purpose
- A stored PL/SQL block associated with a table, a schema, or the database or
- An anonymous PL/SQL block or a call to a procedure implemented in PL/SQL or Java
Oracle Database automatically executes a trigger when specified conditions occur.
When you create a trigger, the database enables it automatically. You can subsequently disable and enable a trigger with the
DISABLE and ENABLE clause of the ALTER TRIGGER or ALTER TABLE statement.
Creating a DML Trigger: Examples This example shows the basic syntax for a
BEFORE statement trigger. You would write such a trigger to place restrictions on DML statements issued on a table, for example, when such statements could be issued.CREATE TRIGGER schema.trigger_name BEFORE DELETE OR INSERT OR UPDATE ON schema.table_name pl/sql_block
Oracle Database fires such a trigger whenever a DML statement affects the table. This trigger is a
BEFORE statement trigger, so the database fires it once before executing the triggering statement.
The next example shows a partial
BEFORE row trigger. The PL/SQL block might specify, for example, that an employee's salary must fall within the established salary range for the employee's job:CREATE TRIGGER hr.salary_check
BEFORE INSERT OR UPDATE OF salary, job_id ON hr.employees
FOR EACH ROW
WHEN (new.job_id <> 'AD_VP')
pl/sql_block
Oracle Database fires this trigger whenever one of the following statements is issued:
- An
INSERTstatement that adds rows to theemployeestable - An
UPDATEstatement that changes values of thesalaryorjob - An
UPDATEstatement that changes values of thesalaryorjob_idcolumns of theemployeestable
salary_checkis aBEFORErow trigger, so the database fires it before changing each row that is updated by theUPDATEstatement or before adding each row that is inserted by theINSERTstatement.salary_checkhas a trigger condition that prevents it from checking the salary of the administrative vice president (AD_VP).Creating a DDL Trigger: Example This example creates anAFTERstatement trigger on any DDL statementCREATE. Such a trigger can be used to audit the creation of new data dictionary objects in your schema.CREATE TRIGGER audit_db_object AFTER CREATE ON SCHEMA pl/sql_blockCalling a Procedure in a Trigger Body: Example You could create thesalary_checktrigger described in the preceding example by calling a procedure instead of providing the trigger body in a PL/SQL block. Assume you have defined a procedurecheck_salin thehrschema, which verifies that an employee's salary is in an appropriate range. Then you could create the triggersalary_checkas follows:CREATE TRIGGER salary_check BEFORE INSERT OR UPDATE OF salary, job_id ON employees FOR EACH ROW WHEN (new.job_id <> 'AD_VP') CALL check_sal(:new.job_id, :new.salary, :new.last_name)
The procedurecheck_salcould be implemented in PL/SQL, C, or Java. Also, you can specify :OLDvalues in theCALLclause instead of :NEWvalues.- Creating a Database Event Trigger: Example This example shows the basic syntax for a trigger to log all errors. The hypothetical PL/SQL block does some special processing for a particular error (invalid logon, error number 1017). This trigger is an
AFTERstatement trigger, so it is fired after an unsuccessful statement execution, such as unsuccessful logon.CREATE TRIGGER log_errors AFTER SERVERERROR ON DATABASE BEGIN IF (IS_SERVERERROR (1017)) THEN <special processing of logon error> ELSE <log error number> END IF; END;
Creating an INSTEAD OF Trigger: Example In this example, anoe.order_infoview is created to display information about customers and their orders:CREATE VIEW order_info AS SELECT c.customer_id, c.cust_last_name, c.cust_first_name, o.order_id, o.order_date, o.order_status FROM customers c, orders o WHERE c.customer_id = o.customer_id;Normally this view would not be updatable, because the primary key of theorderstable (order_id) is not unique in the result set of the join view. To make this view updatable, create anINSTEADOFtrigger on the view to processINSERTstatements directed to the view. The PL/SQL trigger implementation is shown in italics. - Creating a Database Event Trigger: Example This example shows the basic syntax for a trigger to log all errors. The hypothetical PL/SQL block does some special processing for a particular error (invalid logon, error number 1017). This trigger is an
AFTERstatement trigger, so it is fired after an unsuccessful statement execution, such as unsuccessful logon.CREATE TRIGGER log_errors AFTER SERVERERROR ON DATABASE BEGIN IF (IS_SERVERERROR (1017)) THEN <special processing of logon error> ELSE <log error number> END IF; END;
Creating an INSTEAD OF Trigger: Example In this example, anoe.order_infoview is created to display information about customers and their orders:CREATE VIEW order_info AS SELECT c.customer_id, c.cust_last_name, c.cust_first_name, o.order_id, o.order_date, o.order_status FROM customers c, orders o WHERE c.customer_id = o.customer_id;Normally this view would not be updatable, because the primary key of theorderstable (order_id) is not unique in the result set of the join view. To make this view updatable, create anINSTEADOFtrigger on the view to processINSERTstatements directed to the view. The PL/SQL trigger implementation is shown in italics. CREATE OR REPLACE TRIGGER order_info_insert INSTEAD OF INSERT ON order_info DECLARE duplicate_info EXCEPTION; PRAGMA EXCEPTION_INIT (duplicate_info, -00001); BEGIN INSERT INTO customers (customer_id, cust_last_name, cust_first_name) VALUES ( :new.customer_id, :new.cust_last_name, :new.cust_first_name); INSERT INTO orders (order_id, order_date, customer_id) VALUES ( :new.order_id, :new.order_date, :new.customer_id); EXCEPTION WHEN duplicate_info THEN RAISE_APPLICATION_ERROR ( num=> -20107, msg=> 'Duplicate customer or order ID'); END order_info_insert; /
- You can now insert into both base tables through the view (as long as all
NOTNULLcolumns receive values):INSERT INTO order_info VALUES (999, 'Smith', 'John', 2500, '13-MAR-2001', 0);
Creating a SCHEMA Trigger: Example The following example creates aBEFOREstatement trigger on the sample schemahr. When a user connected ashrattempts to drop a database object, the database fires the trigger before dropping the object:CREATE OR REPLACE TRIGGER drop_trigger BEFORE DROP ON hr.SCHEMA BEGIN RAISE_APPLICATION_ERROR ( num => -20000, msg => 'Cannot drop object'); END;