Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > fc62ce67f262cdcd253dc7f849ce3223 > files > 834

postgresql8.4-docs-8.4.12-0.1mdv2010.2.i586.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 8.4.12 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="2012-05-31T23:30: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"
>PostgreSQL 8.4.12 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-createusermapping.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-createusermapping.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="AEN60536"
></A
><H2
>Name</H2
>CREATE VIEW&nbsp;--&nbsp;define a new view</DIV
><A
NAME="AEN60539"
></A
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN60541"
></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
> [, ...] ) ]
    AS <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
></PRE
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN60546"
></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, or index
   in the same schema.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN60554"
></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="REPLACEABLE"
><I
>query</I
></TT
></DT
><DD
><P
>      A <A
HREF="sql-select.html"
><I
>SELECT</I
></A
> or
      <A
HREF="sql-values.html"
><I
>VALUES</I
></A
> command
      which will provide the columns and rows of the view.
     </P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN60582"
></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 <A
HREF="sql-dropview.html"
><I
>DROP VIEW</I
></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 36.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="AEN60597"
></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="AEN60601"
></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="AEN60634"
></A
><H2
>See Also</H2
><A
HREF="sql-alterview.html"
><I
>ALTER VIEW</I
></A
>, <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-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
>