2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_trigger.sgml,v 1.27 2002/08/22 00:01:40 tgl Exp $
3 PostgreSQL documentation
6 <refentry id="SQL-CREATETRIGGER">
8 <refentrytitle id="SQL-CREATETRIGGER-TITLE">CREATE TRIGGER</refentrytitle>
9 <refmiscinfo>SQL - Language Statements</refmiscinfo>
21 <date>2000-03-25</date>
24 CREATE TRIGGER <replaceable class="PARAMETER">name</replaceable> { BEFORE | AFTER } { <replaceable class="PARAMETER">event</replaceable> [OR ...] }
25 ON <replaceable class="PARAMETER">table</replaceable> FOR EACH { ROW | STATEMENT }
26 EXECUTE PROCEDURE <replaceable class="PARAMETER">func</replaceable> ( <replaceable class="PARAMETER">arguments</replaceable> )
29 <refsect2 id="R2-SQL-CREATETRIGGER-1">
31 <date>1998-09-21</date>
40 <term><replaceable class="parameter">name</replaceable></term>
43 The name to give the new trigger. This must be distinct from the name
44 of any other trigger for the same table.
49 <term><replaceable class="parameter">event</replaceable></term>
52 One of INSERT, DELETE or UPDATE.
57 <term><replaceable class="parameter">table</replaceable></term>
60 The name (optionally schema-qualified) of the table the trigger is for.
65 <term><replaceable class="parameter">func</replaceable></term>
68 A user-supplied function that is declared as taking no arguments
69 and returning type <literal>trigger</>.
74 <term><replaceable class="parameter">arguments</replaceable></term>
77 An optional comma-separated list of arguments to be provided to the
78 function when the trigger is executed, along with the standard trigger
79 data such as old and new tuple contents. The arguments are literal
80 string constants. Simple names and numeric constants may be written
81 here too, but they will all be converted to strings.
89 <refsect2 id="R2-SQL-CREATETRIGGER-2">
91 <date>1998-09-21</date>
100 <term><computeroutput>
102 </computeroutput></term>
105 This message is returned if the trigger is successfully created.
114 <refsect1 id="R1-SQL-CREATETRIGGER-1">
116 <date>1998-09-21</date>
123 <command>CREATE TRIGGER</command> will enter a new trigger into the current
124 data base. The trigger will be associated with the relation
125 <replaceable class="parameter">table</replaceable> and will execute
126 the specified function <replaceable class="parameter">func</replaceable>.
130 The trigger can be specified to fire either before BEFORE the
131 operation is attempted on a tuple (before constraints are checked and
132 the <command>INSERT</command>, <command>UPDATE</command> or
133 <command>DELETE</command> is attempted) or AFTER the operation has
134 been attempted (e.g., after constraints are checked and the
135 <command>INSERT</command>, <command>UPDATE</command> or
136 <command>DELETE</command> has completed). If the trigger fires before
137 the event, the trigger may skip the operation for the current tuple,
138 or change the tuple being inserted (for <command>INSERT</command> and
139 <command>UPDATE</command> operations only). If the trigger fires
140 after the event, all changes, including the last insertion, update,
141 or deletion, are <quote>visible</quote> to the trigger.
145 If multiple triggers of the same kind are defined for the same event,
146 they will be fired in alphabetical order by name.
150 <command>SELECT</command> does not modify any rows so you can not
151 create <command>SELECT</command> triggers. Rules and views are more
152 appropriate in such cases.
156 Refer to the chapters on SPI and Triggers in the
157 <citetitle>PostgreSQL Programmer's Guide</citetitle> for more
162 <refsect1 id="SQL-CREATETRIGGER-notes">
166 To create a trigger on a table, the user must have the
167 <literal>TRIGGER</literal> privilege on the table.
171 In <productname>PostgreSQL</productname> versions before 7.3, it was
172 necessary to declare trigger functions as returning the placeholder
173 type <type>opaque</>, rather than <type>trigger</>. This is still
174 supported, but is deprecated because it is obscure and causes loss of
179 As of the current release, <literal>STATEMENT</literal> triggers are not implemented.
183 Refer to the <xref linkend="sql-droptrigger"> command for
184 information on how to remove triggers.
188 <refsect1 id="R1-SQL-CREATETRIGGER-2">
189 <title>Examples</title>
192 Check if the specified distributor code exists in the distributors
193 table before appending or updating a row in the table films:
196 CREATE TRIGGER if_dist_exists
197 BEFORE INSERT OR UPDATE ON films FOR EACH ROW
198 EXECUTE PROCEDURE check_primary_key ('did', 'distributors', 'did');
203 Before cancelling a distributor or updating its code, remove every
204 reference to the table films:
206 CREATE TRIGGER if_film_exists
207 BEFORE DELETE OR UPDATE ON distributors FOR EACH ROW
208 EXECUTE PROCEDURE check_foreign_key (1, 'CASCADE', 'did', 'films', 'did');
213 The second example can also be done by using a foreign key,
217 CREATE TABLE distributors (
220 CONSTRAINT if_film_exists
221 FOREIGN KEY(did) REFERENCES films
222 ON UPDATE CASCADE ON DELETE CASCADE
228 <refsect1 id="SQL-CREATETRIGGER-compatibility">
229 <title>Compatibility</title>
236 There is no <command>CREATE TRIGGER</command> statement in <acronym>SQL92</acronym>.
245 The <command>CREATE TRIGGER</command> statement in
246 <productname>PostgreSQL</productname> implements a subset of the
247 SQL99 standard. The following functionality is missing:
251 SQL99 allows triggers to fire on updates to specific columns
252 (e.g., <literal>AFTER UPDATE OF col1, col2</literal>).
258 SQL99 allows you to define aliases for the <quote>old</quote>
259 and <quote>new</quote> rows or tables for use in the definition
260 of the triggered action (e.g., <literal>CREATE TRIGGER ... ON
261 tablename REFERENCING OLD ROW AS somename NEW ROW AS
262 othername ...</literal>). Since
263 <productname>PostgreSQL</productname> allows trigger
264 procedures to be written in any number of user-defined
265 languages, access to the data is handled in a
266 language-specific way.
272 <productname>PostgreSQL</productname> only has row-level
273 triggers, no statement-level triggers.
279 <productname>PostgreSQL</productname> only allows the
280 execution of a stored procedure for the triggered action.
281 SQL99 allows the execution of a number of other SQL commands,
282 such as <command>CREATE TABLE</command> as triggered action.
283 This limitation is not hard to work around by creating a
284 stored procedure that executes these commands.
291 SQL99 specifies that multiple triggers should be fired in
292 time-of-creation order. <productname>PostgreSQL</productname>
293 uses name order, which was judged more convenient to work with.
301 <title>See Also</title>
303 <simplelist type="inline">
304 <member><xref linkend="sql-createfunction"></member>
305 <member><xref linkend="sql-altertrigger"></member>
306 <member><xref linkend="sql-droptrigger"></member>
307 <member><citetitle>PostgreSQL Programmer's Guide</citetitle></member>
312 <!-- Keep this comment at the end of the file
317 sgml-minimize-attributes:nil
318 sgml-always-quote-attributes:t
321 sgml-parent-document:nil
322 sgml-default-dtd-file:"../reference.ced"
323 sgml-exposed-tags:nil
324 sgml-local-catalogs:"/usr/lib/sgml/catalog"
325 sgml-local-ecat-files:nil