]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/create_view.sgml
Fix erroneous handling of shared dependencies (ie dependencies on roles)
[postgresql] / doc / src / sgml / ref / create_view.sgml
1 <!--
2 $PostgreSQL: pgsql/doc/src/sgml/ref/create_view.sgml,v 1.42 2009/10/02 18:13:04 tgl Exp $
3 PostgreSQL documentation
4 -->
5
6 <refentry id="SQL-CREATEVIEW">
7  <refmeta>
8   <refentrytitle id="SQL-CREATEVIEW-TITLE">CREATE VIEW</refentrytitle>
9   <manvolnum>7</manvolnum>
10   <refmiscinfo>SQL - Language Statements</refmiscinfo>
11  </refmeta>
12
13  <refnamediv>
14   <refname>CREATE VIEW</refname>
15   <refpurpose>define a new view</refpurpose>
16  </refnamediv>
17
18  <indexterm zone="sql-createview">
19   <primary>CREATE VIEW</primary>
20  </indexterm>
21
22  <refsynopsisdiv>
23 <synopsis>
24 CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] VIEW <replaceable class="PARAMETER">name</replaceable> [ ( <replaceable class="PARAMETER">column_name</replaceable> [, ...] ) ]
25     AS <replaceable class="PARAMETER">query</replaceable>
26 </synopsis>
27  </refsynopsisdiv>
28
29  <refsect1>
30   <title>Description</title>
31
32   <para>
33    <command>CREATE VIEW</command> defines a view of a query.  The view
34    is not physically materialized. Instead, the query is run every time
35    the view is referenced in a query.
36   </para>
37
38   <para>
39    <command>CREATE OR REPLACE VIEW</command> is similar, but if a view
40    of the same name already exists, it is replaced.  The new query must
41    generate the same columns that were generated by the existing view query
42    (that is, the same column names in the same order and with the same data
43    types), but it may add additional columns to the end of the list.  The
44    calculations giving rise to the output columns may be completely different.
45   </para>
46
47   <para>
48    If a schema name is given (for example, <literal>CREATE VIEW
49    myschema.myview ...</>) then the view is created in the specified
50    schema.  Otherwise it is created in the current schema.  Temporary
51    views exist in a special schema, so a schema name cannot be given
52    when creating a temporary view. The name of the view must be
53    distinct from the name of any other view, table, sequence, or index
54    in the same schema.
55   </para>
56  </refsect1>
57
58  <refsect1>
59   <title>Parameters</title>
60
61   <variablelist>
62    <varlistentry>
63     <term><literal>TEMPORARY</> or <literal>TEMP</></term>
64     <listitem>
65      <para>
66       If specified, the view is created as a temporary view.
67       Temporary views are automatically dropped at the end of the
68       current session.  Existing
69       permanent relations with the same name are not visible to the
70       current session while the temporary view exists, unless they are
71       referenced with schema-qualified names.
72      </para>
73
74      <para>
75       If any of the tables referenced by the view are temporary,
76       the view is created as a temporary view (whether
77       <literal>TEMPORARY</literal> is specified or not).
78      </para>
79     </listitem>
80    </varlistentry>
81
82    <varlistentry>
83     <term><replaceable class="parameter">name</replaceable></term>
84     <listitem>
85      <para>
86       The name (optionally schema-qualified) of a view to be created.
87      </para>
88     </listitem>
89    </varlistentry>
90
91    <varlistentry>
92     <term><replaceable class="parameter">column_name</replaceable></term>
93     <listitem>
94      <para>
95       An optional list of names to be used for columns of the view.
96       If not given, the column names are deduced from the query.
97      </para>
98     </listitem>
99    </varlistentry>
100
101    <varlistentry>
102     <term><replaceable class="parameter">query</replaceable></term>
103     <listitem>
104      <para>
105       A <xref linkend="sql-select" endterm="sql-select-title"> or
106       <xref linkend="sql-values" endterm="sql-values-title"> command
107       which will provide the columns and rows of the view.
108      </para>
109     </listitem>
110    </varlistentry>
111   </variablelist>
112  </refsect1>
113
114  <refsect1>
115   <title>Notes</title>
116
117    <para>
118     Currently, views are read only: the system will not allow an insert,
119     update, or delete on a view.  You can get the effect of an updatable
120     view by creating rules that rewrite inserts, etc. on the view into
121     appropriate actions on other tables.  For more information see
122     <xref linkend="sql-createrule" endterm="sql-createrule-title">.
123    </para>
124
125    <para>
126     Use the <xref linkend="sql-dropview" endterm="sql-dropview-title">
127     statement to drop views.
128    </para>
129
130    <para>
131     Be careful that the names and types of the view's columns will be
132     assigned the way you want.  For example:
133 <programlisting>
134 CREATE VIEW vista AS SELECT 'Hello World';
135 </programlisting>
136     is bad form in two ways: the column name defaults to <literal>?column?</>,
137     and the column data type defaults to <type>unknown</>.  If you want a
138     string literal in a view's result, use something like:
139 <programlisting>
140 CREATE VIEW vista AS SELECT text 'Hello World' AS hello;
141 </programlisting>
142    </para>
143
144    <para>
145     Access to tables referenced in the view is determined by permissions of
146     the view owner.  However, functions called in the view are treated the
147     same as if they had been called directly from the query using the view.
148     Therefore the user of a view must have permissions to call all functions
149     used by the view.
150    </para>
151
152    <para>
153     When <command>CREATE OR REPLACE VIEW</> is used on an
154     existing view, only the view's defining SELECT rule is changed.
155     Other view properties, including ownership, permissions, and non-SELECT
156     rules, remain unchanged.  You must own the view
157     to replace it (this includes being a member of the owning role).
158    </para>
159
160  </refsect1>
161
162  <refsect1>
163   <title>Examples</title>
164
165   <para>
166    Create a view consisting of all comedy films:
167
168 <programlisting>
169 CREATE VIEW comedies AS
170     SELECT *
171     FROM films
172     WHERE kind = 'Comedy';
173 </programlisting>
174   </para>
175  </refsect1>
176
177  <refsect1>
178   <title>Compatibility</title>
179
180   <para>
181    The SQL standard specifies some additional capabilities for the
182    <command>CREATE VIEW</command> statement:
183 <synopsis>
184 CREATE VIEW <replaceable class="parameter">name</replaceable> [ ( <replaceable class="parameter">column_name</replaceable> [, ...] ) ]
185     AS <replaceable class="PARAMETER">query</replaceable>
186     [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
187 </synopsis>
188   </para>
189
190   <para>
191    The optional clauses for the full SQL command are:
192
193    <variablelist>
194      <varlistentry>
195       <term><literal>CHECK OPTION</literal></term>
196       <listitem>
197        <para>
198         This option has to do with updatable views.  All
199         <command>INSERT</> and <command>UPDATE</> commands on the view
200         will be checked to ensure data satisfy the view-defining
201         condition (that is, the new data would be visible through the
202         view). If they do not, the update will be rejected.
203        </para>
204       </listitem>
205      </varlistentry>
206
207      <varlistentry>
208       <term><literal>LOCAL</literal></term>
209       <listitem>
210        <para>
211         Check for integrity on this view.
212        </para>
213       </listitem>
214      </varlistentry>
215
216      <varlistentry>
217       <term><literal>CASCADED</literal></term>
218       <listitem>
219        <para>
220         Check for integrity on this view and on any dependent
221         view. <literal>CASCADED</> is assumed if neither
222         <literal>CASCADED</> nor <literal>LOCAL</> is specified.
223        </para>
224       </listitem>
225      </varlistentry>
226    </variablelist>
227   </para>
228
229   <para>
230    <command>CREATE OR REPLACE VIEW</command> is a
231    <productname>PostgreSQL</productname> language extension.
232    So is the concept of a temporary view.
233   </para>
234  </refsect1>
235
236  <refsect1>
237   <title>See Also</title>
238
239   <simplelist type="inline">
240    <member><xref linkend="sql-alterview" endterm="sql-alterview-title"></member>
241    <member><xref linkend="sql-dropview" endterm="sql-dropview-title"></member>
242   </simplelist>
243  </refsect1>
244 </refentry>