create_rule.7

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

7
183
字号
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "CREATE RULE" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMECREATE RULE \- define a new rewrite rule.SH SYNOPSIS.sp.nfCREATE [ OR REPLACE ] RULE \fIname\fR AS ON \fIevent\fR    TO \fItable\fR [ WHERE \fIcondition\fR ]    DO [ ALSO | INSTEAD ] { NOTHING | \fIcommand\fR | ( \fIcommand\fR ; \fIcommand\fR ... ) }.sp.fi.SH "DESCRIPTION".PP\fBCREATE RULE\fR defines a new rule applying to a specifiedtable or view.\fBCREATE OR REPLACE RULE\fR will either create anew rule, or replace an existing rule of the same name for the sametable..PPThe PostgreSQL rule system allows one todefine an alternate action to be performed on insertions, updates,or deletions in database tables. Roughly speaking, a rule causesadditional commands to be executed when a given command on a giventable is executed. Alternatively, an INSTEADrule can replace a given command by another, or cause a commandnot to be executed at all. Rules are used to implement tableviews as well. It is important to realize that a rule is reallya command transformation mechanism, or command macro. Thetransformation happens before the execution of the commands starts.If you actually want an operation that fires independently for eachphysical row, you probably want to use a trigger, not a rule.More information about the rules system is in in the documentation..PPPresently, ON SELECT rules must be unconditionalINSTEAD rules and must have actions that consistof a single \fBSELECT\fR command. Thus, anON SELECT rule effectively turns the table intoa view, whose visible contents are the rows returned by the rule's\fBSELECT\fR command rather than whatever had beenstored in the table (if anything). It is considered better styleto write a \fBCREATE VIEW\fR command than to create areal table and define an ON SELECT rule for it..PPYou can create the illusion of an updatable view by definingON INSERT, ON UPDATE, andON DELETE rules (or any subset of those that'ssufficient for your purposes) to replace update actions on the viewwith appropriate updates on other tables. If you want to support\fBINSERT RETURNING\fR and so on, then be sure to put a suitableRETURNING clause into each of these rules..PPThere is a catch if you try to use conditional rules for viewupdates: there \fBmust\fR be an unconditionalINSTEAD rule for each action you wish to allowon the view. If the rule is conditional, or is notINSTEAD, then the system will still rejectattempts to perform the update action, because it thinks it mightend up trying to perform the action on the dummy table of the viewin some cases. If you want to handle all the useful cases inconditional rules, add an unconditional DOINSTEAD NOTHING rule to ensure that the systemunderstands it will never be called on to update the dummy table.Then make the conditional rules non-INSTEAD; inthe cases where they are applied, they add to the defaultINSTEAD NOTHING action. (This method does notcurrently work to support RETURNING queries, however.).SH "PARAMETERS".TP\fB\fIname\fB\fRThe name of a rule to create. This must be distinct from thename of any other rule for the same table. Multiple rules onthe same table and same event type are applied in alphabeticalname order..TP\fB\fIevent\fB\fRThe event is one of SELECT,INSERT, UPDATE, orDELETE..TP\fB\fItable\fB\fRThe name (optionally schema-qualified) of the table or view therule applies to..TP\fB\fIcondition\fB\fRAny SQL conditional expression (returning\fBboolean\fR). The condition expression may not referto any tables except NEW and OLD, andmay not contain aggregate functions..TP\fBINSTEAD\fRINSTEAD indicates that the commands should beexecuted \fBinstead of\fR the original command..TP\fBALSO\fRALSO indicates that the commands should beexecuted \fBin addition to\fR the originalcommand.If neither ALSO norINSTEAD is specified, ALSOis the default..TP\fB\fIcommand\fB\fRThe command or commands that make up the rule action. Validcommands are \fBSELECT\fR,\fBINSERT\fR, \fBUPDATE\fR,\fBDELETE\fR, or \fBNOTIFY\fR..PPWithin \fIcondition\fR and\fIcommand\fR, the specialtable names NEW and OLD maybe used to refer to values in the referenced table.NEW is valid in ON INSERT andON UPDATE rules to refer to the new row beinginserted or updated. OLD is valid inON UPDATE and ON DELETE rulesto refer to the existing row being updated or deleted..PP.SH "NOTES".PPYou must be the owner of a table to create or change rules for it..PPIn a rule for INSERT, UPDATE, orDELETE on a view, you can add a RETURNINGclause that emits the view's columns. This clause will be used to computethe outputs if the rule is triggered by an \fBINSERT RETURNING\fR,\fBUPDATE RETURNING\fR, or \fBDELETE RETURNING\fR commandrespectively. When the rule is triggered by a command withoutRETURNING, the rule's RETURNING clause will beignored. The current implementation allows only unconditionalINSTEAD rules to contain RETURNING; furthermorethere can be at most one RETURNING clause among all the rulesfor the same event. (This ensures that there is only one candidateRETURNING clause to be used to compute the results.)RETURNING queries on the view will be rejected ifthere is no RETURNING clause in any available rule..PPIt is very important to take care to avoid circular rules. Forexample, though each of the following two rule definitions areaccepted by PostgreSQL, the\fBSELECT\fR command would causePostgreSQL to report an error becauseof recursive expansion of a rule:.sp.nfCREATE RULE "_RETURN" AS    ON SELECT TO t1    DO INSTEAD         SELECT * FROM t2;CREATE RULE "_RETURN" AS    ON SELECT TO t2    DO INSTEAD         SELECT * FROM t1;SELECT * FROM t1;.sp.fi.PPPresently, if a rule action contains a \fBNOTIFY\fRcommand, the \fBNOTIFY\fR command will be executedunconditionally, that is, the \fBNOTIFY\fR will beissued even if there are not any rows that the rule should applyto. For example, in.sp.nfCREATE RULE notify_me AS ON UPDATE TO mytable DO ALSO NOTIFY mytable;UPDATE mytable SET name = 'foo' WHERE id = 42;.sp.fione \fBNOTIFY\fR event will be sent during the\fBUPDATE\fR, whether or not there are any rows thatmatch the condition id = 42. This is animplementation restriction that may be fixed in future releases..SH "COMPATIBILITY".PP\fBCREATE RULE\fR is aPostgreSQL language extension, as is theentire query rewrite system.

⌨️ 快捷键说明

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