1CREATE VIEW() SQL Commands CREATE VIEW()
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. You can only replace a view with a new
21 query that generates the identical set of columns (i.e., same column
22 names and data types).
23
24 If a schema name is given (for example, CREATE VIEW myschema.myview
25 ...) then the view is created in the specified schema. Otherwise it is
26 created in the current schema. Temporary views exist in a special
27 schema, so a schema name may not be given when creating a temporary
28 view. The name of the view must be distinct from the name of any other
29 view, table, sequence, or index in the same schema.
30
32 TEMPORARY or TEMP
33 If specified, the view is created as a temporary view. Tempo‐
34 rary views are automatically dropped at the end of the current
35 session. Existing permanent relations with the same name are not
36 visible to the current session while the temporary view exists,
37 unless they are referenced with schema-qualified names.
38
39 If any of the tables referenced by the view are temporary, the
40 view is created as a temporary view (whether TEMPORARY is speci‐
41 fied or not).
42
43 name The name (optionally schema-qualified) of a view to be created.
44
45 column_name
46 An optional list of names to be used for columns of the view.
47 If not given, the column names are deduced from the query.
48
49 query A SELECT [select(7)] or VALUES [values(7)] command which will
50 provide the columns and rows of the view.
51
53 Currently, views are read only: the system will not allow an insert,
54 update, or delete on a view. You can get the effect of an updatable
55 view by creating rules that rewrite inserts, etc. on the view into
56 appropriate actions on other tables. For more information see CREATE
57 RULE [create_rule(7)].
58
59 Use the DROP VIEW [drop_view(7)] statement to drop views.
60
61 Be careful that the names and types of the view's columns will be
62 assigned the way you want. For example,
63
64 CREATE VIEW vista AS SELECT 'Hello World';
65
66 is bad form in two ways: the column name defaults to ?column?, and the
67 column data type defaults to unknown. If you want a string literal in a
68 view's result, use something like
69
70 CREATE VIEW vista AS SELECT text 'Hello World' AS hello;
71
72
73 Access to tables referenced in the view is determined by permissions of
74 the view owner. However, functions called in the view are treated the
75 same as if they had been called directly from the query using the view.
76 Therefore the user of a view must have permissions to call all func‐
77 tions used by the view.
78
80 Create a view consisting of all comedy films:
81
82 CREATE VIEW comedies AS
83 SELECT *
84 FROM films
85 WHERE kind = 'Comedy';
86
87
89 The SQL standard specifies some additional capabilities for the CREATE
90 VIEW statement:
91
92 CREATE VIEW name [ ( column_name [, ...] ) ]
93 AS query
94 [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
95
96
97 The optional clauses for the full SQL command are:
98
99 CHECK OPTION
100 This option has to do with updatable views. All INSERT and
101 UPDATE commands on the view will be checked to ensure data sat‐
102 isfy the view-defining condition (that is, the new data would be
103 visible through the view). If they do not, the update will be
104 rejected.
105
106 LOCAL Check for integrity on this view.
107
108 CASCADED
109 Check for integrity on this view and on any dependent view. CAS‐
110 CADED is assumed if neither CASCADED nor LOCAL is specified.
111
112 CREATE OR REPLACE VIEW is a PostgreSQL language extension. So is the
113 concept of a temporary view.
114
116 DROP VIEW [drop_view(7)]
117
118
119
120SQL - Language Statements 2008-06-08 CREATE VIEW()