2
$PostgreSQL: pgsql/doc/src/sgml/ref/create_view.sgml,v 1.29 2005-01-04 00:39:53 tgl Exp $
3
PostgreSQL documentation
6
<refentry id="SQL-CREATEVIEW">
8
<refentrytitle id="SQL-CREATEVIEW-TITLE">CREATE VIEW</refentrytitle>
9
<refmiscinfo>SQL - Language Statements</refmiscinfo>
13
<refname>CREATE VIEW</refname>
14
<refpurpose>define a new view</refpurpose>
17
<indexterm zone="sql-createview">
18
<primary>CREATE VIEW</primary>
23
CREATE [ OR REPLACE ] VIEW <replaceable class="PARAMETER">name</replaceable> [ ( <replaceable
24
class="PARAMETER">column_name</replaceable> [, ...] ) ] AS <replaceable class="PARAMETER">query</replaceable>
29
<title>Description</title>
32
<command>CREATE VIEW</command> defines a view of a query. The view
33
is not physically materialized. Instead, the query is run every time
34
the view is referenced in a query.
38
<command>CREATE OR REPLACE VIEW</command> is similar, but if a view
39
of the same name already exists, it is replaced. You can only replace
40
a view with a new query that generates the identical set of columns
41
(i.e., same column names and data types).
45
If a schema name is given (for example, <literal>CREATE VIEW
46
myschema.myview ...</>) then the view is created in the
47
specified schema. Otherwise it is created in the current schema.
48
The view name must be distinct from the name of any other view, table,
49
sequence, or index in the same schema.
54
<title>Parameters</title>
58
<term><replaceable class="parameter">name</replaceable></term>
61
The name (optionally schema-qualified) of a view to be created.
67
<term><replaceable class="parameter">column_name</replaceable></term>
70
An optional list of names to be used for columns of the view.
71
If not given, the column names are deduced from the query.
77
<term><replaceable class="parameter">query</replaceable></term>
80
A query (that is, a <command>SELECT</> statement) which will
81
provide the columns and rows of the view.
85
Refer to <xref linkend="sql-select" endterm="sql-select-title">
86
for more information about valid queries.
97
Currently, views are read only: the system will not allow an insert,
98
update, or delete on a view. You can get the effect of an updatable
99
view by creating rules that rewrite inserts, etc. on the view into
100
appropriate actions on other tables. For more information see
101
<xref linkend="sql-createrule" endterm="sql-createrule-title">.
105
Use the <command>DROP VIEW</command> statement to drop views.
109
Be careful that the names and types of the view's columns will be
110
assigned the way you want. For example,
112
CREATE VIEW vista AS SELECT 'Hello World';
114
is bad form in two ways: the column name defaults to <literal>?column?</>,
115
and the column data type defaults to <type>unknown</>. If you want a
116
string literal in a view's result, use something like
118
CREATE VIEW vista AS SELECT text 'Hello World' AS hello;
123
Access to tables referenced in the view is determined by permissions of
124
the view owner. However, functions called in the view are treated the
125
same as if they had been called directly from the query using the view.
126
Therefore the user of a view must have permissions to call all functions
133
<title>Examples</title>
136
Create a view consisting of all comedy films:
139
CREATE VIEW comedies AS
142
WHERE kind = 'Comedy';
148
<title>Compatibility</title>
151
The SQL standard specifies some additional capabilities for the
152
<command>CREATE VIEW</command> statement:
154
CREATE VIEW <replaceable class="parameter">name</replaceable> [ ( <replaceable class="parameter">column</replaceable> [, ...] ) ]
156
[ WITH [ CASCADE | LOCAL ] CHECK OPTION ]
161
The optional clauses for the full SQL command are:
165
<term><literal>CHECK OPTION</literal></term>
168
This option has to do with updatable views. All
169
<command>INSERT</> and <command>UPDATE</> commands on the view
170
will be checked to ensure data satisfy the view-defining
171
condition (that is, the new data would be visible through the
172
view). If they do not, the update will be rejected.
178
<term><literal>LOCAL</literal></term>
181
Check for integrity on this view.
187
<term><literal>CASCADE</literal></term>
190
Check for integrity on this view and on any dependent
191
view. <literal>CASCADE</> is assumed if neither
192
<literal>CASCADE</> nor <literal>LOCAL</> is specified.
200
<command>CREATE OR REPLACE VIEW</command> is a
201
<productname>PostgreSQL</productname> language extension.
206
<title>See Also</title>
208
<simplelist type="inline">
209
<member><xref linkend="sql-dropview" endterm="sql-dropview-title"></member>
214
<!-- Keep this comment at the end of the file
219
sgml-minimize-attributes:nil
220
sgml-always-quote-attributes:t
223
sgml-parent-document:nil
224
sgml-default-dtd-file:"../reference.ced"
225
sgml-exposed-tags:nil
226
sgml-local-catalogs:"/usr/lib/sgml/catalog"
227
sgml-local-ecat-files:nil