| Oracle8i SQL Reference Release 3 (8.1.7) Part Number A85397-01 |
|
SQL Statements:
CREATE SYNONYM to DROP ROLLBACK SEGMENT, 6 of 31
Use the CREATE TRIGGER statement to create and enable a database trigger, which is
Oracle automatically executes a trigger when specified conditions occur.
When you create a trigger, Oracle 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.
|
See Also:
|
Before a trigger can be created, the user SYS must run the SQL script DBMSSTDX.SQL. The exact name and location of this script depend on your operating system.
SCHEMA), you must have the CREATE TRIGGER privilege.
schema.SCHEMA), you must have the CREATE ANY TRIGGER privilege.
DATABASE, you must have the ADMINISTER DATABASE TRIGGER system privilege.
If the trigger issues SQL statements or calls procedures or functions, then the owner of the trigger must have the privileges necessary to perform these operations. These privileges must be granted directly to the owner, rather than acquired through roles.
OR REPLACE
Specify OR REPLACE to re-create the trigger if it already exists. Use this clause to change the definition of an existing trigger without first dropping it.
schema
Specify the schema to contain the trigger. If you omit schema, Oracle creates the trigger in your own schema.
trigger
Specify the name of the trigger to be created.
If a trigger produces compilation errors, it is still created, but it fails on execution. You can see the associated compiler error messages with the SQL*Plus command SHOW ERRORS. This means it effectively blocks all triggering DML statements until it is disabled, replaced by a version without compilation errors, or dropped.
BEFORE
Specify BEFORE to cause Oracle to fire the trigger before executing the triggering event. For row triggers, this is a separate firing before each affected row is changed.
Restrictions:
BEFORE trigger on a view or an object view.
BEFORE trigger for LOB columns, you can read the :OLD value but not the :NEW value. You cannot write either the :OLD or the :NEW value.
AFTER
Specify AFTER to cause Oracle to fire the trigger after executing the triggering event. For row triggers, this is a separate firing after each affected row is changed.
Restrictions:
AFTER trigger on a view or an object view.
AFTER trigger for LOB columns, you can read the :OLD value but not the :NEW value. You cannot write either the :OLD or the :NEW value.
INSTEAD OF
Specify INSTEAD OF to cause Oracle to fire the trigger instead of executing the triggering event. By default, INSTEAD OF triggers are activated for each row.
If a view is inherently updatable and has INSTEAD OF triggers, the triggers take preference. In other words, Oracle fires the triggers instead of performing DML on the view.
Restrictions:
INSTEAD OF is a valid clause only for views. You cannot specify an INSTEAD OF trigger on a table.
INSTEAD OF triggers, any views created on it must have INSTEAD OF triggers, even if the views are inherently updatable.
INSTEAD OF triggers for LOB columns, you can read both the :OLD and the :NEW value, but you cannot write either the :OLD or the :NEW values.
dml_event_clause
The dml_event_clause lets you specify one of three DML statements that can cause the trigger to fire. Oracle fires the trigger in the existing user transaction.
|
|
Specify |
|
|
|
Specify |
|
|
|
Specify
For an |
|
|
|
||
|
|
Restrictions:
|
|
|
Performing DML operations directly on nested table columns does not cause Oracle to fire triggers defined on the table containing the nested table column |
||
ddl_event
Specify one or more types of DDL statements that can cause the trigger to fire. You can create triggers for these events on DATABASE or SCHEMA unless otherwise noted. You can create BEFORE and AFTER triggers for these events. Oracle fires the trigger in the existing user transaction. The following values are valid:
database_event
Specify one or more particular states of the database that can cause the trigger to fire. You can create triggers for these events on DATABASE or SCHEMA unless otherwise noted. For each of these triggering events, Oracle opens an autonomous transaction scope, fires the trigger, and commits any separate transaction (regardless of any existing user transaction).
ON table | view
The ON clause lets you determine the database object on which the trigger is to be created.
referencing_clause
The referencing_clause lets you specify correlation names. You can use correlation names in the PL/SQL block and WHEN condition of a row trigger to refer specifically to old and new values of the current row. The default correlation names are OLD and NEW. If your row trigger is associated with a table named OLD or NEW, use this clause to specify different correlation names to avoid confusion between the table name and the correlation name.
OLD and NEW refer to the row of the nested table, and PARENT refers to the current row of the parent table.
OLD and NEW refer to object instances.
Restriction: This clause is valid only for DML event triggers (not DDL or database event triggers).
WHEN
Specify the trigger restriction, which is a SQL condition that must be satisfied for Oracle to fire the trigger. See the syntax description of condition in "Conditions". This condition must contain correlation names and cannot contain a query.
Restrictions:
INSTEAD OF trigger statements.
sql_block
Specify the PL/SQL block that Oracle executes to fire the trigger.
The PL/SQL block of a database trigger can contain one of a series of built-in functions in the SYS schema designed solely to extract system event attributes. These functions can be used only in the PL/SQL block of a database trigger.
Restrictions:
COMMIT, ROLLBACK, SAVEPOINT, and SET CONSTRAINT) if the block is executed within the same transaction.
See Also:
call_procedure_statement
The call_procedure_statement lets you call a stored procedure, rather than specifying inline the trigger code as a PL/SQL block. The syntax of this statement is the same as that for CALL, with the following exceptions:
INTO clause of CALL, because it applies only to functions.
expr.
NEW and :OLD.
This example creates a BEFORE statement trigger named emp_permit_changes in the schema scott. You would write such a trigger to place restrictions on DML statements issued on this table (such as when such statements could be issued).
CREATE TRIGGER scott.emp_permit_changes BEFORE DELETE OR INSERT OR UPDATE ON scott.emp pl/sql block
Oracle fires this trigger whenever a DELETE, INSERT, or UPDATE statement affects the emp table in the schema scott. The trigger emp_permit_changes is a BEFORE statement trigger, so Oracle fires it once before executing the triggering statement.
This example creates a BEFORE row trigger named salary_check in the schema scott. The PL/SQL block might specify, for example, that the employee's salary must fall within the established salary range for the employee's job:
CREATE TRIGGER scott.salary_check BEFORE INSERT OR UPDATE OF sal, job ON scott.emp FOR EACH ROW WHEN (new.job <> 'PRESIDENT') pl/sql_block
Oracle fires this trigger whenever one of the following statements is issued:
INSERT statement that adds rows to the emp table
UPDATE statement that changes values of the sal or job columns of the emp table
salary_check is a BEFORE row trigger, so Oracle fires it before changing each row that is updated by the UPDATE statement or before adding each row that is inserted by the INSERT statement.
salary_check has a trigger restriction that prevents it from checking the salary of the company president.
You could create the salary_check trigger 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 procedure scott.salary_check, which verifies that an employee's salary in in an appropriate range. Then you could create the trigger salary_check as follows:
CREATE TRIGGER scott.salary_check BEFORE INSERT OR UPDATE OF sal, job ON scott.emp FOR EACH ROW WHEN (new.job <> 'PRESIDENT') CALL check_sal(:new.job, :new.sal, :new.ename);
The procedure check_sal could be implemented in PL/SQL, C, or Java. Also, you can specify :OLD values in the CALL clause instead of :NEW values.
This example creates a trigger to log all errors. The PL/SQL block does some special processing for a particular error (invalid logon, error number 1017. This trigger is an AFTER statement 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;
This example creates an AFTER statement trigger on any DDL statement CREATE. Such a trigger can be used to audit the creation of new data dictionary objects in your schema.
CREATE TRIOGGER audit_db_object AFTER CREATE ON SCHEMA pl/sql_block
INSTEAD OF Trigger Example
In this example, customer data is stored in two tables. The object view all_customers is created as a UNION of the two tables, customers_sj and customers_pa. An INSTEAD OF trigger is used to insert values.
CREATE TABLE customers_sj ( cust NUMBER(6), address VARCHAR2(50), credit NUMBER(9,2) ); CREATE TABLE customers_pa ( cust NUMBER(6), address VARCHAR2(50), credit NUMBER(9,2) ); CREATE TYPE customer_t AS OBJECT ( cust NUMBER(6), address VARCHAR2(50), credit NUMBER(9,2), location VARCHAR2(20) ); CREATE VIEW all_customers (cust) AS SELECT customer_t (cust, address, credit, 'SAN_JOSE') FROM customers_sj UNION ALL SELECT customer_t (cust, address, credit, 'PALO_ALTO') FROM customers_pa; CREATE TRIGGER instrig INSTEAD OF INSERT ON all_customers FOR EACH ROW BEGIN IF (:new.cust.location = 'SAN_JOSE') THEN INSERT INTO customers_sj VALUES (:new.cust.cust, :new.cust.address,:new.cust.credit); ELSE INSERT INTO customers_pa VALUES (:new.cust.cust, :new.cust.address, :new.cust.credit); END IF; END;
|
|
![]() Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|