CREATE CONSTRAINT TRIGGER(7) SQL Commands CREATE CONSTRAINT TRIGGER(7)NAME
CREATE CONSTRAINT TRIGGER - define a new constraint trigger
SYNOPSIS
CREATE CONSTRAINT TRIGGER name
AFTER event [ OR ... ]
ON table_name
[ FROM referenced_table_name ]
{ NOT DEFERRABLE | [ DEFERRABLE ] { INITIALLY IMMEDIATE | INITIALLY DEFERRED } }
FOR EACH ROW
EXECUTE PROCEDURE funcname ( arguments )
DESCRIPTION
CREATE CONSTRAINT TRIGGER creates a constraint trigger. This is the same as a regular trigger except that the timing of the trigger firing
can be adjusted using SET CONSTRAINTS [set_constraints(7)]. Constraint triggers must be AFTER ROW triggers. They can be fired either at
the end of the statement causing the triggering event, or at the end of the containing transaction; in the latter case they are said to be
deferred. A pending deferred-trigger firing can also be forced to happen immediately by using SET CONSTRAINTS.
PARAMETERS
name The name of the constraint trigger. This is also the name to use when modifying the trigger's behavior using SET CONSTRAINTS. The
name cannot be schema-qualified -- the trigger inherits the schema of its table.
event One of INSERT, UPDATE, or DELETE; this specifies the event that will fire the trigger. Multiple events can be specified using OR.
table_name
The (possibly schema-qualified) name of the table in which the triggering events occur.
referenced_table_name
The (possibly schema-qualified) name of another table referenced by the constraint. This option is used for foreign-key constraints
and is not recommended for general use.
DEFERRABLE
NOT DEFERRABLE
INITIALLY IMMEDIATE
INITIALLY DEFERRED
The default timing of the trigger. See the CREATE TABLE [create_table(7)] documentation for details of these constraint options.
funcname
The function to call when the trigger is fired. See CREATE TRIGGER [create_trigger(7)] for details.
arguments
Optional argument strings to pass to the trigger function. See CREATE TRIGGER [create_trigger(7)] for details.
COMPATIBILITY
CREATE CONSTRAINT TRIGGER is a PostgreSQL extension of the SQL standard.
SEE ALSO
CREATE TRIGGER [create_trigger(7)], DROP TRIGGER [drop_trigger(7)], SET CONSTRAINTS [set_constraints(7)]
SQL - Language Statements 2010-05-14 CREATE CONSTRAINT TRIGGER(7)
Check Out this Related Man Page
CREATE TRIGGER(7) SQL Commands CREATE TRIGGER(7)NAME
CREATE TRIGGER - define a new trigger
SYNOPSIS
CREATE TRIGGER name { BEFORE | AFTER } { event [OR ...] }
ON table FOR EACH { ROW | STATEMENT }
EXECUTE PROCEDURE func ( arguments )
INPUTS
name The name to give the new trigger. This must be distinct from the name of any other trigger for the same table.
event One of INSERT, DELETE or UPDATE.
table The name (optionally schema-qualified) of the table the trigger is for.
func A user-supplied function that is declared as taking no arguments and returning type trigger.
arguments
An optional comma-separated list of arguments to be provided to the function when the trigger is executed, along with the standard
trigger data such as old and new tuple contents. The arguments are literal string constants. Simple names and numeric constants may
be written here too, but they will all be converted to strings.
OUTPUTS
CREATE TRIGGER
This message is returned if the trigger is successfully created.
DESCRIPTION
CREATE TRIGGER will enter a new trigger into the current data base. The trigger will be associated with the relation table and will execute
the specified function func.
The trigger can be specified to fire either before BEFORE the operation is attempted on a tuple (before constraints are checked and the
INSERT, UPDATE or DELETE is attempted) or AFTER the operation has been attempted (e.g., after constraints are checked and the INSERT,
UPDATE or DELETE has completed). If the trigger fires before the event, the trigger may skip the operation for the current tuple, or change
the tuple being inserted (for INSERT and UPDATE operations only). If the trigger fires after the event, all changes, including the last
insertion, update, or deletion, are ``visible'' to the trigger.
If multiple triggers of the same kind are defined for the same event, they will be fired in alphabetical order by name.
SELECT does not modify any rows so you can not create SELECT triggers. Rules and views are more appropriate in such cases.
Refer to the chapters on SPI and Triggers in the PostgreSQL Programmer's Guide for more information.
NOTES
To create a trigger on a table, the user must have the TRIGGER privilege on the table.
In PostgreSQL versions before 7.3, it was necessary to declare trigger functions as returning the placeholder type opaque, rather than
trigger. To support loading of old dump files, CREATE TRIGGER will accept a function declared as returning opaque, but it will issue a
NOTICE and change the function's declared return type to trigger.
As of the current release, STATEMENT triggers are not implemented.
Refer to the DROP TRIGGER [drop_trigger(7)] command for information on how to remove triggers.
EXAMPLES
Check if the specified distributor code exists in the distributors table before appending or updating a row in the table films:
CREATE TRIGGER if_dist_exists
BEFORE INSERT OR UPDATE ON films FOR EACH ROW
EXECUTE PROCEDURE check_primary_key ('did', 'distributors', 'did');
Before cancelling a distributor or updating its code, remove every reference to the table films:
CREATE TRIGGER if_film_exists
BEFORE DELETE OR UPDATE ON distributors FOR EACH ROW
EXECUTE PROCEDURE check_foreign_key (1, 'CASCADE', 'did', 'films', 'did');
The second example can also be done by using a foreign key, constraint as in:
CREATE TABLE distributors (
did DECIMAL(3),
name VARCHAR(40),
CONSTRAINT if_film_exists
FOREIGN KEY(did) REFERENCES films
ON UPDATE CASCADE ON DELETE CASCADE
);
COMPATIBILITY
SQL92 There is no CREATE TRIGGER statement in SQL92.
SQL99 The CREATE TRIGGER statement in PostgreSQL implements a subset of the SQL99 standard. The following functionality is missing:
o SQL99 allows triggers to fire on updates to specific columns (e.g., AFTER UPDATE OF col1, col2).
o SQL99 allows you to define aliases for the ``old'' and ``new'' rows or tables for use in the definition of the triggered action
(e.g., CREATE TRIGGER ... ON tablename REFERENCING OLD ROW AS somename NEW ROW AS othername ...). Since PostgreSQL allows trigger
procedures to be written in any number of user-defined languages, access to the data is handled in a language-specific way.
o PostgreSQL only has row-level triggers, no statement-level triggers.
o PostgreSQL only allows the execution of a stored procedure for the triggered action. SQL99 allows the execution of a number of
other SQL commands, such as CREATE TABLE as triggered action. This limitation is not hard to work around by creating a stored
procedure that executes these commands.
SQL99 specifies that multiple triggers should be fired in time-of-creation order. PostgreSQL uses name order, which was judged more conve-
nient to work with.
SEE ALSO
CREATE FUNCTION [create_function(7)], ALTER TRIGGER [alter_trigger(l)], DROP TRIGGER [drop_trigger(l)], PostgreSQL Programmer's Guide
SQL - Language Statements 2002-11-22 CREATE TRIGGER(7)