create_trigger.sgml 6.19 KB
Newer Older
1
<!--
2
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_trigger.sgml,v 1.10 2000/04/07 19:20:48 momjian Exp $
3 4 5
Postgres documentation
-->

6 7
<refentry id="SQL-CREATETRIGGER">
 <refmeta>
8
  <refentrytitle id="SQL-CREATETRIGGER-TITLE">
9
   CREATE TRIGGER
10 11 12 13 14
  </refentrytitle>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>
 <refnamediv>
  <refname>
15
   CREATE TRIGGER
16 17
  </refname>
  <refpurpose>
18
   Creates a new trigger
19
  </refpurpose>
20
 </refnamediv>
21 22
 <refsynopsisdiv>
  <refsynopsisdivinfo>
23
   <date>2000-03-25</date>
24 25 26 27
  </refsynopsisdivinfo>
  <synopsis>
CREATE TRIGGER <replaceable class="PARAMETER">name</replaceable> { BEFORE | AFTER } { <replaceable class="PARAMETER">event</replaceable> [OR ...] }
    ON <replaceable class="PARAMETER">table</replaceable> FOR EACH { ROW | STATEMENT }
28
    EXECUTE PROCEDURE <replaceable class="PARAMETER">func</replaceable> ( <replaceable class="PARAMETER">arguments</replaceable> )
29
  </synopsis>
30
  
31 32 33 34 35
  <refsect2 id="R2-SQL-CREATETRIGGER-1">
   <refsect2info>
    <date>1998-09-21</date>
   </refsect2info>
   <title>
36
    Inputs
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
   </title>
   <para>

    <variablelist>
     <varlistentry>
      <term><replaceable class="parameter">name</replaceable></term>
      <listitem>
       <para>
	The name of an existing trigger.
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><replaceable class="parameter">table</replaceable></term>
      <listitem>
       <para>
	The name of a table.
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><replaceable class="parameter">event</replaceable></term>
      <listitem>
       <para>
	One of INSERT, DELETE or UPDATE.
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><replaceable class="parameter">funcname</replaceable></term>
      <listitem>
       <para>
	A user-supplied function.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>
  </refsect2>

  <refsect2 id="R2-SQL-CREATETRIGGER-2">
   <refsect2info>
    <date>1998-09-21</date>
   </refsect2info>
   <title>
82
    Outputs
83 84 85 86 87 88 89 90 91 92 93
   </title>
   <para>

    <variablelist>
     <varlistentry>
      <term><computeroutput>
CREATE
       </computeroutput></term>
      <listitem>
       <para>
	This message is returned if the trigger is successfully created.
94 95 96
       </para>
      </listitem>
     </varlistentry>
97
    </variablelist>
98
   </para>
99 100
  </refsect2>
 </refsynopsisdiv>
101
 
102 103 104 105 106
 <refsect1 id="R1-SQL-CREATETRIGGER-1">
  <refsect1info>
   <date>1998-09-21</date>
  </refsect1info>
  <title>
107
   Description
108
  </title>
109

110
  <para>
111
   <command>CREATE TRIGGER</command> will enter a new trigger into the current
112 113 114
   data base.  The trigger will be associated with the relation
   <replaceable class="parameter">relname</replaceable> and will execute
   the specified function <replaceable class="parameter">funcname</replaceable>.
115
  </para>
116

117
  <para>
118
   The trigger can be specified to  fire  either  before BEFORE the
119
   operation is attempted on a tuple (before constraints
120 121 122 123 124 125
   are checked and the <command>INSERT</command>, <command>UPDATE</command> or
   <command>DELETE</command> is attempted)  or
   AFTER  the  operation  has been attempted (e.g. after constraints
   are checked and the <command>INSERT</command>,
   <command>UPDATE</command> or <command>DELETE</command> has
   completed). If the
126 127
   trigger fires before the event, the trigger may
   skip the operation for the current tuple, or change the tuple
128 129
   being  inserted  (for  <command>INSERT</command> and
   <command>UPDATE</command> operations only).  If
130
   the trigger fires after the event,  all  changes,  including  the
131
   last insertion, update, or deletion, are "visible" to the trigger.
132
  </para>
133

134
  <para>
135
   Refer to the chapters on SPI and Triggers in the
136
   <citetitle>PostgreSQL Programmer's Guide</citetitle>  for  more
137
   information.
138
  </para>
139

140 141 142 143 144
  <refsect2 id="R2-SQL-CREATETRIGGER-3">
   <refsect2info>
    <date>1998-09-21</date>
   </refsect2info>
   <title>
145
    Notes
146 147
   </title>
   <para>
148
    <command>CREATE TRIGGER</command> is a <productname>Postgres</productname>
149
    language extension.
150 151 152 153 154
   </para>
   <para>
    Only the relation owner may create a trigger on this relation.
   </para>
   <para>
155
    As of the current release (v7.0), STATEMENT triggers are not implemented.
156 157
   </para>
   <para>
158
    Refer to <command>DROP TRIGGER</command> for information on how to 
159
    remove triggers.
160 161
   </para>   
  </refsect2>
162 163
 </refsect1>

164 165
 <refsect1 id="R1-SQL-CREATETRIGGER-2">
  <title>
166
   Usage
167 168
  </title>
  <para>
169 170
   Check if the specified distributor code exists in the distributors
   table before appending or updating a row in the table films:
171 172 173 174 175 176 177 178

   <programlisting>
CREATE TRIGGER if_dist_exists
    BEFORE INSERT OR UPDATE ON films FOR EACH ROW
    EXECUTE PROCEDURE check_primary_key ('did', 'distributors', 'did');
   </programlisting>
  </para>
  <para>
179 180
   Before cancelling a distributor or updating its code, remove every
   reference to the table films:
181 182 183 184 185 186 187 188 189 190
   <programlisting>
CREATE TRIGGER if_film_exists 
    BEFORE DELETE OR UPDATE ON distributors FOR EACH ROW
    EXECUTE PROCEDURE check_foreign_key (1, 'CASCADE', 'did', 'films', 'did');
   </programlisting>
  </para>
 </refsect1>

 <refsect1 id="R1-SQL-CREATETRIGGER-3">
  <title>
191
   Compatibility
192
  </title>
193
  
194 195 196 197 198
  <refsect2 id="R2-SQL-CREATETRIGGER-4">
   <refsect2info>
    <date>1998-09-21</date>
   </refsect2info>
   <title>
199
    SQL92
200
   </title>
201

202
   <para>
203
    There is no <command>CREATE TRIGGER</command> in <acronym>SQL92</acronym>.
204 205 206
   </para>

   <para>
207 208
    The second example above may also be done by using a FOREIGN KEY
    constraint as in:
209 210 211

    <programlisting>
CREATE TABLE distributors (
212 213 214
    did      DECIMAL(3),
    name     VARCHAR(40),
    CONSTRAINT if_film_exists
215 216
    FOREIGN KEY(did) REFERENCES films
    ON UPDATE CASCADE ON DELETE CASCADE  
217 218 219
);
    </programlisting>
   </para>
220 221
  </refsect2>
 </refsect1>
222
</refentry>
223 224 225 226

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
227
sgml-omittag:nil
228 229 230 231 232 233 234 235 236 237 238
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/catalog"
sgml-local-ecat-files:nil
End:
239
-->