1CREATE VIEW(7) SQL Commands CREATE VIEW(7)
2
3
4
6 CREATE VIEW - define a new view
7
8
10 CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] VIEW name [ ( column_name [, ...] ) ]
11 AS query
12
13
15 CREATE VIEW defines a view of a query. The view is not physically mate‐
16 rialized. Instead, the query is run every time the view is referenced
17 in a query.
18
19 CREATE OR REPLACE VIEW is similar, but if a view of the same name
20 already exists, it is replaced. The new query must generate the same
21 columns that were generated by the existing view query (that is, the
22 same column names in the same order and with the same data types), but
23 it may add additional columns to the end of the list. The calculations
24 giving rise to the output columns may be completely different.
25
26 If a schema name is given (for example, CREATE VIEW myschema.myview
27 ...) then the view is created in the specified schema. Otherwise it is
28 created in the current schema. Temporary views exist in a special
29 schema, so a schema name cannot be given when creating a temporary
30 view. The name of the view must be distinct from the name of any other
31 view, table, sequence, or index in the same schema.
32
34 TEMPORARY or TEMP
35 If specified, the view is created as a temporary view. Tempo‐
36 rary views are automatically dropped at the end of the current
37 session. Existing permanent relations with the same name are not
38 visible to the current session while the temporary view exists,
39 unless they are referenced with schema-qualified names.
40
41 If any of the tables referenced by the view are temporary, the
42 view is created as a temporary view (whether TEMPORARY is speci‐
43 fied or not).
44
45 name 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.
49 If not given, the column names are deduced from the query.
50
51 query A SELECT [select(7)] or VALUES [values(7)] command which will
52 provide the columns and rows of the view.
53
55 Currently, views are read only: the system will not allow an insert,
56 update, or delete on a view. You can get the effect of an updatable
57 view by creating rules that rewrite inserts, etc. on the view into
58 appropriate actions on other tables. For more information see CREATE
59 RULE [create_rule(7)].
60
61 Use the DROP VIEW [drop_view(7)] statement to drop views.
62
63 Be careful that the names and types of the view's columns will be
64 assigned the way you want. For example:
65
66 CREATE VIEW vista AS SELECT 'Hello World';
67
68 is bad form in two ways: the column name defaults to ?column?, and the
69 column data type defaults to unknown. If you want a string literal in a
70 view's result, use something like:
71
72 CREATE VIEW vista AS SELECT text 'Hello World' AS hello;
73
74
75 Access to tables referenced in the view is determined by permissions of
76 the view owner. In some cases, this can be used to provide secure but
77 restricted access to the underlying tables. However, not all views are
78 secure against tampering; see in the documentation for details. Func‐
79 tions called in the view are treated the same as if they had been
80 called directly from the query using the view. Therefore the user of a
81 view must have permissions to call all functions used by the view.
82
83 When CREATE OR REPLACE VIEW is used on an existing view, only the
84 view's defining SELECT rule is changed. Other view properties, includ‐
85 ing ownership, permissions, and non-SELECT rules, remain unchanged. You
86 must own the view to replace it (this includes being a member of the
87 owning role).
88
90 Create a view consisting of all comedy films:
91
92 CREATE VIEW comedies AS
93 SELECT *
94 FROM films
95 WHERE kind = 'Comedy';
96
97
99 The SQL standard specifies some additional capabilities for the CREATE
100 VIEW statement:
101
102 CREATE VIEW name [ ( column_name [, ...] ) ]
103 AS query
104 [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
105
106
107 The optional clauses for the full SQL command are:
108
109 CHECK OPTION
110 This option has to do with updatable views. All INSERT and
111 UPDATE commands on the view will be checked to ensure data sat‐
112 isfy the view-defining condition (that is, the new data would be
113 visible through the view). If they do not, the update will be
114 rejected.
115
116 LOCAL Check for integrity on this view.
117
118 CASCADED
119 Check for integrity on this view and on any dependent view. CAS‐
120 CADED is assumed if neither CASCADED nor LOCAL is specified.
121
122 CREATE OR REPLACE VIEW is a PostgreSQL language extension. So is the
123 concept of a temporary view.
124
126 ALTER VIEW [alter_view(7)], DROP VIEW [drop_view(7)]
127
128
129
130SQL - Language Statements 2014-02-17 CREATE VIEW(7)