Sub topic 14
DDL and Database Event Triggers
Assume you know basic PL/SQL blocks, exceptions, transactions, and Oracle data dictionary views. This chapter focuses on triggers that respond to schema changes or database events rather than row changes.
What DDL and Database Event Triggers Do
A DDL trigger runs when a user executes a data definition language statement such as CREATE, ALTER, or DROP. A database event trigger responds to events such as LOGON, STARTUP, or SERVERERROR.
These triggers are useful for enforcing rules that ordinary table constraints cannot express:
- Preventing unsafe
DROPorALTERoperations. - Recording who changed database objects.
- Setting session properties after logon.
- Responding to database startup or server errors.
A DDL trigger is statement-level. It does not use FOR EACH ROW, because a DDL statement does not modify table rows one at a time.
| Trigger scope | Meaning | Example |
|---|---|---|
| `ON SCHEMA` | Fires for events in one schema | Block table drops |
| `ON DATABASE` | Fires for events across the database | Audit logons |
DDL Trigger Syntax
The general form is:
CREATE OR REPLACE TRIGGER trigger_name
BEFORE OR AFTER event
ON SCHEMA OR DATABASE
BEGIN
-- PL/SQL statements
END trigger_name;
/Oracle supplies event attribute functions that describe the event. Common attributes include:
ORA_SYSEVENT: the event, such asDROPorALTER.ORA_DICT_OBJ_NAME: the affected object name.ORA_DICT_OBJ_TYPE: the object type, such asTABLE.ORA_DICT_OBJ_OWNER: the object's owner.ORA_LOGIN_USER: the user who caused the event.
The following trigger prevents table drops in its schema:
CREATE OR REPLACE TRIGGER no_drop_tables
BEFORE DROP ON SCHEMA
BEGIN
IF ORA_DICT_OBJ_TYPE = 'TABLE' THEN
RAISE_APPLICATION_ERROR(
-20003,
'Dropping tables is not allowed in this schema'
);
END IF;
END no_drop_tables;
/When a user tries to drop a table, Oracle runs the trigger before completing the DROP. The raised application error stops the operation.
Database Event Triggers
Database event triggers use events instead of table DML or DDL events. For example, an AFTER LOGON ON DATABASE trigger can initialize a session:
CREATE OR REPLACE TRIGGER set_session_format
AFTER LOGON ON DATABASE
BEGIN
EXECUTE IMMEDIATE
'ALTER SESSION SET NLS_DATE_FORMAT = ''YYYY-MM-DD''';
END set_session_format;
/The trigger runs for every session in the database. A schema-level logon trigger can be limited to sessions associated with one user, while a database-level trigger affects all users.
The account creating a database-wide trigger needs appropriate administration privileges. Limit its logic to safe, predictable actions. A broken logon trigger can prevent users from connecting, so keep an administrative recovery account available.
Event Flow
DDL trigger code should avoid unbounded recursive DDL. For example, creating or altering objects inside a DDL trigger can fire additional DDL triggers and make behavior difficult to predict.
Exercise
Which event attribute function returns the type of object affected by a DDL event?
Show answer
ORADICTOBJ_TYPE returns values such as TABLE, VIEW, or INDEX.