Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > media > main-testing > by-pkgid > bab02a23fa9f3df8d66a9a3231b50245 > files > 756

postgresql8.3-docs-8.3.6-2mdv2008.1.x86_64.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.3.6 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="2009-02-03T04:34:16"></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.3.6 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="AEN55431"
></A
><H2
>Name</H2
>CREATE VIEW&nbsp;--&nbsp;define a new view</DIV
><A
NAME="AEN55434"
></A
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN55436"
></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="AEN55441"
></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.  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="AEN55449"
></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="AEN55477"
></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.  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="AEN55489"
></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="AEN55493"
></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="AEN55526"
></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-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
>