@@ -51,7 +51,7 @@ ____________________________________________________________________________-->
51
51
<refsynopsisdiv>
52
52
<!--==========================orignal english content==========================
53
53
<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 ... ] }
55
55
ON <replaceable class="parameter">table_name</replaceable>
56
56
[ FROM <replaceable class="parameter">referenced_table_name</replaceable> ]
57
57
[ NOT DEFERRABLE | [ DEFERRABLE ] [ INITIALLY IMMEDIATE | INITIALLY DEFERRED ] ]
@@ -69,7 +69,7 @@ CREATE [ CONSTRAINT ] TRIGGER <replaceable class="parameter">name</replaceable>
69
69
</synopsis>
70
70
____________________________________________________________________________-->
71
71
<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 ... ] }
73
73
ON <replaceable class="parameter">table_name</replaceable>
74
74
[ FROM <replaceable class="parameter">referenced_table_name</replaceable> ]
75
75
[ NOT DEFERRABLE | [ DEFERRABLE ] [ INITIALLY IMMEDIATE | INITIALLY DEFERRED ] ]
@@ -95,15 +95,31 @@ ____________________________________________________________________________-->
95
95
96
96
<!--==========================orignal english content==========================
97
97
<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
99
101
trigger will be associated with the specified table, view, or foreign table
100
102
and will execute the specified
101
103
function <replaceable class="parameter">function_name</replaceable> when
102
104
certain operations are performed on that table.
103
105
</para>
104
106
____________________________________________________________________________-->
105
107
<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
+ 所有其他的属性会被替换。
107
123
</para>
108
124
109
125
<!--==========================orignal english content==========================
@@ -307,10 +323,14 @@ ____________________________________________________________________________-->
307
323
308
324
<!--==========================orignal english content==========================
309
325
<para>
326
+ <indexterm>
327
+ <primary>trigger</primary>
328
+ <secondary>constraint trigger</secondary>
329
+ </indexterm>
310
330
When the <literal>CONSTRAINT</literal> option is specified, this command creates a
311
331
<firstterm>constraint trigger</firstterm>. This is the same as a regular trigger
312
332
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 >.
314
334
Constraint triggers must be <literal>AFTER ROW</literal> triggers on plain
315
335
tables (not foreign tables). They
316
336
can be fired either at the end of the statement causing the triggering
@@ -322,7 +342,17 @@ ____________________________________________________________________________-->
322
342
</para>
323
343
____________________________________________________________________________-->
324
344
<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
+ 当约束触发器实现的约束被违背时,约束触发器应该抛出一个异常。
326
356
</para>
327
357
328
358
<!--==========================orignal english content==========================
@@ -760,22 +790,42 @@ ____________________________________________________________________________-->
760
790
761
791
<!--==========================orignal english content==========================
762
792
<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
764
794
<literal>TRIGGER</literal> privilege on the table. The user must
765
795
also have <literal>EXECUTE</literal> privilege on the trigger function.
766
796
</para>
767
797
____________________________________________________________________________-->
768
798
<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>移除一个触发器。
770
810
</para>
771
811
772
812
<!--==========================orignal english content==========================
773
813
<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.
775
822
</para>
776
823
____________________________________________________________________________-->
777
824
<para>
778
- 使用<xref linkend="sql-droptrigger"/>移除一个触发器。
825
+ 在分区表上创建行级触发器将导致在每个它的已有分区上创建相同的<quote>clone</quote>触发器;以后创建或附加的任何分区也将具有相同的触发器。
826
+ 如果子分区上已经有冲突命名的触发器,则会发生错误,除非使用<command>CREATE OR REPLACE TRIGGER</command> 。
827
+ 在这种情况下,该触发器将由一个克隆触发器替换。
828
+ 当一个分区从其父分区脱离时,它的克隆触发器将被删除。
779
829
</para>
780
830
781
831
<!--==========================orignal english content==========================
@@ -794,16 +844,6 @@ ____________________________________________________________________________-->
794
844
<para>
795
845
当一个列相关的触发器(使用<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>上的触发器,即便该列的值没有改变。
796
846
</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>
807
847
808
848
<!--==========================orignal english content==========================
809
849
<para>
@@ -860,7 +900,7 @@ ____________________________________________________________________________-->
860
900
as <literal>ON UPDATE CASCADE</literal> or <literal>ON DELETE SET NULL</literal>, are
861
901
treated as part of the SQL command that caused them (note that such
862
902
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
864
904
fire triggers not directly matching its type. In simple cases, triggers
865
905
that request transition relations will see all changes caused in their
866
906
table by a single original SQL command as a single transition relation.
@@ -895,20 +935,6 @@ ____________________________________________________________________________-->
895
935
只有当视图上的动作被一个行级<literal>INSTEAD OF</literal>触发器处理时才会引发视图上的语句级触发器。如果动作被一个<literal>INSTEAD</literal>规则处理,那么该语句发出的任何语句都会代替提及该视图的原始语句执行,这样将被引发的触发器是替换语句中提及的表上的那些触发器。类似地,如果视图是自动可更新的,则该动作将被处理为把该语句自动重写成在视图基表上的一个动作,这样基表的语句级触发器就是要被引发的。
896
936
</para>
897
937
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
-
912
938
<!--==========================orignal english content==========================
913
939
<para>
914
940
Modifying a partitioned table or a table with inheritance children fires
@@ -920,15 +946,62 @@ ____________________________________________________________________________-->
920
946
named by a <literal>REFERENCING</literal> clause, then before and after
921
947
images of rows are visible from all affected partitions or child tables.
922
948
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.
926
975
</para>
927
976
____________________________________________________________________________-->
928
977
<para>
929
- 修改分区表或者带有继承子表的表会引发挂接到显式提及表的语句级触发器,但不会引发其分区或子表的语句级触发器。相反,行级触发器会在受影响的分区或子表上引发,即便它们在查询中没有被明确提及。如果一个语句级触发器用 <literal>REFERENCING </literal>子句定义有传递关系,则来自所有受影响分区或子表中的行的前后映像都是可见的。在继承子表的情况中,行映像仅包括该触发器所附属的表中存在的列。当前,不能在分区或继承子表上定义带有传递关系的行级触发器 。
978
+ 当前, <literal>OR REPLACE </literal> 选项不支持约束触发器 。
930
979
</para>
931
980
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>
932
1005
</refsect1>
933
1006
934
1007
<refsect1 id="sql-createtrigger-examples">
@@ -949,11 +1022,12 @@ CREATE TRIGGER check_update
949
1022
EXECUTE FUNCTION check_account_update();
950
1023
</programlisting>
951
1024
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:
954
1028
955
1029
<programlisting>
956
- CREATE TRIGGER check_update
1030
+ CREATE OR REPLACE TRIGGER check_update
957
1031
BEFORE UPDATE OF balance ON accounts
958
1032
FOR EACH ROW
959
1033
EXECUTE FUNCTION check_account_update();
@@ -1026,10 +1100,10 @@ CREATE TRIGGER check_update
1026
1100
EXECUTE FUNCTION check_account_update();
1027
1101
</programlisting>
1028
1102
1029
- 下面的例子与上面一个例子相同,但是只在 <command>UPDATE</command>命令指定要更新 <literal>balance</literal>列时才执行该函数:
1103
+ 修改该触发器定义,使其仅在 <command>UPDATE</command>命令中将 <literal>balance</literal>列指定为目标时执行函数:
1030
1104
1031
1105
<programlisting>
1032
- CREATE TRIGGER check_update
1106
+ CREATE OR REPLACE TRIGGER check_update
1033
1107
BEFORE UPDATE OF balance ON accounts
1034
1108
FOR EACH ROW
1035
1109
EXECUTE FUNCTION check_account_update();
@@ -1063,8 +1137,8 @@ CREATE TRIGGER view_insert
1063
1137
FOR EACH ROW
1064
1138
EXECUTE FUNCTION view_insert_row();
1065
1139
</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>的行不会有净值增加:
1068
1142
1069
1143
<programlisting>
1070
1144
CREATE TRIGGER transfer_insert
@@ -1234,10 +1308,12 @@ ____________________________________________________________________________-->
1234
1308
<command>CREATE CONSTRAINT TRIGGER</command> is a
1235
1309
<productname>PostgreSQL</productname> extension of the <acronym>SQL</acronym>
1236
1310
standard.
1311
+ So is the <literal>OR REPLACE</literal> option.
1237
1312
</para>
1238
1313
____________________________________________________________________________-->
1239
1314
<para>
1240
1315
<command>CREATE CONSTRAINT TRIGGER</command>是<acronym>SQL</acronym>标准的一个<productname>PostgreSQL</productname>扩展。
1316
+ 所以是<literal>OR REPLACE</literal> 选项。
1241
1317
</para>
1242
1318
1243
1319
</refsect1>
0 commit comments