2 $PostgreSQL: pgsql/doc/src/sgml/ref/create_view.sgml,v 1.42 2009/10/02 18:13:04 tgl Exp $
3 PostgreSQL documentation
6 <refentry id="SQL-CREATEVIEW">
8 <refentrytitle id="SQL-CREATEVIEW-TITLE">CREATE VIEW</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements</refmiscinfo>
14 <refname>CREATE VIEW</refname>
15 <refpurpose>define a new view</refpurpose>
18 <indexterm zone="sql-createview">
19 <primary>CREATE VIEW</primary>
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>
30 <title>Description</title>
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.
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.
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
59 <title>Parameters</title>
63 <term><literal>TEMPORARY</> or <literal>TEMP</></term>
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.
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).
83 <term><replaceable class="parameter">name</replaceable></term>
86 The name (optionally schema-qualified) of a view to be created.
92 <term><replaceable class="parameter">column_name</replaceable></term>
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.
102 <term><replaceable class="parameter">query</replaceable></term>
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.
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">.
126 Use the <xref linkend="sql-dropview" endterm="sql-dropview-title">
127 statement to drop views.
131 Be careful that the names and types of the view's columns will be
132 assigned the way you want. For example:
134 CREATE VIEW vista AS SELECT 'Hello World';
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:
140 CREATE VIEW vista AS SELECT text 'Hello World' AS hello;
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
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).
163 <title>Examples</title>
166 Create a view consisting of all comedy films:
169 CREATE VIEW comedies AS
172 WHERE kind = 'Comedy';
178 <title>Compatibility</title>
181 The SQL standard specifies some additional capabilities for the
182 <command>CREATE VIEW</command> statement:
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 ]
191 The optional clauses for the full SQL command are:
195 <term><literal>CHECK OPTION</literal></term>
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.
208 <term><literal>LOCAL</literal></term>
211 Check for integrity on this view.
217 <term><literal>CASCADED</literal></term>
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.
230 <command>CREATE OR REPLACE VIEW</command> is a
231 <productname>PostgreSQL</productname> language extension.
232 So is the concept of a temporary view.
237 <title>See Also</title>
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>