9.3. Writing Triggers

Almost all sophisticated databases support triggers and so does PostgreSQL. Triggers are a method to execute functions automatically whenever a certain event happens. For instance, you can define a trigger that deletes a record from table A every time a record is inserted into table B. In this scenario, you would have to define a trigger on table B, which is waiting for records to be inserted.

A trigger can start almost all kinds of functions, but in many cases PL/pgSQL is used to implement triggers because it has been optimized for being integrated into the database. If you don't like writing PL/pgSQL functions, you can also use PL/Tcl or PL/Perl. However, in this section we will focus entirely on writing triggers with ...

Get PHP and PostgreSQL: Advanced Web Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.