CREATE TRIGGER 中文man页面

系统
CREATE TRIGGER 创建一个新的触发器。 触发器将与指定表关联并且将在特定事件发生时执行声明的函数 func。

NAME

CREATE TRIGGER - 定义一个新的触发器

SYNOPSIS

CREATE TRIGGER name { BEFORE | AFTER } { event [ OR ... ] }
    ON table [ FOR [ EACH ] { ROW | STATEMENT } ]
    EXECUTE PROCEDURE funcname ( arguments )

DESCRIPTION 描述

CREATE TRIGGER 创建一个新的触发器。 触发器将与指定表关联并且将在特定事件发生时执行声明的函数 func。


 触发器可以声明为在对记录进行操作之前(在检查约束之前和 INSERT,UPDATE  或 DELETE 执行前)或操作完成之后(在检查约束之后和完成  INSERT, UPDATE 或 DELETE 操作)触发。 如果触发器在事件之前,触发器可能略过当前记录的操作或改变被插入的(当前)记录(只对 INSERT 和 UPDATE 操作有效)。 如果触发器在事件之后,所有更改,包括***的插入, 更新或删除对触发器都是"可见"的。


 一个 FOR EACH ROW 执行指定操作的触发器为操作修改的每一行都调用一次。比如,一个影响 10 行的 DELETE 将导致任何在目标关系上的 ON DELETE 触发器独立调用 10 次, 每个被删除的行一次。相比之下,一个为指定操作 FOR EACH STATEMENT  的触发器只是为任何操作执行一次,不管有多少行被修改。 (特别是,一个修改零行的操作仍然会导致任何合适的 FOR EACH STATEMENT 触发器的执行。)


 如果多个同类型的触发器为同样事件做了定义, 那么它们将按照字母顺序被激活。

SELECT 并不更改任何行,因此你不能创建 SELECT 触发器。这种场合下规则和视图更合适些。


 请参考 Part V ``Triggers'' 获取更多信息。  

PARAMETERS 参数

name

 赋予新触发器的名称。它必需和任何作用于同一表的触发器不同。
BEFORE
AFTER

 决定该函数是在事件之前还是之后调用。
event
INSERT,DELETE 或 UPDATE 其中之一。 它声明击发触发器的事件。多个事件可以用 OR 声明。
table
触发器作用的表名称(可以用模式修饰)。
FOR EACH ROW
FOR EACH STATEMENT

 这些选项声明触发器过程是否为触发器事件影响的每个行触发一次, 还是只为每条 SQL 语句触发一次。如果都没有声明, FOR EACH STATEMENT 是缺省。
func
一个用户提供的函数,它声明为不接受参数并且返回 trigger 类型。
arguments

 一个可选的用逗号分隔的参数列表,它将在触发器执行的时候提供给函数, 这些参数是文本字串常量。也可以在这里写简单的名字和数值常量,但是它们会被转换成字串。 请检查实现语言中关于如何在触发器函数中访问触发器参数的描述; 这些参数可能和普通的函数参数不同。

NOTES 注意


 要在一个表上创建一个触发器,用户必需在该表上有 TRIGGER 权限。


 在 PostgreSQL 版本 7.3 以前, 我们必须把触发器函数声明为返回占位类型 opaque, 而不是 trigger。为了支持装载老的转储文件, CREATE TRIGGER 将接受一个声明为返回 opaque 的函数, 但是它将发出一条 NOTICE 并且把函数声明的返回类型改成 trigger。


 用 DROP TRIGGER [drop_trigger(7)] 获取如何删除触发器的信息。  

EXAMPLES 例子

Section 33.4 ``Triggers'' 包含一个完整的例子。  

COMPATIBILITY 兼容性

CREATE TRIGGER 里的 CREATE TRIGGER 语句实现了一个 SQL99 标准的子集。 (SQL92 里没有触发器) 目前仍然缺少下面的功能∶

*
SQL99 允许触发器为指定的字段的更新进行触发(也就是说,AFTER UPDATE OF col1, col2)。
*
SQL99 允许你为 "old" 和 "new" 行或者表定义别名,用于定义触发器的动作(也就是说, CREATE TRIGGER ... ON tablename REFERENCING OLD ROW AS somename NEW ROW AS othername ...)。因为 PostgreSQL 允许触发器过程以任意数量的用户定义语言进行书写,所以访问数据的工作是用和语言相关的方法实现的。
*
PostgreSQL 只允许为触发的动作执行存储的过程。SQL99 允许执行一些其他的 SQL 命令, 比如那 CREATE TABLE 作为触发器动作。 这个限止并不难绕开,只要创建一个执行这些命令的存储过程即可。

SQL99 要求多个触发器应该以创建的时间顺序执行。 PostgreSQL 采用的是按照名字顺序, 我们认为这样更加方便。


 用 OR 给一个触发器声明多个动作是 PostgreSQL  对标准的扩展。  

SEE ALSO 参见

CREATE FUNCTION [create_function(7)], ALTER TRIGGER [alter_trigger(l)], DROP TRIGGER [drop_trigger(l)]  

#p#

NAME

CREATE TRIGGER - define a new trigger

SYNOPSIS

CREATE TRIGGER name { BEFORE | AFTER } { event [ OR ... ] }
    ON table [ FOR [ EACH ] { ROW | STATEMENT } ]
    EXECUTE PROCEDURE funcname ( arguments )

DESCRIPTION

CREATE TRIGGER creates a new trigger. The trigger will be associated with the specified table and will execute the specified function func when certain events occur.

The trigger can be specified to fire either before before the operation is attempted on a row (before constraints are checked and the INSERT, UPDATE, or DELETE is attempted) or after the operation has completed (after constraints are checked and the INSERT, UPDATE, or DELETE has completed). If the trigger fires before the event, the trigger may skip the operation for the current row, or change the row being inserted (for INSERT and UPDATE operations only). If the trigger fires after the event, all changes, including the last insertion, update, or deletion, are ``visible'' to the trigger.

A trigger that is marked FOR EACH ROW is called once for every row that the operation modifies. For example, a DELETE that affects 10 rows will cause any ON DELETE triggers on the target relation to be called 10 separate times, once for each deleted row. In contrast, a trigger that is marked FOR EACH STATEMENT only executes once for any given operation, regardless of how many rows it modifies (in particular, an operation that modifies zero rows will still result in the execution of any applicable FOR EACH STATEMENT triggers).

If multiple triggers of the same kind are defined for the same event, they will be fired in alphabetical order by name.

SELECT does not modify any rows so you can not create SELECT triggers. Rules and views are more appropriate in such cases.

Refer to the chapter called ``Triggers'' in the documentation for more information about triggers.  

PARAMETERS

name
The name to give the new trigger. This must be distinct from the name of any other trigger for the same table.
BEFORE
AFTER
Determines whether the function is called before or after the event.
event
One of INSERT, UPDATE, or DELETE; this specifies the event that will fire the trigger. Multiple events can be specified using OR.
table
The name (optionally schema-qualified) of the table the trigger is for.
FOR EACH ROW
FOR EACH STATEMENT
This specifies whether the trigger procedure should be fired once for every row affected by the trigger event, or just once per SQL statement. If neither is specified, FOR EACH STATEMENT is the default.
func
A user-supplied function that is declared as taking no arguments and returning type trigger, which is executed when the trigger fires.
arguments
An optional comma-separated list of arguments to be provided to the function when the trigger is executed. The arguments are literal string constants. Simple names and numeric constants may be written here, too, but they will all be converted to strings. Please check the description of the implementation language of the trigger function about how the trigger arguments are accessible within the function; it may be different from normal function arguments.

NOTES

To create a trigger on a table, the user must have the TRIGGER privilege on the table.

In PostgreSQL versions before 7.3, it was necessary to declare trigger functions as returning the placeholder type opaque, rather than trigger. To support loading of old dump files, CREATE TRIGGER will accept a function declared as returning opaque, but it will issue a notice and change the function's declared return type to trigger.

Use DROP TRIGGER [drop_trigger(7)] to remove a trigger.  

EXAMPLES

The chapter called ``Triggers'' in the documentation contains a complete example.  

COMPATIBILITY

The CREATE TRIGGER statement in PostgreSQL implements a subset of the SQL99 standard. (There are no provisions for triggers in SQL92.) The following functionality is missing:

*
SQL99 allows triggers to fire on updates to specific columns (e.g., AFTER UPDATE OF col1, col2).
*
SQL99 allows you to define aliases for the ``old'' and ``new'' rows or tables for use in the definition of the triggered action (e.g., CREATE TRIGGER ... ON tablename REFERENCING OLD ROW AS somename NEW ROW AS othername ...). Since PostgreSQL allows trigger procedures to be written in any number of user-defined languages, access to the data is handled in a language-specific way.
*
PostgreSQL only allows the execution of a user-defined function for the triggered action. SQL99 allows the execution of a number of other SQL commands, such as CREATE TABLE as triggered action. This limitation is not hard to work around by creating a user-defined function that executes the desired commands.

SQL99 specifies that multiple triggers should be fired in time-of-creation order. PostgreSQL uses name order, which was judged more convenient to work with.

The ability to specify multiple actions for a single trigger using OR is a PostgreSQL extension of the SQL standard.  

SEE ALSO

CREATE FUNCTION [create_function(7)], ALTER TRIGGER [alter_trigger(l)], DROP TRIGGER [drop_trigger(l)]

责任编辑:韩亚珊 来源: CMPP.net
相关推荐

2011-08-24 10:53:20

CREATE CONS中文man

2011-08-24 09:48:46

alter_trigg中文man

2011-08-24 14:52:31

drop_trigge中文man

2011-08-24 10:56:32

CREATE CONV中文man

2011-08-24 13:46:39

CREATE VIEW中文man

2011-08-24 13:43:09

CREATE USER中文man

2011-08-24 10:46:36

CREATE AGGR中文man

2011-08-24 13:29:20

CREATE TABL中文man

2011-08-24 11:15:24

CREATE INDE中文man

2011-08-24 13:32:56

CREATE TABL中文man

2011-08-24 11:02:11

CREATE DOMA中文man

2011-08-24 11:05:36

CREATE FUNC中文man

2011-08-24 13:39:44

CREATE TYPE中文man

2011-08-24 13:23:10

CREATE SCHE中文man

2011-08-24 10:59:19

CREATE DATA中文man

2011-08-24 11:18:53

CREATE LANG中文man

2011-08-24 11:10:17

CREATE GROU中文man

2011-08-24 11:23:20

CREATE OPER中文man

2011-08-24 11:31:47

CREATE RULE中文man

2011-08-24 13:26:19

CREATE SEQU中文man
点赞
收藏

51CTO技术栈公众号