ok
Direktori : /opt/alt/postgresql11/usr/share/doc/alt-postgresql11-9.2.24/html/ |
Current File : //opt/alt/postgresql11/usr/share/doc/alt-postgresql11-9.2.24/html/sql-createview.html |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >CREATE VIEW</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 9.2.24 Documentation" HREF="index.html"><LINK REL="UP" TITLE="SQL Commands" HREF="sql-commands.html"><LINK REL="PREVIOUS" TITLE="CREATE USER MAPPING" HREF="sql-createusermapping.html"><LINK REL="NEXT" TITLE="DEALLOCATE" HREF="sql-deallocate.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2017-11-06T22:43:11"></HEAD ><BODY CLASS="REFENTRY" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" ><A HREF="index.html" >PostgreSQL 9.2.24 Documentation</A ></TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A TITLE="CREATE USER MAPPING" HREF="sql-createusermapping.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-commands.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="20%" ALIGN="right" VALIGN="top" ><A TITLE="DEALLOCATE" HREF="sql-deallocate.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="SQL-CREATEVIEW" ></A >CREATE VIEW</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN72647" ></A ><H2 >Name</H2 >CREATE VIEW -- define a new view</DIV ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN72652" ></A ><H2 >Synopsis</H2 ><PRE CLASS="SYNOPSIS" >CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] VIEW <TT CLASS="REPLACEABLE" ><I >name</I ></TT > [ ( <TT CLASS="REPLACEABLE" ><I >column_name</I ></TT > [, ...] ) ] [ WITH ( <TT CLASS="REPLACEABLE" ><I >view_option_name</I ></TT > [= <TT CLASS="REPLACEABLE" ><I >view_option_value</I ></TT >] [, ... ] ) ] AS <TT CLASS="REPLACEABLE" ><I >query</I ></TT ></PRE ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN72659" ></A ><H2 >Description</H2 ><P > <TT CLASS="COMMAND" >CREATE VIEW</TT > defines a view of a query. The view is not physically materialized. Instead, the query is run every time the view is referenced in a query. </P ><P > <TT CLASS="COMMAND" >CREATE OR REPLACE VIEW</TT > is similar, but if a view of the same name already exists, it is replaced. The new query must generate the same columns that were generated by the existing view query (that is, the same column names in the same order and with the same data types), but it may add additional columns to the end of the list. The calculations giving rise to the output columns may be completely different. </P ><P > If a schema name is given (for example, <TT CLASS="LITERAL" >CREATE VIEW myschema.myview ...</TT >) then the view is created in the specified schema. Otherwise it is created in the current schema. Temporary views exist in a special schema, so a schema name cannot be given when creating a temporary view. The name of the view must be distinct from the name of any other view, table, sequence, index or foreign table in the same schema. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN72667" ></A ><H2 >Parameters</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="LITERAL" >TEMPORARY</TT > or <TT CLASS="LITERAL" >TEMP</TT ></DT ><DD ><P > If specified, the view is created as a temporary view. Temporary views are automatically dropped at the end of the current session. Existing permanent relations with the same name are not visible to the current session while the temporary view exists, unless they are referenced with schema-qualified names. </P ><P > If any of the tables referenced by the view are temporary, the view is created as a temporary view (whether <TT CLASS="LITERAL" >TEMPORARY</TT > is specified or not). </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >name</I ></TT ></DT ><DD ><P > The name (optionally schema-qualified) of a view to be created. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >column_name</I ></TT ></DT ><DD ><P > An optional list of names to be used for columns of the view. If not given, the column names are deduced from the query. </P ></DD ><DT ><TT CLASS="LITERAL" >WITH ( <TT CLASS="REPLACEABLE" ><I >view_option_name</I ></TT > [= <TT CLASS="REPLACEABLE" ><I >view_option_value</I ></TT >] [, ... ] )</TT ></DT ><DD ><P > This clause specifies optional parameters for a view; currently, the only supported parameter name is <TT CLASS="LITERAL" >security_barrier</TT >, which should be enabled when a view is intended to provide row-level security. See <A HREF="rules-privileges.html" >Section 37.4</A > for full details. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >query</I ></TT ></DT ><DD ><P > A <A HREF="sql-select.html" >SELECT</A > or <A HREF="sql-values.html" >VALUES</A > command which will provide the columns and rows of the view. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN72704" ></A ><H2 >Notes</H2 ><P > Currently, views are read only: the system will not allow an insert, update, or delete on a view. You can get the effect of an updatable view by creating <TT CLASS="LITERAL" >INSTEAD</TT > triggers on the view, which must convert attempted inserts, etc. on the view into appropriate actions on other tables. For more information see <A HREF="sql-createtrigger.html" >CREATE TRIGGER</A >. Another possibility is to create rules (see <A HREF="sql-createrule.html" >CREATE RULE</A >), but in practice triggers are easier to understand and use correctly. </P ><P > Use the <A HREF="sql-dropview.html" >DROP VIEW</A > statement to drop views. </P ><P > Be careful that the names and types of the view's columns will be assigned the way you want. For example: </P><PRE CLASS="PROGRAMLISTING" >CREATE VIEW vista AS SELECT 'Hello World';</PRE ><P> is bad form in two ways: the column name defaults to <TT CLASS="LITERAL" >?column?</TT >, and the column data type defaults to <TT CLASS="TYPE" >unknown</TT >. If you want a string literal in a view's result, use something like: </P><PRE CLASS="PROGRAMLISTING" >CREATE VIEW vista AS SELECT text 'Hello World' AS hello;</PRE ><P> </P ><P > Access to tables referenced in the view is determined by permissions of the view owner. In some cases, this can be used to provide secure but restricted access to the underlying tables. However, not all views are secure against tampering; see <A HREF="rules-privileges.html" >Section 37.4</A > for details. Functions called in the view are treated the same as if they had been called directly from the query using the view. Therefore the user of a view must have permissions to call all functions used by the view. </P ><P > When <TT CLASS="COMMAND" >CREATE OR REPLACE VIEW</TT > is used on an existing view, only the view's defining SELECT rule is changed. Other view properties, including ownership, permissions, and non-SELECT rules, remain unchanged. You must own the view to replace it (this includes being a member of the owning role). </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN72721" ></A ><H2 >Examples</H2 ><P > Create a view consisting of all comedy films: </P><PRE CLASS="PROGRAMLISTING" >CREATE VIEW comedies AS SELECT * FROM films WHERE kind = 'Comedy';</PRE ><P> This will create a view containing the columns that are in the <TT CLASS="LITERAL" >film</TT > table at the time of view creation. Though <TT CLASS="LITERAL" >*</TT > was used to create the view, columns added later to the table will not be part of the view. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN72727" ></A ><H2 >Compatibility</H2 ><P > The SQL standard specifies some additional capabilities for the <TT CLASS="COMMAND" >CREATE VIEW</TT > statement: </P><PRE CLASS="SYNOPSIS" >CREATE VIEW <TT CLASS="REPLACEABLE" ><I >name</I ></TT > [ ( <TT CLASS="REPLACEABLE" ><I >column_name</I ></TT > [, ...] ) ] AS <TT CLASS="REPLACEABLE" ><I >query</I ></TT > [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]</PRE ><P> </P ><P > The optional clauses for the full SQL command are: <P ></P ></P><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="LITERAL" >CHECK OPTION</TT ></DT ><DD ><P > This option has to do with updatable views. All <TT CLASS="COMMAND" >INSERT</TT > and <TT CLASS="COMMAND" >UPDATE</TT > commands on the view will be checked to ensure data satisfy the view-defining condition (that is, the new data would be visible through the view). If they do not, the update will be rejected. </P ></DD ><DT ><TT CLASS="LITERAL" >LOCAL</TT ></DT ><DD ><P > Check for integrity on this view. </P ></DD ><DT ><TT CLASS="LITERAL" >CASCADED</TT ></DT ><DD ><P > Check for integrity on this view and on any dependent view. <TT CLASS="LITERAL" >CASCADED</TT > is assumed if neither <TT CLASS="LITERAL" >CASCADED</TT > nor <TT CLASS="LITERAL" >LOCAL</TT > is specified. </P ></DD ></DL ></DIV ><P> </P ><P > <TT CLASS="COMMAND" >CREATE OR REPLACE VIEW</TT > is a <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > language extension. So is the concept of a temporary view. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN72760" ></A ><H2 >See Also</H2 ><A HREF="sql-alterview.html" >ALTER VIEW</A >, <A HREF="sql-dropview.html" >DROP VIEW</A ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="sql-createusermapping.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="sql-deallocate.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >CREATE USER MAPPING</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="sql-commands.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >DEALLOCATE</TD ></TR ></TABLE ></DIV ></BODY ></HTML >