gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

292 行
22KB

  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 VIEW</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-createusermapping.html" title="CREATE USER MAPPING" /><link rel="next" href="sql-deallocate.html" title="DEALLOCATE" /></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 VIEW</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createusermapping.html" title="CREATE USER MAPPING">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-deallocate.html" title="DEALLOCATE">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATEVIEW"><div class="titlepage"></div><a id="id-1.9.3.97.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE VIEW</span></h2><p>CREATE VIEW — define a new view</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] [ RECURSIVE ] VIEW <em class="replaceable"><code>name</code></em> [ ( <em class="replaceable"><code>column_name</code></em> [, ...] ) ]
  4. [ WITH ( <em class="replaceable"><code>view_option_name</code></em> [= <em class="replaceable"><code>view_option_value</code></em>] [, ... ] ) ]
  5. AS <em class="replaceable"><code>query</code></em>
  6. [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
  7. </pre></div><div class="refsect1" id="id-1.9.3.97.5"><h2>Description</h2><p>
  8. <code class="command">CREATE VIEW</code> defines a view of a query. The view
  9. is not physically materialized. Instead, the query is run every time
  10. the view is referenced in a query.
  11. </p><p>
  12. <code class="command">CREATE OR REPLACE VIEW</code> is similar, but if a view
  13. of the same name already exists, it is replaced. The new query must
  14. generate the same columns that were generated by the existing view query
  15. (that is, the same column names in the same order and with the same data
  16. types), but it may add additional columns to the end of the list. The
  17. calculations giving rise to the output columns may be completely different.
  18. </p><p>
  19. If a schema name is given (for example, <code class="literal">CREATE VIEW
  20. myschema.myview ...</code>) then the view is created in the specified
  21. schema. Otherwise it is created in the current schema. Temporary
  22. views exist in a special schema, so a schema name cannot be given
  23. when creating a temporary view. The name of the view must be
  24. distinct from the name of any other view, table, sequence, index or foreign table
  25. in the same schema.
  26. </p></div><div class="refsect1" id="id-1.9.3.97.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">TEMPORARY</code> or <code class="literal">TEMP</code></span></dt><dd><p>
  27. If specified, the view is created as a temporary view.
  28. Temporary views are automatically dropped at the end of the
  29. current session. Existing
  30. permanent relations with the same name are not visible to the
  31. current session while the temporary view exists, unless they are
  32. referenced with schema-qualified names.
  33. </p><p>
  34. If any of the tables referenced by the view are temporary,
  35. the view is created as a temporary view (whether
  36. <code class="literal">TEMPORARY</code> is specified or not).
  37. </p></dd><dt><span class="term"><code class="literal">RECURSIVE</code>
  38. <a id="id-1.9.3.97.6.2.2.1.2" class="indexterm"></a>
  39. </span></dt><dd><p>
  40. Creates a recursive view. The syntax
  41. </p><pre class="synopsis">
  42. CREATE RECURSIVE VIEW [ <em class="replaceable"><code>schema</code></em> . ] <em class="replaceable"><code>view_name</code></em> (<em class="replaceable"><code>column_names</code></em>) AS SELECT <em class="replaceable"><code>...</code></em>;
  43. </pre><p>
  44. is equivalent to
  45. </p><pre class="synopsis">
  46. CREATE VIEW [ <em class="replaceable"><code>schema</code></em> . ] <em class="replaceable"><code>view_name</code></em> AS WITH RECURSIVE <em class="replaceable"><code>view_name</code></em> (<em class="replaceable"><code>column_names</code></em>) AS (SELECT <em class="replaceable"><code>...</code></em>) SELECT <em class="replaceable"><code>column_names</code></em> FROM <em class="replaceable"><code>view_name</code></em>;
  47. </pre><p>
  48. A view column name list must be specified for a recursive view.
  49. </p></dd><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
  50. The name (optionally schema-qualified) of a view to be created.
  51. </p></dd><dt><span class="term"><em class="replaceable"><code>column_name</code></em></span></dt><dd><p>
  52. An optional list of names to be used for columns of the view.
  53. If not given, the column names are deduced from the query.
  54. </p></dd><dt><span class="term"><code class="literal">WITH ( <em class="replaceable"><code>view_option_name</code></em> [= <em class="replaceable"><code>view_option_value</code></em>] [, ... ] )</code></span></dt><dd><p>
  55. This clause specifies optional parameters for a view; the following
  56. parameters are supported:
  57. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">check_option</code> (<code class="type">string</code>)</span></dt><dd><p>
  58. This parameter may be either <code class="literal">local</code> or
  59. <code class="literal">cascaded</code>, and is equivalent to specifying
  60. <code class="literal">WITH [ CASCADED | LOCAL ] CHECK OPTION</code> (see below).
  61. This option can be changed on existing views using <a class="xref" href="sql-alterview.html" title="ALTER VIEW"><span class="refentrytitle">ALTER VIEW</span></a>.
  62. </p></dd><dt><span class="term"><code class="literal">security_barrier</code> (<code class="type">boolean</code>)</span></dt><dd><p>
  63. This should be used if the view is intended to provide row-level
  64. security. See <a class="xref" href="rules-privileges.html" title="40.5. Rules and Privileges">Section 40.5</a> for full details.
  65. </p></dd></dl></div><p>
  66. </p></dd><dt><span class="term"><em class="replaceable"><code>query</code></em></span></dt><dd><p>
  67. A <a class="xref" href="sql-select.html" title="SELECT"><span class="refentrytitle">SELECT</span></a> or
  68. <a class="xref" href="sql-values.html" title="VALUES"><span class="refentrytitle">VALUES</span></a> command
  69. which will provide the columns and rows of the view.
  70. </p></dd><dt><span class="term"><code class="literal">WITH [ CASCADED | LOCAL ] CHECK OPTION</code>
  71. <a id="id-1.9.3.97.6.2.7.1.2" class="indexterm"></a>
  72. <a id="id-1.9.3.97.6.2.7.1.3" class="indexterm"></a>
  73. </span></dt><dd><p>
  74. This option controls the behavior of automatically updatable views. When
  75. this option is specified, <code class="command">INSERT</code> and <code class="command">UPDATE</code>
  76. commands on the view will be checked to ensure that new rows satisfy the
  77. view-defining condition (that is, the new rows are checked to ensure that
  78. they are visible through the view). If they are not, the update will be
  79. rejected. If the <code class="literal">CHECK OPTION</code> is not specified,
  80. <code class="command">INSERT</code> and <code class="command">UPDATE</code> commands on the view are
  81. allowed to create rows that are not visible through the view. The
  82. following check options are supported:
  83. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">LOCAL</code></span></dt><dd><p>
  84. New rows are only checked against the conditions defined directly in
  85. the view itself. Any conditions defined on underlying base views are
  86. not checked (unless they also specify the <code class="literal">CHECK OPTION</code>).
  87. </p></dd><dt><span class="term"><code class="literal">CASCADED</code></span></dt><dd><p>
  88. New rows are checked against the conditions of the view and all
  89. underlying base views. If the <code class="literal">CHECK OPTION</code> is specified,
  90. and neither <code class="literal">LOCAL</code> nor <code class="literal">CASCADED</code> is specified,
  91. then <code class="literal">CASCADED</code> is assumed.
  92. </p></dd></dl></div><p>
  93. </p><p>
  94. The <code class="literal">CHECK OPTION</code> may not be used with <code class="literal">RECURSIVE</code>
  95. views.
  96. </p><p>
  97. Note that the <code class="literal">CHECK OPTION</code> is only supported on views that
  98. are automatically updatable, and do not have <code class="literal">INSTEAD OF</code>
  99. triggers or <code class="literal">INSTEAD</code> rules. If an automatically updatable
  100. view is defined on top of a base view that has <code class="literal">INSTEAD OF</code>
  101. triggers, then the <code class="literal">LOCAL CHECK OPTION</code> may be used to check
  102. the conditions on the automatically updatable view, but the conditions
  103. on the base view with <code class="literal">INSTEAD OF</code> triggers will not be
  104. checked (a cascaded check option will not cascade down to a
  105. trigger-updatable view, and any check options defined directly on a
  106. trigger-updatable view will be ignored). If the view or any of its base
  107. relations has an <code class="literal">INSTEAD</code> rule that causes the
  108. <code class="command">INSERT</code> or <code class="command">UPDATE</code> command to be rewritten, then
  109. all check options will be ignored in the rewritten query, including any
  110. checks from automatically updatable views defined on top of the relation
  111. with the <code class="literal">INSTEAD</code> rule.
  112. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.97.7"><h2>Notes</h2><p>
  113. Use the <a class="xref" href="sql-dropview.html" title="DROP VIEW"><span class="refentrytitle">DROP VIEW</span></a>
  114. statement to drop views.
  115. </p><p>
  116. Be careful that the names and types of the view's columns will be
  117. assigned the way you want. For example:
  118. </p><pre class="programlisting">
  119. CREATE VIEW vista AS SELECT 'Hello World';
  120. </pre><p>
  121. is bad form because the column name defaults to <code class="literal">?column?</code>;
  122. also, the column data type defaults to <code class="type">text</code>, which might not
  123. be what you wanted. Better style for a string literal in a view's
  124. result is something like:
  125. </p><pre class="programlisting">
  126. CREATE VIEW vista AS SELECT text 'Hello World' AS hello;
  127. </pre><p>
  128. </p><p>
  129. Access to tables referenced in the view is determined by permissions of
  130. the view owner. In some cases, this can be used to provide secure but
  131. restricted access to the underlying tables. However, not all views are
  132. secure against tampering; see <a class="xref" href="rules-privileges.html" title="40.5. Rules and Privileges">Section 40.5</a> for
  133. details. Functions called in the view are treated the same as if they had
  134. been called directly from the query using the view. Therefore the user of
  135. a view must have permissions to call all functions used by the view.
  136. </p><p>
  137. When <code class="command">CREATE OR REPLACE VIEW</code> is used on an
  138. existing view, only the view's defining SELECT rule is changed.
  139. Other view properties, including ownership, permissions, and non-SELECT
  140. rules, remain unchanged. You must own the view
  141. to replace it (this includes being a member of the owning role).
  142. </p><div class="refsect2" id="SQL-CREATEVIEW-UPDATABLE-VIEWS"><h3>Updatable Views</h3><a id="id-1.9.3.97.7.6.2" class="indexterm"></a><p>
  143. Simple views are automatically updatable: the system will allow
  144. <code class="command">INSERT</code>, <code class="command">UPDATE</code> and <code class="command">DELETE</code> statements
  145. to be used on the view in the same way as on a regular table. A view is
  146. automatically updatable if it satisfies all of the following conditions:
  147. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  148. The view must have exactly one entry in its <code class="literal">FROM</code> list,
  149. which must be a table or another updatable view.
  150. </p></li><li class="listitem"><p>
  151. The view definition must not contain <code class="literal">WITH</code>,
  152. <code class="literal">DISTINCT</code>, <code class="literal">GROUP BY</code>, <code class="literal">HAVING</code>,
  153. <code class="literal">LIMIT</code>, or <code class="literal">OFFSET</code> clauses at the top level.
  154. </p></li><li class="listitem"><p>
  155. The view definition must not contain set operations (<code class="literal">UNION</code>,
  156. <code class="literal">INTERSECT</code> or <code class="literal">EXCEPT</code>) at the top level.
  157. </p></li><li class="listitem"><p>
  158. The view's select list must not contain any aggregates, window functions
  159. or set-returning functions.
  160. </p></li></ul></div><p>
  161. </p><p>
  162. An automatically updatable view may contain a mix of updatable and
  163. non-updatable columns. A column is updatable if it is a simple reference
  164. to an updatable column of the underlying base relation; otherwise the
  165. column is read-only, and an error will be raised if an <code class="command">INSERT</code>
  166. or <code class="command">UPDATE</code> statement attempts to assign a value to it.
  167. </p><p>
  168. If the view is automatically updatable the system will convert any
  169. <code class="command">INSERT</code>, <code class="command">UPDATE</code> or <code class="command">DELETE</code> statement
  170. on the view into the corresponding statement on the underlying base
  171. relation. <code class="command">INSERT</code> statements that have an <code class="literal">ON
  172. CONFLICT UPDATE</code> clause are fully supported.
  173. </p><p>
  174. If an automatically updatable view contains a <code class="literal">WHERE</code>
  175. condition, the condition restricts which rows of the base relation are
  176. available to be modified by <code class="command">UPDATE</code> and <code class="command">DELETE</code>
  177. statements on the view. However, an <code class="command">UPDATE</code> is allowed to
  178. change a row so that it no longer satisfies the <code class="literal">WHERE</code>
  179. condition, and thus is no longer visible through the view. Similarly,
  180. an <code class="command">INSERT</code> command can potentially insert base-relation rows
  181. that do not satisfy the <code class="literal">WHERE</code> condition and thus are not
  182. visible through the view (<code class="literal">ON CONFLICT UPDATE</code> may
  183. similarly affect an existing row not visible through the view).
  184. The <code class="literal">CHECK OPTION</code> may be used to prevent
  185. <code class="command">INSERT</code> and <code class="command">UPDATE</code> commands from creating
  186. such rows that are not visible through the view.
  187. </p><p>
  188. If an automatically updatable view is marked with the
  189. <code class="literal">security_barrier</code> property then all the view's <code class="literal">WHERE</code>
  190. conditions (and any conditions using operators which are marked as <code class="literal">LEAKPROOF</code>)
  191. will always be evaluated before any conditions that a user of the view has
  192. added. See <a class="xref" href="rules-privileges.html" title="40.5. Rules and Privileges">Section 40.5</a> for full details. Note that,
  193. due to this, rows which are not ultimately returned (because they do not
  194. pass the user's <code class="literal">WHERE</code> conditions) may still end up being locked.
  195. <code class="command">EXPLAIN</code> can be used to see which conditions are
  196. applied at the relation level (and therefore do not lock rows) and which are
  197. not.
  198. </p><p>
  199. A more complex view that does not satisfy all these conditions is
  200. read-only by default: the system will not allow an insert, update, or
  201. delete on the view. You can get the effect of an updatable view by
  202. creating <code class="literal">INSTEAD OF</code> triggers on the view, which must
  203. convert attempted inserts, etc. on the view into appropriate actions
  204. on other tables. For more information see <a class="xref" href="sql-createtrigger.html" title="CREATE TRIGGER"><span class="refentrytitle">CREATE TRIGGER</span></a>. Another possibility is to create rules
  205. (see <a class="xref" href="sql-createrule.html" title="CREATE RULE"><span class="refentrytitle">CREATE RULE</span></a>), but in practice triggers are
  206. easier to understand and use correctly.
  207. </p><p>
  208. Note that the user performing the insert, update or delete on the view
  209. must have the corresponding insert, update or delete privilege on the
  210. view. In addition the view's owner must have the relevant privileges on
  211. the underlying base relations, but the user performing the update does
  212. not need any permissions on the underlying base relations (see
  213. <a class="xref" href="rules-privileges.html" title="40.5. Rules and Privileges">Section 40.5</a>).
  214. </p></div></div><div class="refsect1" id="id-1.9.3.97.8"><h2>Examples</h2><p>
  215. Create a view consisting of all comedy films:
  216. </p><pre class="programlisting">
  217. CREATE VIEW comedies AS
  218. SELECT *
  219. FROM films
  220. WHERE kind = 'Comedy';
  221. </pre><p>
  222. This will create a view containing the columns that are in the
  223. <code class="literal">film</code> table at the time of view creation. Though
  224. <code class="literal">*</code> was used to create the view, columns added later to
  225. the table will not be part of the view.
  226. </p><p>
  227. Create a view with <code class="literal">LOCAL CHECK OPTION</code>:
  228. </p><pre class="programlisting">
  229. CREATE VIEW universal_comedies AS
  230. SELECT *
  231. FROM comedies
  232. WHERE classification = 'U'
  233. WITH LOCAL CHECK OPTION;
  234. </pre><p>
  235. This will create a view based on the <code class="literal">comedies</code> view, showing
  236. only films with <code class="literal">kind = 'Comedy'</code> and
  237. <code class="literal">classification = 'U'</code>. Any attempt to <code class="command">INSERT</code> or
  238. <code class="command">UPDATE</code> a row in the view will be rejected if the new row
  239. doesn't have <code class="literal">classification = 'U'</code>, but the film
  240. <code class="literal">kind</code> will not be checked.
  241. </p><p>
  242. Create a view with <code class="literal">CASCADED CHECK OPTION</code>:
  243. </p><pre class="programlisting">
  244. CREATE VIEW pg_comedies AS
  245. SELECT *
  246. FROM comedies
  247. WHERE classification = 'PG'
  248. WITH CASCADED CHECK OPTION;
  249. </pre><p>
  250. This will create a view that checks both the <code class="literal">kind</code> and
  251. <code class="literal">classification</code> of new rows.
  252. </p><p>
  253. Create a view with a mix of updatable and non-updatable columns:
  254. </p><pre class="programlisting">
  255. CREATE VIEW comedies AS
  256. SELECT f.*,
  257. country_code_to_name(f.country_code) AS country,
  258. (SELECT avg(r.rating)
  259. FROM user_ratings r
  260. WHERE r.film_id = f.id) AS avg_rating
  261. FROM films f
  262. WHERE f.kind = 'Comedy';
  263. </pre><p>
  264. This view will support <code class="command">INSERT</code>, <code class="command">UPDATE</code> and
  265. <code class="command">DELETE</code>. All the columns from the <code class="literal">films</code> table will
  266. be updatable, whereas the computed columns <code class="literal">country</code> and
  267. <code class="literal">avg_rating</code> will be read-only.
  268. </p><p>
  269. Create a recursive view consisting of the numbers from 1 to 100:
  270. </p><pre class="programlisting">
  271. CREATE RECURSIVE VIEW public.nums_1_100 (n) AS
  272. VALUES (1)
  273. UNION ALL
  274. SELECT n+1 FROM nums_1_100 WHERE n &lt; 100;
  275. </pre><p>
  276. Notice that although the recursive view's name is schema-qualified in this
  277. <code class="command">CREATE</code>, its internal self-reference is not schema-qualified.
  278. This is because the implicitly-created CTE's name cannot be
  279. schema-qualified.
  280. </p></div><div class="refsect1" id="id-1.9.3.97.9"><h2>Compatibility</h2><p>
  281. <code class="command">CREATE OR REPLACE VIEW</code> is a
  282. <span class="productname">PostgreSQL</span> language extension.
  283. So is the concept of a temporary view.
  284. The <code class="literal">WITH ( ... )</code> clause is an extension as well.
  285. </p></div><div class="refsect1" id="id-1.9.3.97.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-alterview.html" title="ALTER VIEW"><span class="refentrytitle">ALTER VIEW</span></a>, <a class="xref" href="sql-dropview.html" title="DROP VIEW"><span class="refentrytitle">DROP VIEW</span></a>, <a class="xref" href="sql-creatematerializedview.html" title="CREATE MATERIALIZED VIEW"><span class="refentrytitle">CREATE MATERIALIZED VIEW</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-createusermapping.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-deallocate.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE USER MAPPING </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> DEALLOCATE</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1