Oracle® Database Administrator's Guide 11g Release 2 (11.2) Part Number E10595-04 |
|
|
View PDF |
Database triggers are procedures that are stored in the database and activated ("fired") when specific conditions occur, such as adding a row to a table. You can use triggers to supplement the standard capabilities of the database to provide a highly customized database management system. For example, you can create a trigger to restrict DML operations against a table, allowing only statements issued during regular business hours.
Database triggers can be associated with a table, schema, or database. They are implicitly fired when:
DML statements are executed (INSERT
, UPDATE
, DELETE
) against an associated table
Certain DDL statements are executed (for example: ALTER
, CREATE
, DROP
) on objects within a database or schema
A specified database event occurs (for example: STARTUP
, SHUTDOWN
, SERVERERROR
)
This is not a complete list. See the Oracle Database SQL Language Reference for a full list of statements and database events that cause triggers to fire
Create triggers with the CREATE TRIGGER
statement. They can be defined as firing BEFORE
or AFTER
the triggering event, or INSTEAD OF
it. The following statement creates a trigger scott.emp_permit_changes
on table scott.emp
. The trigger fires before any of the specified statements are executed.
CREATE TRIGGER scott.emp_permit_changes
BEFORE
DELETE OR INSERT OR UPDATE
ON scott.emp
.
.
.
pl/sql block
.
.
.
You can later remove a trigger from the database by issuing the DROP TRIGGER
statement.
A trigger can be in either of two distinct modes:
Enabled
An enabled trigger executes its trigger body if a triggering statement is issued and the trigger restriction, if any, evaluates to true. By default, triggers are enabled when first created.
Disabled
A disabled trigger does not execute its trigger body, even if a triggering statement is issued and the trigger restriction (if any) evaluates to true.
To enable or disable triggers using the ALTER TABLE
statement, you must own the table, have the ALTER
object privilege for the table, or have the ALTER ANY TABLE
system privilege. To enable or disable an individual trigger using the ALTER TRIGGER
statement, you must own the trigger or have the ALTER ANY TRIGGER
system privilege.
See Also:
Oracle Database Concepts for a more detailed description of triggers
Oracle Database SQL Language Reference for syntax of the CREATE TRIGGER
statement
Oracle Database PL/SQL Language Reference for information about creating and using triggers
You enable a disabled trigger using the ALTER TRIGGER
statement with the ENABLE
option. To enable the disabled trigger named reorder
on the inventory
table, enter the following statement:
ALTER TRIGGER reorder ENABLE;
To enable all triggers defined for a specific table, use the ALTER TABLE
statement with the ENABLE ALL TRIGGERS
option. To enable all triggers defined for the INVENTORY
table, enter the following statement:
ALTER TABLE inventory ENABLE ALL TRIGGERS;
See Also:
Oracle Database SQL Language Reference for syntax and other information about theALTER TRIGGER
statementConsider temporarily disabling a trigger if one of the following conditions is true:
An object that the trigger references is not available.
You must perform a large data load and want it to proceed quickly without firing triggers.
You are loading data into the table to which the trigger applies.
You disable a trigger using the ALTER TRIGGER
statement with the DISABLE
option. To disable the trigger reorder
on the inventory
table, enter the following statement:
ALTER TRIGGER reorder DISABLE;
You can disable all triggers associated with a table at the same time using the ALTER TABLE
statement with the DISABLE ALL TRIGGERS
option. For example, to disable all triggers defined for the inventory
table, enter the following statement:
ALTER TABLE inventory DISABLE ALL TRIGGERS;