CREATE TRIGGER 中文man頁面
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)]