codekingpro/portable-devtools
115k
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 Vsnapshot" /><link rel="prev" href="sql-createusermapping.html" title="CREATE USER MAPPING" /><link rel="next" href="sql-deallocate.html" title="DEALLOCATE" /></head><body id="docContent" class="container-fluid col-10"><div 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 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-deallocate.html" title="DEALLOCATE">Next</a></td></tr></table><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">3CREATE [ 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 view9 is not physically materialized. Instead, the query is run every time10 the view is referenced in a query.11 </p><p>12 <code class="command">CREATE OR REPLACE VIEW</code> is similar, but if a view13 of the same name already exists, it is replaced. The new query must14 generate the same columns that were generated by the existing view query15 (that is, the same column names in the same order and with the same data16 types), but it may add additional columns to the end of the list. The17 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 VIEW20 myschema.myview ...</code>) then the view is created in the specified21 schema. Otherwise it is created in the current schema. Temporary22 views exist in a special schema, so a schema name cannot be given23 when creating a temporary view. The name of the view must be24 distinct from the name of any other relation (table, sequence, index, view,25 materialized view, or foreign table) 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 the29 current session. Existing30 permanent relations with the same name are not visible to the31 current session while the temporary view exists, unless they are32 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 (whether36 <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 syntax41</p><pre class="synopsis">42CREATE 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 to45</p><pre class="synopsis">46CREATE 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 following56 parameters are supported:57 58 </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">check_option</code> (<code class="type">enum</code>)</span></dt><dd><p>59 This parameter may be either <code class="literal">local</code> or60 <code class="literal">cascaded</code>, and is equivalent to specifying61 <code class="literal">WITH [ CASCADED | LOCAL ] CHECK OPTION</code> (see below).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-level64 security. See <a class="xref" href="rules-privileges.html" title="41.5. Rules and Privileges">Section 41.5</a> for full details.65 </p></dd><dt><span class="term"><code class="literal">security_invoker</code> (<code class="type">boolean</code>)</span></dt><dd><p>66 This option causes the underlying base relations to be checked67 against the privileges of the user of the view rather than the view68 owner. See the notes below for full details.69 </p></dd></dl></div><p>70 71 All of the above options can be changed on existing views using <a class="link" href="sql-alterview.html" title="ALTER VIEW"><code class="command">ALTER VIEW</code></a>.72 </p></dd><dt><span class="term"><em class="replaceable"><code>query</code></em></span></dt><dd><p>73 A <a class="link" href="sql-select.html" title="SELECT"><code class="command">SELECT</code></a> or74 <a class="link" href="sql-values.html" title="VALUES"><code class="command">VALUES</code></a> command75 which will provide the columns and rows of the view.76 </p></dd><dt><span class="term"><code class="literal">WITH [ CASCADED | LOCAL ] CHECK OPTION</code>77 <a id="id-1.9.3.97.6.2.7.1.2" class="indexterm"></a>78 <a id="id-1.9.3.97.6.2.7.1.3" class="indexterm"></a>79 </span></dt><dd><p>80 This option controls the behavior of automatically updatable views. When81 this option is specified, <code class="command">INSERT</code> and <code class="command">UPDATE</code>82 commands on the view will be checked to ensure that new rows satisfy the83 view-defining condition (that is, the new rows are checked to ensure that84 they are visible through the view). If they are not, the update will be85 rejected. If the <code class="literal">CHECK OPTION</code> is not specified,86 <code class="command">INSERT</code> and <code class="command">UPDATE</code> commands on the view are87 allowed to create rows that are not visible through the view. The88 following check options are supported:89 90 </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">LOCAL</code></span></dt><dd><p>91 New rows are only checked against the conditions defined directly in92 the view itself. Any conditions defined on underlying base views are93 not checked (unless they also specify the <code class="literal">CHECK OPTION</code>).94 </p></dd><dt><span class="term"><code class="literal">CASCADED</code></span></dt><dd><p>95 New rows are checked against the conditions of the view and all96 underlying base views. If the <code class="literal">CHECK OPTION</code> is specified,97 and neither <code class="literal">LOCAL</code> nor <code class="literal">CASCADED</code> is specified,98 then <code class="literal">CASCADED</code> is assumed.99 </p></dd></dl></div><p>100 </p><p>101 The <code class="literal">CHECK OPTION</code> may not be used with <code class="literal">RECURSIVE</code>102 views.103 </p><p>104 Note that the <code class="literal">CHECK OPTION</code> is only supported on views that105 are automatically updatable, and do not have <code class="literal">INSTEAD OF</code>106 triggers or <code class="literal">INSTEAD</code> rules. If an automatically updatable107 view is defined on top of a base view that has <code class="literal">INSTEAD OF</code>108 triggers, then the <code class="literal">LOCAL CHECK OPTION</code> may be used to check109 the conditions on the automatically updatable view, but the conditions110 on the base view with <code class="literal">INSTEAD OF</code> triggers will not be111 checked (a cascaded check option will not cascade down to a112 trigger-updatable view, and any check options defined directly on a113 trigger-updatable view will be ignored). If the view or any of its base114 relations has an <code class="literal">INSTEAD</code> rule that causes the115 <code class="command">INSERT</code> or <code class="command">UPDATE</code> command to be rewritten, then116 all check options will be ignored in the rewritten query, including any117 checks from automatically updatable views defined on top of the relation118 with the <code class="literal">INSTEAD</code> rule.119 </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.97.7"><h2>Notes</h2><p>120 Use the <a class="link" href="sql-dropview.html" title="DROP VIEW"><code class="command">DROP VIEW</code></a>121 statement to drop views.122 </p><p>123 Be careful that the names and types of the view's columns will be124 assigned the way you want. For example:125</p><pre class="programlisting">126CREATE VIEW vista AS SELECT 'Hello World';127</pre><p>128 is bad form because the column name defaults to <code class="literal">?column?</code>;129 also, the column data type defaults to <code class="type">text</code>, which might not130 be what you wanted. Better style for a string literal in a view's131 result is something like:132</p><pre class="programlisting">133CREATE VIEW vista AS SELECT text 'Hello World' AS hello;134</pre><p>135 </p><p>136 By default, access to the underlying base relations referenced in the view137 is determined by the permissions of the view owner. In some cases, this138 can be used to provide secure but restricted access to the underlying139 tables. However, not all views are secure against tampering; see <a class="xref" href="rules-privileges.html" title="41.5. Rules and Privileges">Section 41.5</a> for details.140 </p><p>141 If the view has the <code class="literal">security_invoker</code> property set to142 <code class="literal">true</code>, access to the underlying base relations is143 determined by the permissions of the user executing the query, rather than144 the view owner. Thus, the user of a security invoker view must have the145 relevant permissions on the view and its underlying base relations.146 </p><p>147 If any of the underlying base relations is a security invoker view, it148 will be treated as if it had been accessed directly from the original149 query. Thus, a security invoker view will always check its underlying150 base relations using the permissions of the current user, even if it is151 accessed from a view without the <code class="literal">security_invoker</code>152 property.153 </p><p>154 If any of the underlying base relations has155 <a class="link" href="ddl-rowsecurity.html" title="5.8. Row Security Policies">row-level security</a> enabled, then156 by default, the row-level security policies of the view owner are applied,157 and access to any additional relations referred to by those policies is158 determined by the permissions of the view owner. However, if the view has159 <code class="literal">security_invoker</code> set to <code class="literal">true</code>, then160 the policies and permissions of the invoking user are used instead, as if161 the base relations had been referenced directly from the query using the162 view.163 </p><p>164 Functions called in the view are treated the same as if they had been165 called directly from the query using the view. Therefore, the user of166 a view must have permissions to call all functions used by the view.167 Functions in the view are executed with the privileges of the user168 executing the query or the function owner, depending on whether the169 functions are defined as <code class="literal">SECURITY INVOKER</code> or170 <code class="literal">SECURITY DEFINER</code>. Thus, for example, calling171 <code class="literal">CURRENT_USER</code> directly in a view will always return the172 invoking user, not the view owner. This is not affected by the view's173 <code class="literal">security_invoker</code> setting, and so a view with174 <code class="literal">security_invoker</code> set to <code class="literal">false</code> is175 <span class="emphasis"><em>not</em></span> equivalent to a176 <code class="literal">SECURITY DEFINER</code> function and those concepts should not177 be confused.178 </p><p>179 The user creating or replacing a view must have <code class="literal">USAGE</code>180 privileges on any schemas referred to in the view query, in order to look181 up the referenced objects in those schemas. Note, however, that this182 lookup only happens when the view is created or replaced. Therefore, the183 user of the view only requires the <code class="literal">USAGE</code> privilege on184 the schema containing the view, not on the schemas referred to in the view185 query, even for a security invoker view.186 </p><p>187 When <code class="command">CREATE OR REPLACE VIEW</code> is used on an existing188 view, only the view's defining SELECT rule, plus any189 <code class="literal">WITH ( ... )</code> parameters and its190 <code class="literal">CHECK OPTION</code> are changed.191 Other view properties, including ownership, permissions, and non-SELECT192 rules, remain unchanged. You must own the view193 to replace it (this includes being a member of the owning role).194 </p><div class="refsect2" id="SQL-CREATEVIEW-UPDATABLE-VIEWS"><h3>Updatable Views</h3><a id="id-1.9.3.97.7.11.2" class="indexterm"></a><p>195 Simple views are automatically updatable: the system will allow196 <code class="command">INSERT</code>, <code class="command">UPDATE</code> and <code class="command">DELETE</code> statements197 to be used on the view in the same way as on a regular table. A view is198 automatically updatable if it satisfies all of the following conditions:199 200 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>201 The view must have exactly one entry in its <code class="literal">FROM</code> list,202 which must be a table or another updatable view.203 </p></li><li class="listitem"><p>204 The view definition must not contain <code class="literal">WITH</code>,205 <code class="literal">DISTINCT</code>, <code class="literal">GROUP BY</code>, <code class="literal">HAVING</code>,206 <code class="literal">LIMIT</code>, or <code class="literal">OFFSET</code> clauses at the top level.207 </p></li><li class="listitem"><p>208 The view definition must not contain set operations (<code class="literal">UNION</code>,209 <code class="literal">INTERSECT</code> or <code class="literal">EXCEPT</code>) at the top level.210 </p></li><li class="listitem"><p>211 The view's select list must not contain any aggregates, window functions212 or set-returning functions.213 </p></li></ul></div><p>214 </p><p>215 An automatically updatable view may contain a mix of updatable and216 non-updatable columns. A column is updatable if it is a simple reference217 to an updatable column of the underlying base relation; otherwise the218 column is read-only, and an error will be raised if an <code class="command">INSERT</code>219 or <code class="command">UPDATE</code> statement attempts to assign a value to it.220 </p><p>221 If the view is automatically updatable the system will convert any222 <code class="command">INSERT</code>, <code class="command">UPDATE</code> or <code class="command">DELETE</code> statement223 on the view into the corresponding statement on the underlying base224 relation. <code class="command">INSERT</code> statements that have an <code class="literal">ON225 CONFLICT UPDATE</code> clause are fully supported.226 </p><p>227 If an automatically updatable view contains a <code class="literal">WHERE</code>228 condition, the condition restricts which rows of the base relation are229 available to be modified by <code class="command">UPDATE</code> and <code class="command">DELETE</code>230 statements on the view. However, an <code class="command">UPDATE</code> is allowed to231 change a row so that it no longer satisfies the <code class="literal">WHERE</code>232 condition, and thus is no longer visible through the view. Similarly,233 an <code class="command">INSERT</code> command can potentially insert base-relation rows234 that do not satisfy the <code class="literal">WHERE</code> condition and thus are not235 visible through the view (<code class="literal">ON CONFLICT UPDATE</code> may236 similarly affect an existing row not visible through the view).237 The <code class="literal">CHECK OPTION</code> may be used to prevent238 <code class="command">INSERT</code> and <code class="command">UPDATE</code> commands from creating239 such rows that are not visible through the view.240 </p><p>241 If an automatically updatable view is marked with the242 <code class="literal">security_barrier</code> property then all the view's <code class="literal">WHERE</code>243 conditions (and any conditions using operators which are marked as <code class="literal">LEAKPROOF</code>)244 will always be evaluated before any conditions that a user of the view has245 added. See <a class="xref" href="rules-privileges.html" title="41.5. Rules and Privileges">Section 41.5</a> for full details. Note that,246 due to this, rows which are not ultimately returned (because they do not247 pass the user's <code class="literal">WHERE</code> conditions) may still end up being locked.248 <code class="command">EXPLAIN</code> can be used to see which conditions are249 applied at the relation level (and therefore do not lock rows) and which are250 not.251 </p><p>252 A more complex view that does not satisfy all these conditions is253 read-only by default: the system will not allow an insert, update, or254 delete on the view. You can get the effect of an updatable view by255 creating <code class="literal">INSTEAD OF</code> triggers on the view, which must256 convert attempted inserts, etc. on the view into appropriate actions257 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 rules258 (see <a class="xref" href="sql-createrule.html" title="CREATE RULE"><span class="refentrytitle">CREATE RULE</span></a>), but in practice triggers are259 easier to understand and use correctly.260 </p><p>261 Note that the user performing the insert, update or delete on the view262 must have the corresponding insert, update or delete privilege on the263 view. In addition, by default, the view's owner must have the relevant264 privileges on the underlying base relations, whereas the user performing265 the update does not need any permissions on the underlying base relations266 (see <a class="xref" href="rules-privileges.html" title="41.5. Rules and Privileges">Section 41.5</a>). However, if the view has267 <code class="literal">security_invoker</code> set to <code class="literal">true</code>, the268 user performing the update, rather than the view owner, must have the269 relevant privileges on the underlying base relations.270 </p></div></div><div class="refsect1" id="id-1.9.3.97.8"><h2>Examples</h2><p>271 Create a view consisting of all comedy films:272 273</p><pre class="programlisting">274CREATE VIEW comedies AS275 SELECT *276 FROM films277 WHERE kind = 'Comedy';278</pre><p>279 This will create a view containing the columns that are in the280 <code class="literal">film</code> table at the time of view creation. Though281 <code class="literal">*</code> was used to create the view, columns added later to282 the table will not be part of the view.283 </p><p>284 Create a view with <code class="literal">LOCAL CHECK OPTION</code>:285 286</p><pre class="programlisting">287CREATE VIEW universal_comedies AS288 SELECT *289 FROM comedies290 WHERE classification = 'U'291 WITH LOCAL CHECK OPTION;292</pre><p>293 This will create a view based on the <code class="literal">comedies</code> view, showing294 only films with <code class="literal">kind = 'Comedy'</code> and295 <code class="literal">classification = 'U'</code>. Any attempt to <code class="command">INSERT</code> or296 <code class="command">UPDATE</code> a row in the view will be rejected if the new row297 doesn't have <code class="literal">classification = 'U'</code>, but the film298 <code class="literal">kind</code> will not be checked.299 </p><p>300 Create a view with <code class="literal">CASCADED CHECK OPTION</code>:301 302</p><pre class="programlisting">303CREATE VIEW pg_comedies AS304 SELECT *305 FROM comedies306 WHERE classification = 'PG'307 WITH CASCADED CHECK OPTION;308</pre><p>309 This will create a view that checks both the <code class="literal">kind</code> and310 <code class="literal">classification</code> of new rows.311 </p><p>312 Create a view with a mix of updatable and non-updatable columns:313 314</p><pre class="programlisting">315CREATE VIEW comedies AS316 SELECT f.*,317 country_code_to_name(f.country_code) AS country,318 (SELECT avg(r.rating)319 FROM user_ratings r320 WHERE r.film_id = f.id) AS avg_rating321 FROM films f322 WHERE f.kind = 'Comedy';323</pre><p>324 This view will support <code class="command">INSERT</code>, <code class="command">UPDATE</code> and325 <code class="command">DELETE</code>. All the columns from the <code class="literal">films</code> table will326 be updatable, whereas the computed columns <code class="literal">country</code> and327 <code class="literal">avg_rating</code> will be read-only.328 </p><p>329 Create a recursive view consisting of the numbers from 1 to 100:330</p><pre class="programlisting">331CREATE RECURSIVE VIEW public.nums_1_100 (n) AS332 VALUES (1)333UNION ALL334 SELECT n+1 FROM nums_1_100 WHERE n < 100;335</pre><p>336 Notice that although the recursive view's name is schema-qualified in this337 <code class="command">CREATE</code>, its internal self-reference is not schema-qualified.338 This is because the implicitly-created CTE's name cannot be339 schema-qualified.340 </p></div><div class="refsect1" id="id-1.9.3.97.9"><h2>Compatibility</h2><p>341 <code class="command">CREATE OR REPLACE VIEW</code> is a342 <span class="productname">PostgreSQL</span> language extension.343 So is the concept of a temporary view.344 The <code class="literal">WITH ( ... )</code> clause is an extension as well, as are345 security barrier views and security invoker views.346 </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" title="CREATE USER MAPPING">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-deallocate.html" title="DEALLOCATE">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" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> DEALLOCATE</td></tr></table></div></body></html>