Documente Academic
Documente Profesional
Documente Cultură
com/en/)
Download
()
Categories
Home (../../index.htm )
() CREATE
()
TRIGGER ()
Purpose
() () () Use
See Also:
()
Prerequisites
()
Before a trigger can be created, the user SYS must run a SQL script
commonly called DBMSSTDX.SQL. The exact name and location of this script
depend on your operating system.
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.
()
Syntax
() create_trigger::=
() DML_event_clause ::= ()
() referencing_clause::= ()
()
Semantics
() OR
REPLACE () ()
() schema
Specify the schema to contain the trigger. If you omit schema, then Oracle
Database creates the trigger in your own schema.
() trigger
Specify the name of the trigger to be created.
If a trigger produces compilation errors, then it is still created, but it fails on
execution. This means it effectively blocks all triggering DML statements until it
is disabled, replaced by a version without compilation errors, or dropped. You
can see the associated compiler error messages with the SQL*Plus command
SHOW ERRORS.
Note:
If you create a trigger on a base table of a materialized view, then
you must ensure that the trigger does not fire during a refresh of the
materialized view. During refresh, the DBMS_MVIEW procedure
I_AM_A_REFRESH returns TRUE .
() BEFORE () () ()
Specify BEFORE to cause the database to fire the trigger before executing the
triggering event. For row triggers, the trigger is fired before each affected row
is changed.
()
You can write to the : NEW value but not to the : OLD value.
() AFTER () () ()
Specify AFTER to cause the database to fire the trigger after executing the
triggering event. For row triggers, the trigger is fired after each affected row is
changed.
()
Note:
When you create a materialized view log for a table, Oracle Database
implicitly creates an AFTER ROW trigger on the table. This trigger
inserts a row into the materialized view log whenever an INSERT ,
UPDATE , or DELETE statement modifies data in the master table.
You cannot control the order in which multiple row triggers fire.
Therefore, you should not write triggers intended to affect the
content of the materialized view.
See Also:
CREATE MATERIALIZED VIEW LOG (statements_6003.htm#i2064649) for
more information on materialized view logs
() INSTEAD
OF () () () () ()
Note:
Oracle Database fine-grained access control lets you define rowlevel security policies on views. These policies enforce specified rules
in response to DML operations. If an INSTEAD OF trigger is also
defined on the view, then the database will not enforce the row-level
security policies, because the database fires the INSTEAD OF
trigger instead of executing the DML on the view.
()
INSTEAD OF Triggers
INSTEAD OF triggers are valid only for views. You cannot specify an
INSTEAD OF trigger on a table.
You can read both the : OLD and the : NEW value, but you cannot write either
the : OLD or the : NEW value.
() () Note:
You can create multiple triggers of the same type ( BEFORE ,
AFTER , or INSTEAD OF ) that fire for the same statement on the
same table. The order in which Oracle Database fires these triggers
is indeterminate. If your application requires that one trigger be fired
before another of the same type for the same statement, then
combine these triggers into a single trigger whose trigger action
performs the trigger actions of the original triggers in the appropriate
order.
See Also:
"Creating an INSTEAD OF Trigger: Example"
() DML_event_clause () () () () ()
The DML_event_clause lets you specify one of three DML statements that
can cause the trigger to fire. Oracle Database fires the trigger in the existing
user transaction.
You cannot specify the MERGE keyword in the DML_event_clause. If you
want a trigger to fire in relation to a MERGE operation, you must create triggers
on the INSERT and UPDATE operations to which the MERGE operation
decomposes.
See Also:
"Creating a DML Trigger: Examples"
() DELETE
Specify DELETE if you want the database to fire the trigger whenever a
DELETE statement removes a row from the table or removes an element from
a nested table.
() INSERT
Specify INSERT if you want the database to fire the trigger whenever an
INSERT statement adds a row to a table or adds an element to a nested
table.
() UPDATE
Specify UPDATE if you want the database to fire the trigger whenever an
UPDATE statement changes a value in one of the columns specified after OF.
If you omit OF, then the database fires the trigger whenever an UPDATE
statement changes a value in any column of the table or nested table.
For an UPDATE trigger, you can specify object type, varray, and REF columns
after OF to indicate that the trigger should be fired whenever an UPDATE
statement changes a value in one of the columns. However, you cannot
change the values of these columns in the body of the trigger itself.
Note:
Using OCI functions or the DBMS_LOB package to update LOB
values or LOB attributes of object columns does not cause Oracle
()
See Also:
AS subquery clause of CREATE VIEW (statements_8004.htm#i2065510)
Performing DML operations directly on nested table columns does not cause
Oracle Database 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 Database fires the trigger in the existing user transaction.
()
See Also:
"Creating a DDL Trigger: Example"
ALTER
Specify ALTER to fire the trigger whenever an ALTER statement modifies a
database object in the data dictionary.
()
ANALYZE
Specify ANALYZE to fire the trigger whenever the database collects or deletes
statistics or validates the structure of a database object.
See Also:
ANALYZE (statements_4005.htm#i2086320) for information on various ways
of collecting statistics
()
ASSOCIATE STATISTICS
Specify ASSOCIATE STATISTICS to fire the trigger whenever the database
associates a statistics type with a database object.
()
AUDIT
Specify AUDIT to fire the trigger whenever the database tracks the
occurrence of a SQL statement or tracks operations on a schema object.
()
COMMENT
Specify COMMENT to fire the trigger whenever a comment on a database
object is added to the data dictionary.
()
CREATE
Specify CREATE to fire the trigger whenever a CREATE statement adds a new
database object to the data dictionary.
()
DISASSOCIATE STATISTICS
Specify DISASSOCIATE STATISTICS to fire the trigger whenever the
database disassociates a statistics type from a database object.
()
DROP
Specify DROP to fire the trigger whenever a DROP statement removes a
database object from the data dictionary.
()
GRANT
Specify GRANT to fire the trigger whenever a user grants system privileges or
roles or object privileges to another user or to a role.
()
NOAUDIT
Specify NOAUDIT to fire the trigger whenever a NOAUDIT statement instructs
the database to stop tracking a SQL statement or operations on a schema
object.
()
RENAME
Specify RENAME to fire the trigger whenever a RENAME statement changes the
name of a database object.
()
REVOKE
Specify REVOKE to fire the trigger whenever a REVOKE statement removes
system privileges or roles or object privileges from a user or role.
()
TRUNCATE
Specify TRUNCATE to fire the trigger whenever a TRUNCATE statement
removes the rows from a table or cluster and resets its storage characteristics.
()
DDL
Specify DDL to fire the trigger whenever any of the preceding DDL statements
is issued.
() 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
Database opens an autonomous transaction scope, fires the trigger, and
commits any separate transaction (regardless of any existing user
transaction).
See Also:
"Creating a Database Event Trigger: Example"
()
SERVERERROR ()
Specify SERVERERROR to fire the trigger whenever a server error message is
logged.
The following errors do not cause a SERVERERROR trigger to fire:
( string, string)
()
LOGON ()
Specify LOGON to fire the trigger whenever a client application logs onto the
database.
()
LOGOFF ()
Specify LOGOFF to fire the trigger whenever a client application logs off the
database.
()
STARTUP
Specify STARTUP to fire the trigger whenever the database is opened.
()
SHUTDOWN
Specify SHUTDOWN to fire the trigger whenever an instance of the database is
shut down.
()
SUSPEND
Specify SUSPEND to fire the trigger whenever a server error causes a
transaction to be suspended.
()
DB_ROLE_CHANGE
In a Data Guard configuration, specify DB_ROLE_CHANGE to fire the trigger
whenever a role change occurs from standby to primary or from primary to
standby.
Notes:
Only AFTER triggers are relevant for LOGON, STARTUP,
SERVERERROR, SUSPEND, and DB_ROLE_CHANGE.
Only BEFORE triggers are relevant for LOGOFF and SHUTDOWN.
AFTER STARTUP and BEFORE SHUTDOWN triggers apply only to
DATABASE.
See Also:
PL/SQL User's Guide and Reference (../../appdev.102/b14261/toc.htm) for
more information on autonomous transaction scope
() ON
table | view () () ()
The ON clause lets you determine the database object on which the trigger is
to be created. Specify the schema and table or view name of one of the
following on which the trigger is to be created:
Table or view
Object table or object view
A column of nested-table type
If you omit schema, then Oracle Database assumes the table is in your own
schema. You can create triggers on index-organized tables.
()
Restriction on Schema
You cannot create a trigger on a table in the schema SYS.
() NESTED
TABLE Clause ()
Specify DATABASE to define the trigger on the entire database. The trigger
fires whenever any database user initiates the triggering event.
() SCHEMA
Specify SCHEMA to define the trigger on the current schema. The trigger fires
whenever any user connected as schema initiates the triggering event.
See Also:
"Creating a SCHEMA Trigger: Example"
() 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.
If the trigger is defined on a nested table, then OLD and NEW refer to the
row of the nested table, and PARENT refers to the current row of the parent
table.
If the trigger is defined on an object table or view, then OLD and NEW refer
to object instances.
()
() FOR
EACH ROW () () ()
Specify FOR EACH ROW to designate the trigger as a row trigger. Oracle
Database fires a row trigger once for each row that is affected by the
triggering statement and meets the optional trigger constraint defined in the
WHEN condition.
Except for INSTEAD OF triggers, if you omit this clause, then the trigger is a
statement trigger. Oracle Database fires a statement trigger only once when
the triggering statement is issued if the optional trigger constraint is met.
INSTEAD OF trigger statements are implicitly activated for each row.
()
() WHEN
Clause () ()
Specify the trigger condition, which is a SQL condition that must be satisfied
for the database to fire the trigger. See the syntax description of condition
in Chapter 7, "Conditions" (conditions.htm#g1077361). This condition must contain
correlation names and cannot contain a query.
The NEW and OLD keywords, when specified in the WHEN clause, are not
considered bind variables, so are not preceded by a colon (:). However, you
must precede NEW and OLD with a colon in all references other than the
WHEN clause.
See Also:
"Calling a Procedure in a Trigger Body: Example"
()
If you specify this clause for a DML event trigger, then you must also specify
FOR EACH ROW. Oracle Database evaluates this condition for each row
affected by the triggering statement.
You cannot specify trigger conditions for INSTEAD OF trigger statements.
You can reference object columns or their attributes, or varray, nested
table, or LOB columns. You cannot invoke PL/SQL functions or methods in
the trigger condition.
() pl/sql_block ()
Specify the PL/SQL block that Oracle Database executes to fire the trigger.
() () () The
You can reference and use LOB columns in the trigger action inside the
PL/SQL block. You can modify the : NEW values but not the : OLD values of
LOB columns within the trigger action.
See Also:
PL/SQL User's Guide and Reference (../../appdev.102/b14261/toc.htm) for
information on PL/SQL, including how to write PL/SQL blocks
Oracle Database Application Developer's Guide - Fundamentals
(../../appdev.102/b14251/toc.htm) for information on these functions
"Calling a Procedure in a Trigger Body: Example"
() call_procedure_statement () () ()
The call_procedure_statement lets you call a stored procedure rather
than specifying the trigger code inline as a PL/SQL block. The syntax of this
statement is the same as that for CALL (statements_4008.htm#BABDEHHG), with the
following exceptions:
You cannot specify the INTO clause of CALL, because it applies only to
functions.
You cannot specify bind variables in expr.
To reference columns of tables on which the trigger is being defined, you
must specify : NEW and : OLD.
See Also:
"Calling a Procedure in a Trigger Body: Example"
()
()
Examples ()
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:
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 condition that prevents it from checking the
Normally this view would not be updatable, because the primary key of the
orders table ( order_id) is not unique in the result set of the join view. To
make this view updatable, create an INSTEAD OF trigger on the view to
process INSERT statements 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 NOT
NULL columns receive values):
Contact Us (http://www.oracle.com/us/corporate/contact/index.html)
Legal Notices