DDL Triggers

Oracle8 introduced a long-awaited feature: the firing of triggers 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 TABLE

  • CREATE INDEX

  • CREATE TRIGGER / 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 tables.

Creating a DDL Trigger

To create (or replace) a DDL trigger, use the syntax shown here:

 1  CREATE [OR REPLACE] TRIGGER trigger name
 2  {BEFORE | AFTER| {DDL event} ON {DATABASE | SCHEMA}
 3  DECLARE
 4  Variable declarations
 5  BEGIN
 6  ... some code...
 7  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 4081 will appear stating just that.

2

This line has a lot to say. It defines whether the trigger will fire before or after the particular DDL event as well as whether it will fire for all operations within the database or just within the current schema.

3-7

These lines simply demonstrate the PL/SQL contents of the trigger.

Here’s an example of a somewhat uninformed ...

Get Oracle PL/SQL Programming, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.