Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Semestr2 / 1 - Oracle / Oracle selected docs / Database concepts.pdf
Скачиваний:
29
Добавлен:
12.05.2015
Размер:
6.96 Mб
Скачать

Types of Triggers

Triggers on System Events and User Events

You can use triggers to publish information about database events to subscribers. Applications can subscribe to database events just as they subscribe to messages from other applications. These database events can include:

System events

Database startup and shutdown

Server error message events User events

User logon and logoff

DDL statements (CREATE, ALTER, and DROP)

DML statements (INSERT, DELETE, and UPDATE)

Triggers on system events can be defined at the database level or schema level. For example, a database shutdown trigger is defined at the database level:

CREATE TRIGGER register_shutdown ON DATABASE

SHUTDOWN

BEGIN

...

DBMS_AQ.ENQUEUE(...);

...

END;

Triggers on DDL statements or logon/logoff events can also be defined at the database level or schema level. Triggers on DML statements can be defined on a table or view. A trigger defined at the database level fires for all users, and a trigger defined at the schema or table level fires only when the triggering event involves that schema or table.

Event Publication

Event publication uses the publish-subscribe mechanism of Oracle Advanced Queuing. A queue serves as a message repository for subjects of interest to various subscribers. Triggers use the DBMS_AQ package to enqueue a message when specific system or user events occur.

17-14 Oracle9i Database Concepts

Types of Triggers

See Also:

Oracle9i Application Developer’s Guide - Advanced Queuing

Oracle9i Supplied PL/SQL Packages and Types Reference

Event Attributes

Each event allows the use of attributes within the trigger text. For example, the database startup and shutdown triggers have attributes for the instance number and the database name, and the logon and logoff triggers have attributes for the username. You can specify a function with the same name as an attribute when you create a trigger if you want to publish that attribute when the event occurs. The attribute’s value is then passed to the function or payload when the trigger fires. For triggers on DML statements, the :OLD column values pass the attribute’s value to the :NEW column value.

System Events

System events that can fire triggers are related to instance startup and shutdown and error messages. Triggers created on startup and shutdown events have to be associated with the database. Triggers created on error events can be associated with the database or with a schema.

STARTUP triggers fire when the database is opened by an instance. Their attributes include the system event, instance number, and database name.

SHUTDOWN triggers fire just before the server starts shutting down an instance. You can use these triggers to make subscribing applications shut down completely when the database shuts down. For abnormal instance shutdown, these triggers cannot be fired. The attributes of SHUTDOWN triggers include the system event, instance number, and database name.

SERVERERROR triggers fire when a specified error occurs, or when any error occurs if no error number is specified. Their attributes include the system event and error number.

User Events

User events that can fire triggers are related to user logon and logoff, DDL statements, and DML statements.

Triggers on LOGON and LOGOFF Events LOGON and LOGOFF triggers can be associated with the database or with a schema. Their attributes include the system event and username, and they can specify simple conditions on USERID and USERNAME.

Triggers 17-15

Types of Triggers

LOGON triggers fire after a successful logon of a user.

LOGOFF triggers fire at the start of a user logoff.

Triggers on DDL Statements DDL triggers can be associated with the database or with a schema. Their attributes include the system event, the type of schema object, and its name. They can specify simple conditions on the type and name of the schema object, as well as functions like USERID and USERNAME. DDL triggers include the following types of triggers:

BEFORE CREATE and AFTER CREATE triggers fire when a schema object is created in the database or schema.

BEFORE ALTER and AFTER ALTER triggers fire when a schema object is altered in the database or schema.

BEFORE DROP and AFTER DROP triggers fire when a schema object is dropped from the database or schema.

Triggers on DML Statements DML triggers for event publication are associated with a table. They can be either BEFORE or AFTER triggers that fire for each row on which the specified DML operation occurs. You cannot use INSTEAD OF triggers on views to publish events related to DML statements—instead, you can publish events using BEFORE or AFTER triggers for the DML operations on a view’s underlying tables that are caused by INSTEAD OF triggers.

The attributes of DML triggers for event publication include the system event and the columns defined by the user in the SELECT list. They can specify simple conditions on the type and name of the schema object, as well as functions (such as UID, USER, USERENV, and SYSDATE), pseudocolumns, and columns. The columns can be prefixed by :OLD and :NEW for old and new values. Triggers on DML statements include the following triggers:

BEFORE INSERT and AFTER INSERT triggers fire for each row inserted into the table.

BEFORE UPDATE and AFTER UPDATE triggers fire for each row updated in the table.

BEFORE DELETE and AFTER DELETE triggers fire for each row deleted from the table.

17-16 Oracle9i Database Concepts

Соседние файлы в папке Oracle selected docs