1CREATE VIEW(7)          PostgreSQL 9.2.24 Documentation         CREATE VIEW(7)
2
3
4

NAME

6       CREATE_VIEW - define a new view
7

SYNOPSIS

9       CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] VIEW name [ ( column_name [, ...] ) ]
10           [ WITH ( view_option_name [= view_option_value] [, ... ] ) ]
11           AS query
12

DESCRIPTION

14       CREATE VIEW defines a view of a query. The view is not physically
15       materialized. Instead, the query is run every time the view is
16       referenced in a query.
17
18       CREATE OR REPLACE VIEW is similar, but if a view of the same name
19       already exists, it is replaced. The new query must generate the same
20       columns that were generated by the existing view query (that is, the
21       same column names in the same order and with the same data types), but
22       it may add additional columns to the end of the list. The calculations
23       giving rise to the output columns may be completely different.
24
25       If a schema name is given (for example, CREATE VIEW myschema.myview
26       ...) then the view is created in the specified schema. Otherwise it is
27       created in the current schema. Temporary views exist in a special
28       schema, so a schema name cannot be given when creating a temporary
29       view. The name of the view must be distinct from the name of any other
30       view, table, sequence, index or foreign table in the same schema.
31

PARAMETERS

33       TEMPORARY or TEMP
34           If specified, the view is created as a temporary view. Temporary
35           views are automatically dropped at the end of the current session.
36           Existing permanent relations with the same name are not visible to
37           the current session while the temporary view exists, unless they
38           are referenced with schema-qualified names.
39
40           If any of the tables referenced by the view are temporary, the view
41           is created as a temporary view (whether TEMPORARY is specified or
42           not).
43
44       name
45           The name (optionally schema-qualified) of a view to be created.
46
47       column_name
48           An optional list of names to be used for columns of the view. If
49           not given, the column names are deduced from the query.
50
51       WITH ( view_option_name [= view_option_value] [, ... ] )
52           This clause specifies optional parameters for a view; currently,
53           the only supported parameter name is security_barrier, which should
54           be enabled when a view is intended to provide row-level security.
55           See Section 37.4, “Rules and Privileges”, in the documentation for
56           full details.
57
58       query
59           A SELECT(7) or VALUES(7) command which will provide the columns and
60           rows of the view.
61

NOTES

63       Currently, views are read only: the system will not allow an insert,
64       update, or delete on a view. You can get the effect of an updatable
65       view by creating INSTEAD triggers on the view, which must convert
66       attempted inserts, etc. on the view into appropriate actions on other
67       tables. For more information see CREATE TRIGGER (CREATE_TRIGGER(7)).
68       Another possibility is to create rules (see CREATE RULE
69       (CREATE_RULE(7))), but in practice triggers are easier to understand
70       and use correctly.
71
72       Use the DROP VIEW (DROP_VIEW(7)) statement to drop views.
73
74       Be careful that the names and types of the view's columns will be
75       assigned the way you want. For example:
76
77           CREATE VIEW vista AS SELECT 'Hello World';
78
79       is bad form in two ways: the column name defaults to ?column?, and the
80       column data type defaults to unknown. If you want a string literal in a
81       view's result, use something like:
82
83           CREATE VIEW vista AS SELECT text 'Hello World' AS hello;
84
85       Access to tables referenced in the view is determined by permissions of
86       the view owner. In some cases, this can be used to provide secure but
87       restricted access to the underlying tables. However, not all views are
88       secure against tampering; see Section 37.4, “Rules and Privileges”, in
89       the documentation for details. Functions called in the view are treated
90       the same as if they had been called directly from the query using the
91       view. Therefore the user of a view must have permissions to call all
92       functions used by the view.
93
94       When CREATE OR REPLACE VIEW is used on an existing view, only the
95       view's defining SELECT rule is changed. Other view properties,
96       including ownership, permissions, and non-SELECT rules, remain
97       unchanged. You must own the view to replace it (this includes being a
98       member of the owning role).
99

EXAMPLES

101       Create a view consisting of all comedy films:
102
103           CREATE VIEW comedies AS
104               SELECT *
105               FROM films
106               WHERE kind = 'Comedy';
107
108       This will create a view containing the columns that are in the film
109       table at the time of view creation. Though * was used to create the
110       view, columns added later to the table will not be part of the view.
111

COMPATIBILITY

113       The SQL standard specifies some additional capabilities for the CREATE
114       VIEW statement:
115
116           CREATE VIEW name [ ( column_name [, ...] ) ]
117               AS query
118               [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
119
120       The optional clauses for the full SQL command are:
121
122       CHECK OPTION
123           This option has to do with updatable views. All INSERT and UPDATE
124           commands on the view will be checked to ensure data satisfy the
125           view-defining condition (that is, the new data would be visible
126           through the view). If they do not, the update will be rejected.
127
128       LOCAL
129           Check for integrity on this view.
130
131       CASCADED
132           Check for integrity on this view and on any dependent view.
133           CASCADED is assumed if neither CASCADED nor LOCAL is specified.
134
135       CREATE OR REPLACE VIEW is a PostgreSQL language extension. So is the
136       concept of a temporary view.
137

SEE ALSO

139       ALTER VIEW (ALTER_VIEW(7)), DROP VIEW (DROP_VIEW(7))
140
141
142
143PostgreSQL 9.2.24                 2017-11-06                    CREATE VIEW(7)
Impressum