CREATE TRIGGER defines a on a specified table.
Required privileges
To create a trigger, a user must have theTRIGGER on the table and the EXECUTE privilege on the trigger function. By default, the public role has EXECUTE privilege on all functions, so this is granted automatically unless it has been revoked.
Synopsis
Parameters
| Parameter | Description |
|---|---|
trigger_create_name | The name of the trigger. |
table_name | The name of the table associated with the trigger. |
func_name | The that is executed when the trigger activates. |
a_expr | Boolean condition that determines if the trigger function should execute for a given row. For details, refer to . |
trigger_func_args | A comma-separated list of constant string arguments. |
Examples
The following are examples of basic triggers. For more detailed examples of trigger usage, see .Create a BEFORE trigger
Create a sample table:
lock_table with sample values:
prevent_delete_locked before a DELETE is issued on lock_table:
lock_table to verify that the row was not deleted:
Create an AFTER trigger
Create two sample tables. stock contains a product inventory, and orders_placed contains a list of orders on those products:
stock with three products each at 1000 count:
stock table to reflect the quantity on hand after each order that is placed:
update_stock_after_order after an INSERT is issued on orders_placed (i.e., an order is placed):
stock table to see that the quantities have decreased accordingly:

