Triggers

In the object hierarchy under any table, you will find Triggers. Triggers can be added by selecting CREATE TRIGGER from the Triggers context menu. Once a trigger has been created, it can be commented on and dropped. This section describes how to create, comment on, and drop a trigger with RHDB Administrator.

In this example, you will create a trigger called logger.

  1. A trigger is an object under a table, so to create a trigger, first expand the table under which you wish to create the trigger (click the + beside the table name). In this example, choose the table named invoices.

  2. Right-click on Triggers and choose CREATE TRIGGER. The CREATE TRIGGER dialog box appears.

  3. In the Name For The New Trigger field, type: logger

  4. The table to which the trigger will apply (invoices) is already selected in the Table combo box as that is the table from which the menu option was activated. Note that you can select any other table as the triggered table, so you can create a trigger for any table using the context menu from any other table in the same schema.

  5. The Procedure combo box allows the selection of the PostgreSQL function that will be activated when the trigger fires, which is usually referred to as the "trigger function". This function must have been previously created and registered with the database (refer to the Red Hat Database Programmer's Guide for information on how to create trigger functions). For this example choose invoices_logger(). An optional entry box is provided in case the trigger function requires arguments.

  6. Two sets of buttons allow for the specification of when and under what circumstances a trigger should fire. The first set of buttons define When To Fire the trigger, before or after the event that caused the trigger to fire. For this example choose After. The On Events set of check boxes define which events fire the triggers, which can be any combination of INSERT, DELETE, or UPDATE operations. For this example select all three events.

  7. Click OK to create the trigger.

Figure 4-49. The Triggers Tree View and Detailed View

Note that the trigger logger has been added under the Triggers node in the Tree View.

RHDB Administrator also enables you to perform the following operations on existing triggers:

Dropping

To drop a trigger, right-click on it and select DROP TRIGGER. If View -> Ask For Confirmations is enabled, you are asked to confirm that you want the trigger dropped; if it is disabled, the trigger is dropped immediately. This action cannot be undone.

Commenting

PostgreSQL allows commenting on trigger, which makes them easier to identify. To comment on a trigger, right-click on it and select COMMENT. A dialog asks for the comment. If there is already a comment on the trigger, this comment is displayed. Type the new comment in the edit box and click OK (or press [Enter]) to save the new comment.

Note

SELECT TRIGGER commands are used to implement views in PostgreSQL. The CREATE VIEW command is the appropriate method for creating views and you must not create triggers on SELECT directly. For this reason, an option to fire a trigger on SELECT has not been provided, despite the fact that the SQL syntax allows it.

Refer to the Red Hat Database SQL Guide and Reference for more information on views and triggers.