PostgreSQL

Скачать в pdf «PostgreSQL»


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 INSERTUPDATE 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.


Refer to the chapters on SPI and Triggers in the RostgreSQL Programmer’s Guide for more information.


Notes


CREATE TRIGGER is a Postgres language extension.


Only the relation owner may create a trigger on this relation.


As of the current release (v7.0), STATEMENT triggers are not implemented.


Refer to DROP TRIGGER for information on how to remove triggers.


Usage


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 cheek_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’);


Compatibility


SQL92


There is no CREATE TRIGGER in SQL92.

Скачать в pdf «PostgreSQL»