Skip to main content
The CREATE TRIGGER defines a on a specified table.

Required privileges

To create a trigger, a user must have the TRIGGER 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

create_trigger syntax diagram

Parameters

ParameterDescription
trigger_create_nameThe name of the trigger.
table_nameThe name of the table associated with the trigger.
func_nameThe that is executed when the trigger activates.
a_exprBoolean condition that determines if the trigger function should execute for a given row. For details, refer to .
trigger_func_argsA 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:
Populate lock_table with sample values:
Create a that prevents “locked” rows from being deleted:
Create a trigger that executes prevent_delete_locked before a DELETE is issued on lock_table:
Test the trigger by attempting to delete a row:
View 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:
Populate stock with three products each at 1000 count:
Create a that updates the stock table to reflect the quantity on hand after each order that is placed:
Create a trigger that executes update_stock_after_order after an INSERT is issued on orders_placed (i.e., an order is placed):
Test the trigger by inserting some sample orders:
View the stock table to see that the quantities have decreased accordingly:

See also