<!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 8.0.11 Documentation" HREF="index.html"><LINK REL="UP" TITLE="SQL Commands" HREF="sql-commands.html"><LINK REL="PREVIOUS" TITLE="CREATE USER" HREF="sql-createuser.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="2007-02-02T03:57:22"></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" >PostgreSQL 8.0.11 Documentation</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-createuser.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-createuser.html" >Fast Backward</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="sql-deallocate.html" >Fast Forward</A ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A 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="AEN41532" ></A ><H2 >Name</H2 >CREATE VIEW -- define a new view</DIV ><A NAME="AEN41535" ></A ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN41537" ></A ><H2 >Synopsis</H2 ><PRE CLASS="SYNOPSIS" >CREATE [ OR REPLACE ] VIEW <TT CLASS="REPLACEABLE" ><I >name</I ></TT > [ ( <TT CLASS="REPLACEABLE" ><I >column_name</I ></TT > [, ...] ) ] AS <TT CLASS="REPLACEABLE" ><I >query</I ></TT ></PRE ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN41542" ></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. You can only replace a view with a new query that generates the identical set of columns (i.e., same column names and data types). </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. The view name must be distinct from the name of any other view, table, sequence, or index in the same schema. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN41550" ></A ><H2 >Parameters</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><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="REPLACEABLE" ><I >query</I ></TT ></DT ><DD ><P > A query (that is, a <TT CLASS="COMMAND" >SELECT</TT > statement) which will provide the columns and rows of the view. </P ><P > Refer to <A HREF="sql-select.html" ><I >SELECT</I ></A > for more information about valid queries. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN41571" ></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 rules that rewrite inserts, etc. on the view into appropriate actions on other tables. For more information see <A HREF="sql-createrule.html" ><I >CREATE RULE</I ></A >. </P ><P > Use the <TT CLASS="COMMAND" >DROP VIEW</TT > 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. However, 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 ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN41583" ></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> </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN41587" ></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</I ></TT > [, ...] ) ] AS query [ WITH [ CASCADE | 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" >CASCADE</TT ></DT ><DD ><P > Check for integrity on this view and on any dependent view. <TT CLASS="LITERAL" >CASCADE</TT > is assumed if neither <TT CLASS="LITERAL" >CASCADE</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. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN41619" ></A ><H2 >See Also</H2 ><A HREF="sql-dropview.html" ><I >DROP VIEW</I ></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-createuser.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</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 >