]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/create_trigger.sgml
Add a bunch of pseudo-types to replace the behavior formerly associated
[postgresql] / doc / src / sgml / ref / create_trigger.sgml
1 <!--
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
4 -->
5
6 <refentry id="SQL-CREATETRIGGER">
7  <refmeta>
8   <refentrytitle id="SQL-CREATETRIGGER-TITLE">CREATE TRIGGER</refentrytitle>
9   <refmiscinfo>SQL - Language Statements</refmiscinfo>
10  </refmeta>
11  <refnamediv>
12   <refname>
13    CREATE TRIGGER
14   </refname>
15   <refpurpose>
16    define a new trigger
17   </refpurpose>
18  </refnamediv>
19  <refsynopsisdiv>
20   <refsynopsisdivinfo>
21    <date>2000-03-25</date>
22   </refsynopsisdivinfo>
23   <synopsis>
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> )
27   </synopsis>
28   
29   <refsect2 id="R2-SQL-CREATETRIGGER-1">
30    <refsect2info>
31     <date>1998-09-21</date>
32    </refsect2info>
33    <title>
34     Inputs
35    </title>
36    <para>
37
38     <variablelist>
39      <varlistentry>
40       <term><replaceable class="parameter">name</replaceable></term>
41       <listitem>
42        <para>
43         The name to give the new trigger.  This must be distinct from the name
44         of any other trigger for the same table.
45        </para>
46       </listitem>
47      </varlistentry>
48      <varlistentry>
49       <term><replaceable class="parameter">event</replaceable></term>
50       <listitem>
51        <para>
52         One of INSERT, DELETE or UPDATE.
53        </para>
54       </listitem>
55      </varlistentry>
56      <varlistentry>
57       <term><replaceable class="parameter">table</replaceable></term>
58       <listitem>
59        <para>
60         The name (optionally schema-qualified) of the table the trigger is for.
61        </para>
62       </listitem>
63      </varlistentry>
64      <varlistentry>
65       <term><replaceable class="parameter">func</replaceable></term>
66       <listitem>
67        <para>
68         A user-supplied function that is declared as taking no arguments
69         and returning type <literal>trigger</>.
70        </para>
71       </listitem>
72      </varlistentry>
73      <varlistentry>
74       <term><replaceable class="parameter">arguments</replaceable></term>
75       <listitem>
76        <para>
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.
82        </para>
83       </listitem>
84      </varlistentry>
85     </variablelist>
86    </para>
87   </refsect2>
88
89   <refsect2 id="R2-SQL-CREATETRIGGER-2">
90    <refsect2info>
91     <date>1998-09-21</date>
92    </refsect2info>
93    <title>
94     Outputs
95    </title>
96    <para>
97
98     <variablelist>
99      <varlistentry>
100       <term><computeroutput>
101 CREATE TRIGGER
102        </computeroutput></term>
103       <listitem>
104        <para>
105         This message is returned if the trigger is successfully created.
106        </para>
107       </listitem>
108      </varlistentry>
109     </variablelist>
110    </para>
111   </refsect2>
112  </refsynopsisdiv>
113  
114  <refsect1 id="R1-SQL-CREATETRIGGER-1">
115   <refsect1info>
116    <date>1998-09-21</date>
117   </refsect1info>
118   <title>
119    Description
120   </title>
121
122   <para>
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>.
127   </para>
128
129   <para>
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.
142   </para>
143
144   <para>
145    If multiple triggers of the same kind are defined for the same event,
146    they will be fired in alphabetical order by name.
147   </para>
148
149   <para>
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.
153   </para>
154
155   <para>
156    Refer to the chapters on SPI and Triggers in the
157    <citetitle>PostgreSQL Programmer's Guide</citetitle>  for  more
158    information.
159   </para>
160  </refsect1>
161
162  <refsect1 id="SQL-CREATETRIGGER-notes">
163   <title>Notes</title>
164
165   <para>
166    To create a trigger on a table, the user must have the
167    <literal>TRIGGER</literal> privilege on the table.
168   </para>
169
170   <para>
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
175    type safety.
176   </para>
177
178   <para>
179    As of the current release, <literal>STATEMENT</literal> triggers are not implemented.
180   </para>
181
182   <para>
183    Refer to the <xref linkend="sql-droptrigger"> command for
184    information on how to remove triggers.
185   </para>
186  </refsect1>
187
188  <refsect1 id="R1-SQL-CREATETRIGGER-2">
189   <title>Examples</title>
190
191   <para>
192    Check if the specified distributor code exists in the distributors
193    table before appending or updating a row in the table films:
194
195 <programlisting>
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');
199 </programlisting>
200   </para>
201
202   <para>
203    Before cancelling a distributor or updating its code, remove every
204    reference to the table films:
205 <programlisting>
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');
209 </programlisting>
210   </para>
211
212   <para>
213    The second example can also be done by using a foreign key,
214    constraint as in:
215
216 <programlisting>
217 CREATE TABLE distributors (
218     did      DECIMAL(3),
219     name     VARCHAR(40),
220     CONSTRAINT if_film_exists
221     FOREIGN KEY(did) REFERENCES films
222     ON UPDATE CASCADE ON DELETE CASCADE  
223 );
224 </programlisting>
225   </para>
226  </refsect1>
227
228  <refsect1 id="SQL-CREATETRIGGER-compatibility">
229   <title>Compatibility</title>
230   
231   <variablelist>
232    <varlistentry>
233     <term>SQL92</term>
234     <listitem>
235      <para>
236       There is no <command>CREATE TRIGGER</command> statement in <acronym>SQL92</acronym>.
237      </para>
238     </listitem>
239    </varlistentry>
240
241    <varlistentry>
242     <term>SQL99</term>
243     <listitem>
244      <para>
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:
248       <itemizedlist>
249        <listitem>
250         <para>
251          SQL99 allows triggers to fire on updates to specific columns
252          (e.g., <literal>AFTER UPDATE OF col1, col2</literal>).
253         </para>
254        </listitem>
255
256        <listitem>
257         <para>
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.
267         </para>
268        </listitem>
269
270        <listitem>
271         <para>
272          <productname>PostgreSQL</productname> only has row-level
273          triggers, no statement-level triggers.
274         </para>
275        </listitem>
276
277        <listitem>
278         <para>
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.
285         </para>
286        </listitem>
287       </itemizedlist>
288      </para>
289
290      <para>
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.
294      </para>
295     </listitem>
296    </varlistentry>
297   </variablelist>
298  </refsect1>
299
300  <refsect1>
301   <title>See Also</title>
302
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>
308   </simplelist>
309  </refsect1>
310 </refentry>
311
312 <!-- Keep this comment at the end of the file
313 Local variables:
314 mode: sgml
315 sgml-omittag:nil
316 sgml-shorttag:t
317 sgml-minimize-attributes:nil
318 sgml-always-quote-attributes:t
319 sgml-indent-step:1
320 sgml-indent-data: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
326 End:
327 -->