Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / doc / src / sgml / ref / create_view.sgml
blob2d485c33eaf4ce34b982e8887e4090d254c3a446
1 <!--
2 $PostgreSQL$
3 PostgreSQL documentation
4 -->
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>
13 <refnamediv>
14 <refname>CREATE VIEW</refname>
15 <refpurpose>define a new view</refpurpose>
16 </refnamediv>
18 <indexterm zone="sql-createview">
19 <primary>CREATE VIEW</primary>
20 </indexterm>
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>
29 <refsect1>
30 <title>Description</title>
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>
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>
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>
58 <refsect1>
59 <title>Parameters</title>
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>
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>
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>
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>
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>
114 <refsect1>
115 <title>Notes</title>
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>
125 <para>
126 Use the <xref linkend="sql-dropview" endterm="sql-dropview-title">
127 statement to drop views.
128 </para>
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>
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>
152 </refsect1>
154 <refsect1>
155 <title>Examples</title>
157 <para>
158 Create a view consisting of all comedy films:
160 <programlisting>
161 CREATE VIEW comedies AS
162 SELECT *
163 FROM films
164 WHERE kind = 'Comedy';
165 </programlisting>
166 </para>
167 </refsect1>
169 <refsect1>
170 <title>Compatibility</title>
172 <para>
173 The SQL standard specifies some additional capabilities for the
174 <command>CREATE VIEW</command> statement:
175 <synopsis>
176 CREATE VIEW <replaceable class="parameter">name</replaceable> [ ( <replaceable class="parameter">column_name</replaceable> [, ...] ) ]
177 AS <replaceable class="PARAMETER">query</replaceable>
178 [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
179 </synopsis>
180 </para>
182 <para>
183 The optional clauses for the full SQL command are:
185 <variablelist>
186 <varlistentry>
187 <term><literal>CHECK OPTION</literal></term>
188 <listitem>
189 <para>
190 This option has to do with updatable views. All
191 <command>INSERT</> and <command>UPDATE</> commands on the view
192 will be checked to ensure data satisfy the view-defining
193 condition (that is, the new data would be visible through the
194 view). If they do not, the update will be rejected.
195 </para>
196 </listitem>
197 </varlistentry>
199 <varlistentry>
200 <term><literal>LOCAL</literal></term>
201 <listitem>
202 <para>
203 Check for integrity on this view.
204 </para>
205 </listitem>
206 </varlistentry>
208 <varlistentry>
209 <term><literal>CASCADED</literal></term>
210 <listitem>
211 <para>
212 Check for integrity on this view and on any dependent
213 view. <literal>CASCADED</> is assumed if neither
214 <literal>CASCADED</> nor <literal>LOCAL</> is specified.
215 </para>
216 </listitem>
217 </varlistentry>
218 </variablelist>
219 </para>
221 <para>
222 <command>CREATE OR REPLACE VIEW</command> is a
223 <productname>PostgreSQL</productname> language extension.
224 So is the concept of a temporary view.
225 </para>
226 </refsect1>
228 <refsect1>
229 <title>See Also</title>
231 <simplelist type="inline">
232 <member><xref linkend="sql-alterview" endterm="sql-alterview-title"></member>
233 <member><xref linkend="sql-dropview" endterm="sql-dropview-title"></member>
234 </simplelist>
235 </refsect1>
236 </refentry>