Rules

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

PostgreSQL allows you to define rules on tables or views (views are essentially tables in most respects). A rule allows the administrator to configure the database to execute certain events when a SELECT, UPDATE, INSERT, or DELETION takes place within the table.

In this example, you will create a rule called give_customer_discount on the table invoices.

  1. A rule is an object under a table, so to create a rule, first expand the table you wish to create the rule under (click the + beside the database name, then the + beside the table name).

  2. Right-click on Rules and select CREATE RULE.

  3. The first field in the dialog is the name of the rule. Type: give_customer_discount

  4. Select the table/view on which you want the rule. By default the table from which you invoke CREATE RULE is the one that is selected.

  5. In the On Event section, specify the event on which the rule should be executed. For this example, the event will be INSERT.

  6. In the Condition field, specify the condition that should occur when the specified event occurs. For this example, set the condition to: customer_id=10

  7. In the Action field, specify the action that is to be performed when the event and condition described above occur. For this example,set the action to:
    UPDATE invoices SET total_cost=total_cost*0.9::numeric(7,2) 
       WHERE invoice_number=new.invoice_number;

  8. Last is the Instead check box. If this is checked, the action in the Action field discussed above is performed instead of the requested action. For this example, leave Instead unchecked.

    The rule that you have specified says, "When adding an invoice to the database, if the invoice is for the customer with customer_id=10, set the total_cost to 9/10ths of the regular cost (that is, give this customer a 10% discount)."

  9. Click OK to create the rule.

Figure 4-40. The Rules Tree View and Detailed View

Note that the rule give_customer_discount has been added under the Rules node in the Tree View.

Refer to the Red Hat Database SQL Guide and Reference section on CREATE RULE for more information.

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

Dropping

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

Commenting

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