DDL Triggers
Oracle allows you to define triggers that will fire when Data Definition Language (DDL) statements are executed. Simply put, DDL is any SQL statement used to create or modify a database object such as a table or an index. Here are some examples of DDL statements:
CREATE TABLE
ALTER INDEX
DROP TRIGGER
Each of these statements results in the creation, alteration, or removal of a database object.
The syntax for creating these triggers is remarkably similar to that of DML triggers, except that the firing events differ and they are not applied to individual tables.
Creating a DDL Trigger
To create (or replace) a DDL trigger, use the syntax shown here:
1 CREATE [OR REPLACE] TRIGGERtrigger name2 {BEFORE | AFTER } {DDL event} ON {DATABASE | SCHEMA} 3 [WHEN (...)] 4 DECLARE 5Variable declarations6 BEGIN 7 ...some code... 8 END;
The following table summarizes what is happening in this code:
|
Line(s) |
Description |
|
1 |
Specifies that a trigger is to be created with the name supplied. Specifying OR REPLACE is optional. If the trigger exists and REPLACE is not specified, then good old Oracle error ORA-4081 will appear stating just that. |
|
2 |
This line has a lot to say. It defines whether the trigger will fire before, after, or instead of the particular DDL event, as well as whether it will fire for all operations within the database or just within the current schema. Note that the INSTEAD OF option is available only in Oracle9i Release 1 and higher. |
|
3 |
An optional WHEN clause that allows ... |