create_trigger.7

来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 160 行

7
160
字号
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "CREATE TRIGGER" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMECREATE TRIGGER \- define a new trigger.SH SYNOPSIS.sp.nfCREATE TRIGGER \fIname\fR { BEFORE | AFTER } { \fIevent\fR [ OR ... ] }    ON \fItable\fR [ FOR [ EACH ] { ROW | STATEMENT } ]    EXECUTE PROCEDURE \fIfuncname\fR ( \fIarguments\fR ).sp.fi.SH "DESCRIPTION".PP\fBCREATE TRIGGER\fR creates a new trigger. Thetrigger will be associated with the specified table and willexecute the specified function \fIfuncname\fR when certain events occur..PPThe trigger can be specified to fire either before theoperation is attempted on a row (before constraints are checked andthe \fBINSERT\fR, \fBUPDATE\fR, or\fBDELETE\fR is attempted) or after the operation hascompleted (after constraints are checked and the\fBINSERT\fR, \fBUPDATE\fR, or\fBDELETE\fR has completed). If the trigger firesbefore the event, the trigger may skip the operation for thecurrent row, or change the row being inserted (for\fBINSERT\fR and \fBUPDATE\fR operationsonly). If the trigger fires after the event, all changes, includingthe last insertion, update, or deletion, are ``visible''to the trigger..PPA trigger that is marked FOR EACH ROW is calledonce for every row that the operation modifies. For example, a\fBDELETE\fR that affects 10 rows will cause anyON DELETE triggers on the target relation to becalled 10 separate times, once for each deleted row. In contrast, atrigger that is marked FOR EACH STATEMENT onlyexecutes once for any given operation, regardless of how many rowsit modifies (in particular, an operation that modifies zero rowswill still result in the execution of any applicable FOREACH STATEMENT triggers)..PPIf multiple triggers of the same kind are defined for the same event,they will be fired in alphabetical order by name..PP\fBSELECT\fR does not modify any rows so you can notcreate \fBSELECT\fR triggers. Rules and views are moreappropriate in such cases..PPRefer to in the documentation for more information about triggers..SH "PARAMETERS".TP\fB\fIname\fB\fRThe name to give the new trigger. This must be distinct fromthe name of any other trigger for the same table..TP\fBBEFORE\fR.TP\fBAFTER\fRDetermines whether the function is called before or after theevent..TP\fB\fIevent\fB\fROne of \fBINSERT\fR, \fBUPDATE\fR, or\fBDELETE\fR; this specifies the event that willfire the trigger. Multiple events can be specified usingOR..TP\fB\fItable\fB\fRThe name (optionally schema-qualified) of the table the triggeris for..TP\fBFOR EACH ROW\fR.TP\fBFOR EACH STATEMENT\fRThis specifies whether the trigger procedure should be firedonce for every row affected by the trigger event, or just onceper SQL statement. If neither is specified, FOR EACHSTATEMENT is the default..TP\fB\fIfuncname\fB\fRA user-supplied function that is declared as taking no argumentsand returning type trigger, which is executed whenthe trigger fires..TP\fB\fIarguments\fB\fRAn optional comma-separated list of arguments to be provided tothe function when the trigger is executed. The arguments areliteral string constants. Simple names and numeric constantsmay be written here, too, but they will all be converted tostrings. Please check the description of the implementationlanguage of the trigger function about how the trigger argumentsare accessible within the function; it may be different fromnormal function arguments..SH "NOTES".PPTo create a trigger on a table, the user must have theTRIGGER privilege on the table..PPIn PostgreSQL versions before 7.3, it wasnecessary to declare trigger functions as returning the placeholdertype \fBopaque\fR, rather than \fBtrigger\fR. To support loadingof old dump files, \fBCREATE TRIGGER\fR will accept a functiondeclared as returning \fBopaque\fR, but it will issue a notice andchange the function's declared return type to \fBtrigger\fR..PPUse DROP TRIGGER [\fBdrop_trigger\fR(7)] to remove a trigger..SH "EXAMPLES".PPin the documentation contains a complete example..SH "COMPATIBILITY".PPThe \fBCREATE TRIGGER\fR statement inPostgreSQL implements a subset of theSQL standard. The following functionality is currently missing:.TP 0.2i\(buSQL allows triggers to fire on updates to specific columns(e.g., AFTER UPDATE OF col1, col2)..TP 0.2i\(buSQL allows you to define aliases for the ``old''and ``new'' rows or tables for use in the definitionof the triggered action (e.g., CREATE TRIGGER ... ONtablename REFERENCING OLD ROW AS somename NEW ROW AS othername\&...). Since PostgreSQLallows trigger procedures to be written in any number ofuser-defined languages, access to the data is handled in alanguage-specific way..TP 0.2i\(buPostgreSQL only allows the executionof a user-defined function for the triggered action. The standardallows the execution of a number of other SQL commands, such as\fBCREATE TABLE\fR as the triggered action. Thislimitation is not hard to work around by creating a user-definedfunction that executes the desired commands..PP.PPSQL specifies that multiple triggers should be fired intime-of-creation order. PostgreSQL usesname order, which was judged to be more convenient..PPSQL specifies that BEFORE DELETE triggers on cascadeddeletes fire \fBafter\fR the cascaded DELETE completes.The PostgreSQL behavior is for BEFOREDELETE to always fire before the delete action, even a cascadingone. This is considered more consistent. There is also unpredictablebehavior when BEFORE triggers modify rows that are laterto be modified by referential actions. This can lead to constraint violationsor stored data that does not honor the referential constraint..PPThe ability to specify multiple actions for a single trigger usingOR is a PostgreSQL extension ofthe SQL standard..SH "SEE ALSO"CREATE FUNCTION [\fBcreate_function\fR(7)], ALTER TRIGGER [\fBalter_trigger\fR(l)], DROP TRIGGER [\fBdrop_trigger\fR(l)]

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?