|
1 | 1 | <!-- |
2 | | -$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_trigger.sgml,v 1.16 2001/09/1315:55:24 petere Exp $ |
| 2 | +$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_trigger.sgml,v 1.17 2001/09/1318:17:44 petere Exp $ |
3 | 3 | Postgres documentation |
4 | 4 | --> |
5 | 5 |
|
6 | 6 | <refentry id="SQL-CREATETRIGGER"> |
| 7 | + <docinfo> |
| 8 | + <date>2001-09-13</date> |
| 9 | + </docinfo> |
| 10 | + |
7 | 11 | <refmeta> |
8 | 12 | <refentrytitle id="SQL-CREATETRIGGER-TITLE">CREATE TRIGGER</refentrytitle> |
9 | 13 | <refmiscinfo>SQL - Language Statements</refmiscinfo> |
@@ -134,88 +138,140 @@ CREATE |
134 | 138 | <citetitle>PostgreSQL Programmer's Guide</citetitle> for more |
135 | 139 | information. |
136 | 140 | </para> |
| 141 | + </refsect1> |
137 | 142 |
|
138 | | - <refsect2 id="R2-SQL-CREATETRIGGER-3"> |
139 | | - <refsect2info> |
140 | | - <date>1998-09-21</date> |
141 | | - </refsect2info> |
142 | | - <title> |
143 | | - Notes |
144 | | - </title> |
145 | | - <para> |
146 | | - <command>CREATE TRIGGER</command> is a <productname>Postgres</productname> |
147 | | - language extension. |
148 | | - </para> |
149 | | - <para> |
150 | | - Only the relation owner may create a trigger on this relation. |
151 | | - </para> |
152 | | - <para> |
153 | | - As of the current release, STATEMENT triggers are not implemented. |
154 | | - </para> |
155 | | - <para> |
156 | | - Refer to <command>DROP TRIGGER</command> for information on how to |
157 | | - remove triggers. |
158 | | - </para> |
159 | | - </refsect2> |
| 143 | + <refsect1 id="SQL-CREATETRIGGER-notes"> |
| 144 | + <title>Notes</title> |
| 145 | + |
| 146 | + <para> |
| 147 | + To create a trigger on a table, the user must have the |
| 148 | + <literal>TRIGGER</literal> privilege on the table. |
| 149 | + </para> |
| 150 | + |
| 151 | + <para> |
| 152 | + As of the current release, <literal>STATEMENT</literal> triggers are not implemented. |
| 153 | + </para> |
| 154 | + |
| 155 | + <para> |
| 156 | + Refer to the <xref linkend="sql-droptrigger"> command for |
| 157 | + information on how to remove triggers. |
| 158 | + </para> |
160 | 159 | </refsect1> |
161 | 160 |
|
162 | 161 | <refsect1 id="R1-SQL-CREATETRIGGER-2"> |
163 | | - <title> |
164 | | - Usage |
165 | | - </title> |
| 162 | + <title>Examples</title> |
| 163 | + |
166 | 164 | <para> |
167 | 165 | Check if the specified distributor code exists in the distributors |
168 | 166 | table before appending or updating a row in the table films: |
169 | 167 |
|
170 | | -<programlisting> |
| 168 | +<programlisting> |
171 | 169 | CREATE TRIGGER if_dist_exists |
172 | 170 | BEFORE INSERT OR UPDATE ON films FOR EACH ROW |
173 | 171 | EXECUTE PROCEDURE check_primary_key ('did', 'distributors', 'did'); |
174 | | -</programlisting> |
| 172 | +</programlisting> |
175 | 173 | </para> |
| 174 | + |
176 | 175 | <para> |
177 | 176 | Before cancelling a distributor or updating its code, remove every |
178 | 177 | reference to the table films: |
179 | | -<programlisting> |
| 178 | +<programlisting> |
180 | 179 | CREATE TRIGGER if_film_exists |
181 | 180 | BEFORE DELETE OR UPDATE ON distributors FOR EACH ROW |
182 | 181 | EXECUTE PROCEDURE check_foreign_key (1, 'CASCADE', 'did', 'films', 'did'); |
183 | | -</programlisting> |
| 182 | +</programlisting> |
184 | 183 | </para> |
185 | | - </refsect1> |
186 | | - |
187 | | - <refsect1 id="R1-SQL-CREATETRIGGER-3"> |
188 | | - <title> |
189 | | - Compatibility |
190 | | - </title> |
191 | | - |
192 | | - <refsect2 id="R2-SQL-CREATETRIGGER-4"> |
193 | | - <refsect2info> |
194 | | - <date>1998-09-21</date> |
195 | | - </refsect2info> |
196 | | - <title> |
197 | | - SQL92 |
198 | | - </title> |
199 | 184 |
|
200 | | - <para> |
201 | | - There is no <command>CREATE TRIGGER</command> in <acronym>SQL92</acronym>. |
202 | | - </para> |
203 | | - |
204 | | - <para> |
205 | | - The second example above may also be done by using a FOREIGN KEY |
206 | | - constraint as in: |
| 185 | + <para> |
| 186 | + The second example can also be done by using a foreign key, |
| 187 | + constraint as in: |
207 | 188 |
|
208 | | -<programlisting> |
| 189 | +<programlisting> |
209 | 190 | CREATE TABLE distributors ( |
210 | 191 | did DECIMAL(3), |
211 | 192 | name VARCHAR(40), |
212 | 193 | CONSTRAINT if_film_exists |
213 | 194 | FOREIGN KEY(did) REFERENCES films |
214 | 195 | ON UPDATE CASCADE ON DELETE CASCADE |
215 | 196 | ); |
216 | | - </programlisting> |
217 | | - </para> |
218 | | - </refsect2> |
| 197 | +</programlisting> |
| 198 | + </para> |
| 199 | + </refsect1> |
| 200 | + |
| 201 | + <refsect1 id="SQL-CREATETRIGGER-compatibility"> |
| 202 | + <title>Compatibility</title> |
| 203 | + |
| 204 | + <variablelist> |
| 205 | + <varlistentry> |
| 206 | + <term>SQL92</term> |
| 207 | + <listitem> |
| 208 | + <para> |
| 209 | + There is no <command>CREATE TRIGGER</command> statement in <acronym>SQL92</acronym>. |
| 210 | + </para> |
| 211 | + </listitem> |
| 212 | + </varlistentry> |
| 213 | + |
| 214 | + <varlistentry> |
| 215 | + <term>SQL99</term> |
| 216 | + <listitem> |
| 217 | + <para> |
| 218 | + The <command>CREATE TRIGGER</command> statement in |
| 219 | + <productname>PostgreSQL</productname> implements a subset of the |
| 220 | + SQL99 standard. The following functionality is missing: |
| 221 | + <itemizedlist> |
| 222 | + <listitem> |
| 223 | + <para> |
| 224 | + SQL99 allows triggers to fire on updates to specific columns |
| 225 | + (e.g., <literal>AFTER UPDATE OF col1, col2</literal>). |
| 226 | + </para> |
| 227 | + </listitem> |
| 228 | + |
| 229 | + <listitem> |
| 230 | + <para> |
| 231 | + SQL99 allows you to define aliases for the <quote>old</quote> |
| 232 | + and <quote>new</quote> rows or tables for use in the definiton |
| 233 | + of the triggered action (e.g., <literal>CREATE TRIGGER ... ON |
| 234 | + tablename REFERENCING OLD ROW AS somename NEW ROW AS |
| 235 | + othername ...</literal>). Since |
| 236 | + <productname>PostgreSQL</productname> allows trigger |
| 237 | + procedures to be written in any number of user-defined |
| 238 | + languages, access to the data is handled in a |
| 239 | + language-specific way. |
| 240 | + </para> |
| 241 | + </listitem> |
| 242 | + |
| 243 | + <listitem> |
| 244 | + <para> |
| 245 | + <productname>PostgreSQL</productname> only has row-level |
| 246 | + triggers, no statement-level triggers. |
| 247 | + </para> |
| 248 | + </listitem> |
| 249 | + |
| 250 | + <listitem> |
| 251 | + <para> |
| 252 | + <productname>PostgreSQL</productname> only allows the |
| 253 | + execution of a stored procedure for the triggered action. |
| 254 | + SQL99 allows the execution of a number of other SQL commands, |
| 255 | + such as <command>CREATE TABLE</command> as triggered action. |
| 256 | + This limitation is not hard to work around by creating a |
| 257 | + stored procedure that executes these commands. |
| 258 | + </para> |
| 259 | + </listitem> |
| 260 | + </itemizedlist> |
| 261 | + </para> |
| 262 | + </listitem> |
| 263 | + </varlistentry> |
| 264 | + </variablelist> |
| 265 | + </refsect1> |
| 266 | + |
| 267 | + <refsect1> |
| 268 | + <title>See Also</title> |
| 269 | + |
| 270 | + <simplelist type="inline"> |
| 271 | + <member><xref linkend="sql-createfunction"></member> |
| 272 | + <member><xref linkend="sql-droptrigger"></member> |
| 273 | + <member><citetitle>PostgreSQL Programmer's Guide</citetitle></member> |
| 274 | + </simplelist> |
219 | 275 | </refsect1> |
220 | 276 | </refentry> |
221 | 277 |
|
|