Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > 977b9e43ddbf791a68788d984b14383d > files > 1027

postgresql9.3-docs-9.3.9-1.mga4.noarch.rpm

<!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.3.9 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="2015-06-13T20:07: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"
><A
HREF="index.html"
>PostgreSQL 9.3.9 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="AEN74926"
></A
><H2
>Name</H2
>CREATE VIEW&nbsp;--&nbsp;define a new view</DIV
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN74931"
></A
><H2
>Synopsis</H2
><PRE
CLASS="SYNOPSIS"
>CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] [ RECURSIVE ] 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="AEN74938"
></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="AEN74946"
></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="LITERAL"
>RECURSIVE</TT
></DT
><DD
><P
>      Creates a recursive view.  The syntax
</P><PRE
CLASS="SYNOPSIS"
>CREATE RECURSIVE VIEW <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> (<TT
CLASS="REPLACEABLE"
><I
>columns</I
></TT
>) AS SELECT <TT
CLASS="REPLACEABLE"
><I
>...</I
></TT
>;</PRE
><P>
      is equivalent to
</P><PRE
CLASS="SYNOPSIS"
>CREATE VIEW <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> AS WITH RECURSIVE <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> (<TT
CLASS="REPLACEABLE"
><I
>columns</I
></TT
>) AS (SELECT <TT
CLASS="REPLACEABLE"
><I
>...</I
></TT
>) SELECT <TT
CLASS="REPLACEABLE"
><I
>columns</I
></TT
> FROM <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
>;</PRE
><P>
      A view column list must be specified for a recursive view.
     </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 38.5</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="AEN74999"
></A
><H2
>Notes</H2
><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 38.5</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
CLASS="REFSECT2"
><A
NAME="SQL-CREATEVIEW-UPDATABLE-VIEWS"
></A
><H3
>Updatable Views</H3
><P
>    Simple views are automatically updatable: the system will allow
    <TT
CLASS="COMMAND"
>INSERT</TT
>, <TT
CLASS="COMMAND"
>UPDATE</TT
> and <TT
CLASS="COMMAND"
>DELETE</TT
> statements
    to be used on the view in the same way as on a regular table.  A view is
    automatically updatable if it satisfies all of the following conditions:

    <P
></P
></P><UL
><LI
><P
>       The view must have exactly one entry in its <TT
CLASS="LITERAL"
>FROM</TT
> list,
       which must be a table or another updatable view.
      </P
></LI
><LI
><P
>       The view definition must not contain <TT
CLASS="LITERAL"
>WITH</TT
>,
       <TT
CLASS="LITERAL"
>DISTINCT</TT
>, <TT
CLASS="LITERAL"
>GROUP BY</TT
>, <TT
CLASS="LITERAL"
>HAVING</TT
>,
       <TT
CLASS="LITERAL"
>LIMIT</TT
>, or <TT
CLASS="LITERAL"
>OFFSET</TT
> clauses at the top level.
      </P
></LI
><LI
><P
>       The view definition must not contain set operations (<TT
CLASS="LITERAL"
>UNION</TT
>,
       <TT
CLASS="LITERAL"
>INTERSECT</TT
> or <TT
CLASS="LITERAL"
>EXCEPT</TT
>) at the top level.
      </P
></LI
><LI
><P
>       All columns in the view's select list must be simple references to
       columns of the underlying relation.  They cannot be expressions,
       literals or functions.  System columns cannot be referenced, either.
      </P
></LI
><LI
><P
>       No column of the underlying relation can appear more than once in
       the view's select list.
      </P
></LI
><LI
><P
>       The view must not have the <TT
CLASS="LITERAL"
>security_barrier</TT
> property.
      </P
></LI
></UL
><P>
   </P
><P
>    If the view is automatically updatable the system will convert any
    <TT
CLASS="COMMAND"
>INSERT</TT
>, <TT
CLASS="COMMAND"
>UPDATE</TT
> or <TT
CLASS="COMMAND"
>DELETE</TT
> statement
    on the view into the corresponding statement on the underlying base
    relation.
   </P
><P
>    If an automatically updatable view contains a <TT
CLASS="LITERAL"
>WHERE</TT
>
    condition, the condition restricts which rows of the base relation are
    available to be modified by <TT
CLASS="COMMAND"
>UPDATE</TT
> and <TT
CLASS="COMMAND"
>DELETE</TT
>
    statements on the view.  However, an <TT
CLASS="COMMAND"
>UPDATE</TT
> is allowed to
    change a row so that it no longer satisfies the <TT
CLASS="LITERAL"
>WHERE</TT
>
    condition, and thus is no longer visible through the view.  Similarly,
    an <TT
CLASS="COMMAND"
>INSERT</TT
> command can potentially insert base-relation rows
    that do not satisfy the <TT
CLASS="LITERAL"
>WHERE</TT
> condition and thus are not
    visible through the view.
   </P
><P
>    A more complex view that does not satisfy all these conditions is
    read-only by default: the system will not allow an insert, update, or
    delete on the view.  You can get the effect of an updatable view by
    creating <TT
CLASS="LITERAL"
>INSTEAD OF</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
>    Note that the user performing the insert, update or delete on the view
    must have the corresponding insert, update or delete privilege on the
    view.  In addition the view's owner must have the relevant privileges on
    the underlying base relations, but the user performing the update does
    not need any permissions on the underlying base relations (see
    <A
HREF="rules-privileges.html"
>Section 38.5</A
>).
   </P
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN75062"
></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
><P
>   Create a recursive view consisting of the numbers from 1 to 100:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE RECURSIVE VIEW nums_1_100 (n) AS
    VALUES (1)
UNION ALL
    SELECT n+1 FROM nums_1_100 WHERE n &#60; 100;</PRE
><P></P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN75070"
></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 controls the behavior of automatically updatable views.
        When given, <TT
CLASS="COMMAND"
>INSERT</TT
> and <TT
CLASS="COMMAND"
>UPDATE</TT
> commands on
        the view will be checked to ensure new rows satisfy the
        view-defining condition (that is, the new rows would be visible
        through the view). If they do not, the update will be rejected.
        Without <TT
CLASS="LITERAL"
>CHECK OPTION</TT
>, <TT
CLASS="COMMAND"
>INSERT</TT
> and
        <TT
CLASS="COMMAND"
>UPDATE</TT
> commands on the view are allowed to create rows
        that are not visible through the view.  (The latter behavior is the
        only one currently provided by <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>.)
       </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.
   The <TT
CLASS="LITERAL"
>WITH</TT
> clause is an extension as well.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN75108"
></A
><H2
>See Also</H2
><A
HREF="sql-alterview.html"
>ALTER VIEW</A
>, <A
HREF="sql-dropview.html"
>DROP VIEW</A
>, <A
HREF="sql-creatematerializedview.html"
>CREATE MATERIALIZED 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
>