Skip to content

Commit 1851814

Browse files
committed
Update create_trigger.sgml
1 parent e41697d commit 1851814

File tree

1 file changed

+122
-46
lines changed

1 file changed

+122
-46
lines changed

postgresql/doc/src/sgml/ref/create_trigger.sgml

Lines changed: 122 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ ____________________________________________________________________________-->
5151
<refsynopsisdiv>
5252
<!--==========================orignal english content==========================
5353
<synopsis>
54-
CREATE [ CONSTRAINT ] TRIGGER <replaceable class="parameter">name</replaceable> { BEFORE | AFTER | INSTEAD OF } { <replaceable class="parameter">event</replaceable> [ OR ... ] }
54+
CREATE [ OR REPLACE ] [ CONSTRAINT ] TRIGGER <replaceable class="parameter">name</replaceable> { BEFORE | AFTER | INSTEAD OF } { <replaceable class="parameter">event</replaceable> [ OR ... ] }
5555
ON <replaceable class="parameter">table_name</replaceable>
5656
[ FROM <replaceable class="parameter">referenced_table_name</replaceable> ]
5757
[ NOT DEFERRABLE | [ DEFERRABLE ] [ INITIALLY IMMEDIATE | INITIALLY DEFERRED ] ]
@@ -69,7 +69,7 @@ CREATE [ CONSTRAINT ] TRIGGER <replaceable class="parameter">name</replaceable>
6969
</synopsis>
7070
____________________________________________________________________________-->
7171
<synopsis>
72-
CREATE [ CONSTRAINT ] TRIGGER <replaceable class="parameter">name</replaceable> { BEFORE | AFTER | INSTEAD OF } { <replaceable class="parameter">event</replaceable> [ OR ... ] }
72+
CREATE [ OR REPLACE ] [ CONSTRAINT ] TRIGGER <replaceable class="parameter">name</replaceable> { BEFORE | AFTER | INSTEAD OF } { <replaceable class="parameter">event</replaceable> [ OR ... ] }
7373
ON <replaceable class="parameter">table_name</replaceable>
7474
[ FROM <replaceable class="parameter">referenced_table_name</replaceable> ]
7575
[ NOT DEFERRABLE | [ DEFERRABLE ] [ INITIALLY IMMEDIATE | INITIALLY DEFERRED ] ]
@@ -95,15 +95,31 @@ ____________________________________________________________________________-->
9595

9696
<!--==========================orignal english content==========================
9797
<para>
98-
<command>CREATE TRIGGER</command> creates a new trigger. The
98+
<command>CREATE TRIGGER</command> creates a new trigger.
99+
<command>CREATE OR REPLACE TRIGGER</command> will either create a
100+
new trigger, or replace an existing trigger. The
99101
trigger will be associated with the specified table, view, or foreign table
100102
and will execute the specified
101103
function <replaceable class="parameter">function_name</replaceable> when
102104
certain operations are performed on that table.
103105
</para>
104106
____________________________________________________________________________-->
105107
<para>
106-
<command>CREATE TRIGGER</command>创建一个新触发器。该触发器将被关联到指定的表、视图或者外部表并且在表上发生特定操作时将执行指定的函数<replaceable class="parameter">function_name</replaceable>。
108+
<command>CREATE TRIGGER</command>创建一个新触发器。
109+
<command>CREATE OR REPLACE TRIGGER</command> 将创建一个新触发器,或者替换一个已有的触发器。
110+
该触发器将被关联到指定的表、视图或者外部表并且在表上发生特定操作时将执行指定的函数<replaceable class="parameter">function_name</replaceable>。
111+
</para>
112+
113+
<!--==========================orignal english content==========================
114+
<para>
115+
To replace the current definition of an existing trigger, use
116+
<command>CREATE OR REPLACE TRIGGER</command>, specifying the existing
117+
trigger's name and parent table. All other properties are replaced.
118+
</para>
119+
____________________________________________________________________________-->
120+
<para>
121+
要替换已有触发器的当前定义,使用<command>CREATE OR REPLACE TRIGGER</command>,指定已有触发器的名称和父表。
122+
所有其他的属性会被替换。
107123
</para>
108124

109125
<!--==========================orignal english content==========================
@@ -307,10 +323,14 @@ ____________________________________________________________________________-->
307323

308324
<!--==========================orignal english content==========================
309325
<para>
326+
<indexterm>
327+
<primary>trigger</primary>
328+
<secondary>constraint trigger</secondary>
329+
</indexterm>
310330
When the <literal>CONSTRAINT</literal> option is specified, this command creates a
311331
<firstterm>constraint trigger</firstterm>. This is the same as a regular trigger
312332
except that the timing of the trigger firing can be adjusted using
313-
<xref linkend="sql-set-constraints"/>.
333+
<link linkend="sql-set-constraints"><command>SET CONSTRAINTS</command></link>.
314334
Constraint triggers must be <literal>AFTER ROW</literal> triggers on plain
315335
tables (not foreign tables). They
316336
can be fired either at the end of the statement causing the triggering
@@ -322,7 +342,17 @@ ____________________________________________________________________________-->
322342
</para>
323343
____________________________________________________________________________-->
324344
<para>
325-
当<literal>CONSTRAINT</literal>选项被指定,这个命令会创建一个<firstterm>约束触发器</firstterm>。这和一个常规触发器相同,不过触发该触发器的时机可以使用<xref linkend="sql-set-constraints"/>调整。约束触发器必须是表上的<literal>AFTER ROW</literal>触发器。它们可以在导致触发器事件的语句末尾被引发或者在包含该语句的事务末尾被引发。在后一种情况中,它们被称作是被<firstterm>延迟</firstterm>。一个待处理的延迟触发器的引发也可以使用<command>SET CONSTRAINTS</command>立即强制发生。当约束触发器实现的约束被违背时,约束触发器应该抛出一个异常。
345+
<indexterm>
346+
<primary>trigger</primary>
347+
<secondary>约束触发器</secondary>
348+
</indexterm>
349+
当<literal>CONSTRAINT</literal>选项被指定,这个命令会创建一个<firstterm>约束触发器</firstterm>。
350+
这和一个常规触发器相同,不过触发该触发器的时机可以使用<link linkend="sql-set-constraints"><command>SET CONSTRAINTS</command></link>.调整。
351+
约束触发器必须是表上的<literal>AFTER ROW</literal>触发器。
352+
它们可以在导致触发器事件的语句末尾被引发或者在包含该语句的事务末尾被引发。
353+
在后一种情况中,它们被称作是被<firstterm>延迟</firstterm>。
354+
一个待处理的延迟触发器的引发也可以使用<command>SET CONSTRAINTS</command>立即强制发生。
355+
当约束触发器实现的约束被违背时,约束触发器应该抛出一个异常。
326356
</para>
327357

328358
<!--==========================orignal english content==========================
@@ -760,22 +790,42 @@ ____________________________________________________________________________-->
760790

761791
<!--==========================orignal english content==========================
762792
<para>
763-
To create a trigger on a table, the user must have the
793+
To create or replace a trigger on a table, the user must have the
764794
<literal>TRIGGER</literal> privilege on the table. The user must
765795
also have <literal>EXECUTE</literal> privilege on the trigger function.
766796
</para>
767797
____________________________________________________________________________-->
768798
<para>
769-
要在一个表上创建一个触发器,用户必须具有该表上的<literal>TRIGGER</literal>特权。用户还必须具有在触发器函数上的<literal>EXECUTE</literal>特权。
799+
要在一个表上创建或替换一个触发器,用户必须具有该表上的<literal>TRIGGER</literal>特权。
800+
用户还必须具有在触发器函数上的<literal>EXECUTE</literal>特权。
801+
</para>
802+
803+
<!--==========================orignal english content==========================
804+
<para>
805+
Use <link linkend="sql-droptrigger"><command>DROP TRIGGER</command></link> to remove a trigger.
806+
</para>
807+
____________________________________________________________________________-->
808+
<para>
809+
使用<link linkend="sql-droptrigger"><command>DROP TRIGGER</command></link>移除一个触发器。
770810
</para>
771811

772812
<!--==========================orignal english content==========================
773813
<para>
774-
Use <xref linkend="sql-droptrigger"/> to remove a trigger.
814+
Creating a row-level trigger on a partitioned table will cause an
815+
identical <quote>clone</quote> trigger to be created on each of its
816+
existing partitions; and any partitions created or attached later will have
817+
an identical trigger, too. If there is a conflictingly-named trigger on a
818+
child partition already, an error occurs unless <command>CREATE OR REPLACE
819+
TRIGGER</command> is used, in which case that trigger is replaced with a
820+
clone trigger. When a partition is detached from its parent, its clone
821+
triggers are removed.
775822
</para>
776823
____________________________________________________________________________-->
777824
<para>
778-
使用<xref linkend="sql-droptrigger"/>移除一个触发器。
825+
在分区表上创建行级触发器将导致在每个它的已有分区上创建相同的<quote>clone</quote>触发器;以后创建或附加的任何分区也将具有相同的触发器。
826+
如果子分区上已经有冲突命名的触发器,则会发生错误,除非使用<command>CREATE OR REPLACE TRIGGER</command> 。
827+
在这种情况下,该触发器将由一个克隆触发器替换。
828+
当一个分区从其父分区脱离时,它的克隆触发器将被删除。
779829
</para>
780830

781831
<!--==========================orignal english content==========================
@@ -794,16 +844,6 @@ ____________________________________________________________________________-->
794844
<para>
795845
当一个列相关的触发器(使用<literal>UPDATE OF <replaceable>column_name</replaceable></literal>语法定义的触发器)的列被列为<command>UPDATE</command>命令的<literal>SET</literal>列表目标时,它会被触发。即便该触发器没有被引发,一个列的值也可能改变,因为<literal>BEFORE UPDATE</literal>触发器对行内容所作的改变不会被考虑。相反,一个诸如<literal>UPDATE ... SET x = x ...</literal>的命令将引发一个位于列<literal>x</literal>上的触发器,即便该列的值没有改变。
796846
</para>
797-
<!--==========================orignal english content==========================
798-
<para>
799-
There are a few built-in trigger functions that can be used to
800-
solve common problems without having to write your own trigger code;
801-
see <xref linkend="functions-trigger"/>.
802-
</para>
803-
____________________________________________________________________________-->
804-
<para>
805-
有一些内置的触发函数可用于解决常见问题,而无需编写您自己的触发代码。请参见<xref linkend="functions-trigger"/>。
806-
</para>
807847

808848
<!--==========================orignal english content==========================
809849
<para>
@@ -860,7 +900,7 @@ ____________________________________________________________________________-->
860900
as <literal>ON UPDATE CASCADE</literal> or <literal>ON DELETE SET NULL</literal>, are
861901
treated as part of the SQL command that caused them (note that such
862902
actions are never deferred). Relevant triggers on the affected table will
863-
be fired, so that this provides another way in which a SQL command might
903+
be fired, so that this provides another way in which an SQL command might
864904
fire triggers not directly matching its type. In simple cases, triggers
865905
that request transition relations will see all changes caused in their
866906
table by a single original SQL command as a single transition relation.
@@ -895,20 +935,6 @@ ____________________________________________________________________________-->
895935
只有当视图上的动作被一个行级<literal>INSTEAD OF</literal>触发器处理时才会引发视图上的语句级触发器。如果动作被一个<literal>INSTEAD</literal>规则处理,那么该语句发出的任何语句都会代替提及该视图的原始语句执行,这样将被引发的触发器是替换语句中提及的表上的那些触发器。类似地,如果视图是自动可更新的,则该动作将被处理为把该语句自动重写成在视图基表上的一个动作,这样基表的语句级触发器就是要被引发的。
896936
</para>
897937

898-
<!--==========================orignal english content==========================
899-
<para>
900-
Creating a row-level trigger on a partitioned table will cause identical
901-
triggers to be created in all its existing partitions; and any partitions
902-
created or attached later will contain an identical trigger, too.
903-
If the partition is detached from its parent, the trigger is removed.
904-
Triggers on partitioned tables may not be <literal>INSTEAD OF</literal>.
905-
</para>
906-
____________________________________________________________________________-->
907-
<para>
908-
在分区表上创建一个行级触发器将导致在它所有的现有分区上创建相同的触发器,并且以后创建或者挂接的任何分区也将包含一个相同的触发器。
909-
如果该分区与其父分区分离,则触发器将被删除。分区表上的触发器只能是<literal>AFTER</literal>。
910-
</para>
911-
912938
<!--==========================orignal english content==========================
913939
<para>
914940
Modifying a partitioned table or a table with inheritance children fires
@@ -920,15 +946,62 @@ ____________________________________________________________________________-->
920946
named by a <literal>REFERENCING</literal> clause, then before and after
921947
images of rows are visible from all affected partitions or child tables.
922948
In the case of inheritance children, the row images include only columns
923-
that are present in the table that the trigger is attached to. Currently,
924-
row-level triggers with transition relations cannot be defined on
925-
partitions or inheritance child tables.
949+
that are present in the table that the trigger is attached to.
950+
</para>
951+
____________________________________________________________________________-->
952+
<para>
953+
修改分区表或者带有继承子表的表会引发挂接到显式提及表的语句级触发器,但不会引发其分区或子表的语句级触发器。
954+
相反,行级触发器会在受影响的分区或子表上引发,即便它们在查询中没有被明确提及。
955+
如果一个语句级触发器用<literal>REFERENCING</literal>子句定义有传递关系,则来自所有受影响分区或子表中的行的前后映像都是可见的。
956+
在继承子表的情况中,行映像仅包括该触发器所附属的表中存在的列。
957+
</para>
958+
959+
<!--==========================orignal english content==========================
960+
<para>
961+
Currently, row-level triggers with transition relations cannot be defined
962+
on partitions or inheritance child tables. Also, triggers on partitioned
963+
tables may not be <literal>INSTEAD OF</literal>.
964+
</para>
965+
____________________________________________________________________________-->
966+
<para>
967+
当前,有事务关系的行级触发器不能再分区或继承的子表上定义。
968+
此外,分区表上的触发器不能被<literal>INSTEAD OF</literal>。
969+
</para>
970+
971+
<!--==========================orignal english content==========================
972+
<para>
973+
Currently, the <literal>OR REPLACE</literal> option is not supported for
974+
constraint triggers.
926975
</para>
927976
____________________________________________________________________________-->
928977
<para>
929-
修改分区表或者带有继承子表的表会引发挂接到显式提及表的语句级触发器,但不会引发其分区或子表的语句级触发器。相反,行级触发器会在受影响的分区或子表上引发,即便它们在查询中没有被明确提及。如果一个语句级触发器用<literal>REFERENCING</literal>子句定义有传递关系,则来自所有受影响分区或子表中的行的前后映像都是可见的。在继承子表的情况中,行映像仅包括该触发器所附属的表中存在的列。当前,不能在分区或继承子表上定义带有传递关系的行级触发器
978+
当前, <literal>OR REPLACE</literal> 选项不支持约束触发器
930979
</para>
931980

981+
<!--==========================orignal english content==========================
982+
<para>
983+
Replacing an existing trigger within a transaction that has already
984+
performed updating actions on the trigger's table is not recommended.
985+
Trigger firing decisions, or portions of firing decisions, that have
986+
already been made will not be reconsidered, so the effects could be
987+
surprising.
988+
</para>
989+
____________________________________________________________________________-->
990+
<para>
991+
不建议在已经对触发器的表执行过更新操作的事务中替换已有的触发器。
992+
触发器点火决定,或部分点火决定,已经被做出的将不会被重新考虑,所以作用可能会令人惊讶。
993+
</para>
994+
995+
<!--==========================orignal english content==========================
996+
<para>
997+
There are a few built-in trigger functions that can be used to
998+
solve common problems without having to write your own trigger code;
999+
see <xref linkend="functions-trigger"/>.
1000+
</para>
1001+
____________________________________________________________________________-->
1002+
<para>
1003+
有少量内置触发器函数可以用于解决常见问题而不用写你自己的触发器代码;参见<xref linkend="functions-trigger"/>。
1004+
</para>
9321005
</refsect1>
9331006

9341007
<refsect1 id="sql-createtrigger-examples">
@@ -949,11 +1022,12 @@ CREATE TRIGGER check_update
9491022
EXECUTE FUNCTION check_account_update();
9501023
</programlisting>
9511024

952-
The same, but only execute the function if column <literal>balance</literal>
953-
is specified as a target in the <command>UPDATE</command> command:
1025+
Modify that trigger definition to only execute the function if
1026+
column <literal>balance</literal> is specified as a target in
1027+
the <command>UPDATE</command> command:
9541028

9551029
<programlisting>
956-
CREATE TRIGGER check_update
1030+
CREATE OR REPLACE TRIGGER check_update
9571031
BEFORE UPDATE OF balance ON accounts
9581032
FOR EACH ROW
9591033
EXECUTE FUNCTION check_account_update();
@@ -1026,10 +1100,10 @@ CREATE TRIGGER check_update
10261100
EXECUTE FUNCTION check_account_update();
10271101
</programlisting>
10281102

1029-
下面的例子与上面一个例子相同,但是只在<command>UPDATE</command>命令指定要更新<literal>balance</literal>列时才执行该函数:
1103+
修改该触发器定义,使其仅在<command>UPDATE</command>命令中将<literal>balance</literal>列指定为目标时执行函数:
10301104

10311105
<programlisting>
1032-
CREATE TRIGGER check_update
1106+
CREATE OR REPLACE TRIGGER check_update
10331107
BEFORE UPDATE OF balance ON accounts
10341108
FOR EACH ROW
10351109
EXECUTE FUNCTION check_account_update();
@@ -1063,8 +1137,8 @@ CREATE TRIGGER view_insert
10631137
FOR EACH ROW
10641138
EXECUTE FUNCTION view_insert_row();
10651139
</programlisting>
1066-
1067-
为每个语句执行函数<function>check_transfer_balances_to_zero</function>以确认<literal>transfer</literal>的行不会有净值增加:
1140+
1141+
为每个语句执行函数<function>check_transfer_balances_to_zero</function>以确认<literal>transfer</literal>的行不会有净值增加:
10681142

10691143
<programlisting>
10701144
CREATE TRIGGER transfer_insert
@@ -1234,10 +1308,12 @@ ____________________________________________________________________________-->
12341308
<command>CREATE CONSTRAINT TRIGGER</command> is a
12351309
<productname>PostgreSQL</productname> extension of the <acronym>SQL</acronym>
12361310
standard.
1311+
So is the <literal>OR REPLACE</literal> option.
12371312
</para>
12381313
____________________________________________________________________________-->
12391314
<para>
12401315
<command>CREATE CONSTRAINT TRIGGER</command>是<acronym>SQL</acronym>标准的一个<productname>PostgreSQL</productname>扩展。
1316+
所以是<literal>OR REPLACE</literal> 选项。
12411317
</para>
12421318

12431319
</refsect1>

0 commit comments

Comments
 (0)