gooderp18绿色标准版
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

174 lines
14KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>CREATE RULE</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="sql-createrole.html" title="CREATE ROLE" /><link rel="next" href="sql-createschema.html" title="CREATE SCHEMA" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">CREATE RULE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createrole.html" title="CREATE ROLE">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-createschema.html" title="CREATE SCHEMA">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATERULE"><div class="titlepage"></div><a id="id-1.9.3.79.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE RULE</span></h2><p>CREATE RULE — define a new rewrite rule</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CREATE [ OR REPLACE ] RULE <em class="replaceable"><code>name</code></em> AS ON <em class="replaceable"><code>event</code></em>
  4. TO <em class="replaceable"><code>table_name</code></em> [ WHERE <em class="replaceable"><code>condition</code></em> ]
  5. DO [ ALSO | INSTEAD ] { NOTHING | <em class="replaceable"><code>command</code></em> | ( <em class="replaceable"><code>command</code></em> ; <em class="replaceable"><code>command</code></em> ... ) }
  6. <span class="phrase">where <em class="replaceable"><code>event</code></em> can be one of:</span>
  7. SELECT | INSERT | UPDATE | DELETE
  8. </pre></div><div class="refsect1" id="id-1.9.3.79.5"><h2>Description</h2><p>
  9. <code class="command">CREATE RULE</code> defines a new rule applying to a specified
  10. table or view.
  11. <code class="command">CREATE OR REPLACE RULE</code> will either create a
  12. new rule, or replace an existing rule of the same name for the same
  13. table.
  14. </p><p>
  15. The <span class="productname">PostgreSQL</span> rule system allows one to
  16. define an alternative action to be performed on insertions, updates,
  17. or deletions in database tables. Roughly speaking, a rule causes
  18. additional commands to be executed when a given command on a given
  19. table is executed. Alternatively, an <code class="literal">INSTEAD</code>
  20. rule can replace a given command by another, or cause a command
  21. not to be executed at all. Rules are used to implement SQL
  22. views as well. It is important to realize that a rule is really
  23. a command transformation mechanism, or command macro. The
  24. transformation happens before the execution of the command starts.
  25. If you actually want an operation that fires independently for each
  26. physical row, you probably want to use a trigger, not a rule.
  27. More information about the rules system is in <a class="xref" href="rules.html" title="Chapter 40. The Rule System">Chapter 40</a>.
  28. </p><p>
  29. Presently, <code class="literal">ON SELECT</code> rules must be unconditional
  30. <code class="literal">INSTEAD</code> rules and must have actions that consist
  31. of a single <code class="command">SELECT</code> command. Thus, an
  32. <code class="literal">ON SELECT</code> rule effectively turns the table into
  33. a view, whose visible contents are the rows returned by the rule's
  34. <code class="command">SELECT</code> command rather than whatever had been
  35. stored in the table (if anything). It is considered better style
  36. to write a <code class="command">CREATE VIEW</code> command than to create a
  37. real table and define an <code class="literal">ON SELECT</code> rule for it.
  38. </p><p>
  39. You can create the illusion of an updatable view by defining
  40. <code class="literal">ON INSERT</code>, <code class="literal">ON UPDATE</code>, and
  41. <code class="literal">ON DELETE</code> rules (or any subset of those that's
  42. sufficient for your purposes) to replace update actions on the view
  43. with appropriate updates on other tables. If you want to support
  44. <code class="command">INSERT RETURNING</code> and so on, then be sure to put a suitable
  45. <code class="literal">RETURNING</code> clause into each of these rules.
  46. </p><p>
  47. There is a catch if you try to use conditional rules for complex view
  48. updates: there <span class="emphasis"><em>must</em></span> be an unconditional
  49. <code class="literal">INSTEAD</code> rule for each action you wish to allow
  50. on the view. If the rule is conditional, or is not
  51. <code class="literal">INSTEAD</code>, then the system will still reject
  52. attempts to perform the update action, because it thinks it might
  53. end up trying to perform the action on the dummy table of the view
  54. in some cases. If you want to handle all the useful cases in
  55. conditional rules, add an unconditional <code class="literal">DO
  56. INSTEAD NOTHING</code> rule to ensure that the system
  57. understands it will never be called on to update the dummy table.
  58. Then make the conditional rules non-<code class="literal">INSTEAD</code>; in
  59. the cases where they are applied, they add to the default
  60. <code class="literal">INSTEAD NOTHING</code> action. (This method does not
  61. currently work to support <code class="literal">RETURNING</code> queries, however.)
  62. </p><div class="note"><h3 class="title">Note</h3><p>
  63. A view that is simple enough to be automatically updatable (see <a class="xref" href="sql-createview.html" title="CREATE VIEW"><span class="refentrytitle">CREATE VIEW</span></a>) does not require a user-created rule in
  64. order to be updatable. While you can create an explicit rule anyway,
  65. the automatic update transformation will generally outperform an
  66. explicit rule.
  67. </p><p>
  68. Another alternative worth considering is to use <code class="literal">INSTEAD OF</code>
  69. triggers (see <a class="xref" href="sql-createtrigger.html" title="CREATE TRIGGER"><span class="refentrytitle">CREATE TRIGGER</span></a>) in place of rules.
  70. </p></div></div><div class="refsect1" id="id-1.9.3.79.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
  71. The name of a rule to create. This must be distinct from the
  72. name of any other rule for the same table. Multiple rules on
  73. the same table and same event type are applied in alphabetical
  74. name order.
  75. </p></dd><dt><span class="term"><em class="replaceable"><code>event</code></em></span></dt><dd><p>
  76. The event is one of <code class="literal">SELECT</code>,
  77. <code class="literal">INSERT</code>, <code class="literal">UPDATE</code>, or
  78. <code class="literal">DELETE</code>. Note that an
  79. <code class="command">INSERT</code> containing an <code class="literal">ON
  80. CONFLICT</code> clause cannot be used on tables that have
  81. either <code class="literal">INSERT</code> or <code class="literal">UPDATE</code>
  82. rules. Consider using an updatable view instead.
  83. </p></dd><dt><span class="term"><em class="replaceable"><code>table_name</code></em></span></dt><dd><p>
  84. The name (optionally schema-qualified) of the table or view the
  85. rule applies to.
  86. </p></dd><dt><span class="term"><em class="replaceable"><code>condition</code></em></span></dt><dd><p>
  87. Any <acronym class="acronym">SQL</acronym> conditional expression (returning
  88. <code class="type">boolean</code>). The condition expression cannot refer
  89. to any tables except <code class="literal">NEW</code> and <code class="literal">OLD</code>, and
  90. cannot contain aggregate functions.
  91. </p></dd><dt><span class="term"><code class="option">INSTEAD</code></span></dt><dd><p><code class="literal">INSTEAD</code> indicates that the commands should be
  92. executed <span class="emphasis"><em>instead of</em></span> the original command.
  93. </p></dd><dt><span class="term"><code class="option">ALSO</code></span></dt><dd><p><code class="literal">ALSO</code> indicates that the commands should be
  94. executed <span class="emphasis"><em>in addition to</em></span> the original
  95. command.
  96. </p><p>
  97. If neither <code class="literal">ALSO</code> nor
  98. <code class="literal">INSTEAD</code> is specified, <code class="literal">ALSO</code>
  99. is the default.
  100. </p></dd><dt><span class="term"><em class="replaceable"><code>command</code></em></span></dt><dd><p>
  101. The command or commands that make up the rule action. Valid
  102. commands are <code class="command">SELECT</code>,
  103. <code class="command">INSERT</code>, <code class="command">UPDATE</code>,
  104. <code class="command">DELETE</code>, or <code class="command">NOTIFY</code>.
  105. </p></dd></dl></div><p>
  106. Within <em class="replaceable"><code>condition</code></em> and
  107. <em class="replaceable"><code>command</code></em>, the special
  108. table names <code class="literal">NEW</code> and <code class="literal">OLD</code> can
  109. be used to refer to values in the referenced table.
  110. <code class="literal">NEW</code> is valid in <code class="literal">ON INSERT</code> and
  111. <code class="literal">ON UPDATE</code> rules to refer to the new row being
  112. inserted or updated. <code class="literal">OLD</code> is valid in
  113. <code class="literal">ON UPDATE</code> and <code class="literal">ON DELETE</code> rules
  114. to refer to the existing row being updated or deleted.
  115. </p></div><div class="refsect1" id="id-1.9.3.79.7"><h2>Notes</h2><p>
  116. You must be the owner of a table to create or change rules for it.
  117. </p><p>
  118. In a rule for <code class="literal">INSERT</code>, <code class="literal">UPDATE</code>, or
  119. <code class="literal">DELETE</code> on a view, you can add a <code class="literal">RETURNING</code>
  120. clause that emits the view's columns. This clause will be used to compute
  121. the outputs if the rule is triggered by an <code class="command">INSERT RETURNING</code>,
  122. <code class="command">UPDATE RETURNING</code>, or <code class="command">DELETE RETURNING</code> command
  123. respectively. When the rule is triggered by a command without
  124. <code class="literal">RETURNING</code>, the rule's <code class="literal">RETURNING</code> clause will be
  125. ignored. The current implementation allows only unconditional
  126. <code class="literal">INSTEAD</code> rules to contain <code class="literal">RETURNING</code>; furthermore
  127. there can be at most one <code class="literal">RETURNING</code> clause among all the rules
  128. for the same event. (This ensures that there is only one candidate
  129. <code class="literal">RETURNING</code> clause to be used to compute the results.)
  130. <code class="literal">RETURNING</code> queries on the view will be rejected if
  131. there is no <code class="literal">RETURNING</code> clause in any available rule.
  132. </p><p>
  133. It is very important to take care to avoid circular rules. For
  134. example, though each of the following two rule definitions are
  135. accepted by <span class="productname">PostgreSQL</span>, the
  136. <code class="command">SELECT</code> command would cause
  137. <span class="productname">PostgreSQL</span> to report an error because
  138. of recursive expansion of a rule:
  139. </p><pre class="programlisting">
  140. CREATE RULE "_RETURN" AS
  141. ON SELECT TO t1
  142. DO INSTEAD
  143. SELECT * FROM t2;
  144. CREATE RULE "_RETURN" AS
  145. ON SELECT TO t2
  146. DO INSTEAD
  147. SELECT * FROM t1;
  148. SELECT * FROM t1;
  149. </pre><p>
  150. </p><p>
  151. Presently, if a rule action contains a <code class="command">NOTIFY</code>
  152. command, the <code class="command">NOTIFY</code> command will be executed
  153. unconditionally, that is, the <code class="command">NOTIFY</code> will be
  154. issued even if there are not any rows that the rule should apply
  155. to. For example, in:
  156. </p><pre class="programlisting">
  157. CREATE RULE notify_me AS ON UPDATE TO mytable DO ALSO NOTIFY mytable;
  158. UPDATE mytable SET name = 'foo' WHERE id = 42;
  159. </pre><p>
  160. one <code class="command">NOTIFY</code> event will be sent during the
  161. <code class="command">UPDATE</code>, whether or not there are any rows that
  162. match the condition <code class="literal">id = 42</code>. This is an
  163. implementation restriction that might be fixed in future releases.
  164. </p></div><div class="refsect1" id="id-1.9.3.79.8"><h2>Compatibility</h2><p>
  165. <code class="command">CREATE RULE</code> is a
  166. <span class="productname">PostgreSQL</span> language extension, as is the
  167. entire query rewrite system.
  168. </p></div><div class="refsect1" id="id-1.9.3.79.9"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-alterrule.html" title="ALTER RULE"><span class="refentrytitle">ALTER RULE</span></a>, <a class="xref" href="sql-droprule.html" title="DROP RULE"><span class="refentrytitle">DROP RULE</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-createrole.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createschema.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE ROLE </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CREATE SCHEMA</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1