Sophie

Sophie

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

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
>INSERT</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="GRANT"
HREF="sql-grant.html"><LINK
REL="NEXT"
TITLE="LISTEN"
HREF="sql-listen.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-grant.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-grant.html"
>Fast Backward</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-listen.html"
>Fast Forward</A
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-listen.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="SQL-INSERT"
></A
>INSERT</H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN58173"
></A
><H2
>Name</H2
>INSERT&nbsp;--&nbsp;create new rows in a table</DIV
><A
NAME="AEN58176"
></A
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN58178"
></A
><H2
>Synopsis</H2
><PRE
CLASS="SYNOPSIS"
>INSERT INTO <TT
CLASS="REPLACEABLE"
><I
>table</I
></TT
> [ ( <TT
CLASS="REPLACEABLE"
><I
>column</I
></TT
> [, ...] ) ]
    { DEFAULT VALUES | VALUES ( { <TT
CLASS="REPLACEABLE"
><I
>expression</I
></TT
> | DEFAULT } [, ...] ) [, ...] | <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
> }
    [ RETURNING * | <TT
CLASS="REPLACEABLE"
><I
>output_expression</I
></TT
> [ AS <TT
CLASS="REPLACEABLE"
><I
>output_name</I
></TT
> ] [, ...] ]</PRE
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN58186"
></A
><H2
>Description</H2
><P
>   <TT
CLASS="COMMAND"
>INSERT</TT
> inserts new rows into a table.
   One can insert one or more rows specified by value expressions,
   or zero or more rows resulting from a query.
  </P
><P
>   The target column names can be listed in any order.  If no list of
   column names is given at all, the default is all the columns of the
   table in their declared order; or the first <TT
CLASS="REPLACEABLE"
><I
>N</I
></TT
> column
   names, if there are only <TT
CLASS="REPLACEABLE"
><I
>N</I
></TT
> columns supplied by the
   <TT
CLASS="LITERAL"
>VALUES</TT
> clause or <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
>.  The values
   supplied by the <TT
CLASS="LITERAL"
>VALUES</TT
> clause or <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
> are
   associated with the explicit or implicit column list left-to-right.
  </P
><P
>   Each column not present in the explicit or implicit column list will be
   filled with a default value, either its declared default value
   or null if there is none.
  </P
><P
>   If the expression for any column is not of the correct data type,
   automatic type conversion will be attempted.
  </P
><P
>   The optional <TT
CLASS="LITERAL"
>RETURNING</TT
> clause causes <TT
CLASS="COMMAND"
>INSERT</TT
>
   to compute and return value(s) based on each row actually inserted.
   This is primarily useful for obtaining values that were supplied by
   defaults, such as a serial sequence number.  However, any expression
   using the table's columns is allowed.  The syntax of the
   <TT
CLASS="LITERAL"
>RETURNING</TT
> list is identical to that of the output list
   of <TT
CLASS="COMMAND"
>SELECT</TT
>.
  </P
><P
>   You must have <TT
CLASS="LITERAL"
>INSERT</TT
> privilege on a table in
   order to insert into it, and <TT
CLASS="LITERAL"
>SELECT</TT
> privilege on it to
   use <TT
CLASS="LITERAL"
>RETURNING</TT
>.  If you use the <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
> clause to insert rows from a
   query, you also need to have <TT
CLASS="LITERAL"
>SELECT</TT
> privilege on
   any table used in the query.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN58210"
></A
><H2
>Parameters</H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="REPLACEABLE"
><I
>table</I
></TT
></DT
><DD
><P
>      The name (optionally schema-qualified) of an existing table.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>column</I
></TT
></DT
><DD
><P
>      The name of a column in <TT
CLASS="REPLACEABLE"
><I
>table</I
></TT
>.
      The column name can be qualified with a subfield name or array
      subscript, if needed.  (Inserting into only some fields of a
      composite column leaves the other fields null.)
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>DEFAULT VALUES</TT
></DT
><DD
><P
>      All columns will be filled with their default values.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>expression</I
></TT
></DT
><DD
><P
>      An expression or value to assign to the corresponding <TT
CLASS="REPLACEABLE"
><I
>column</I
></TT
>.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>DEFAULT</TT
></DT
><DD
><P
>      The corresponding <TT
CLASS="REPLACEABLE"
><I
>column</I
></TT
> will be filled with
      its default value.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
></DT
><DD
><P
>      A query (<TT
CLASS="COMMAND"
>SELECT</TT
> statement) that supplies the
      rows to be inserted.  Refer to the
      <A
HREF="sql-select.html"
><I
>SELECT</I
></A
>
      statement for a description of the syntax.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>output_expression</I
></TT
></DT
><DD
><P
>      An expression to be computed and returned by the <TT
CLASS="COMMAND"
>INSERT</TT
>
      command after each row is inserted.  The expression can use any
      column names of the <TT
CLASS="REPLACEABLE"
><I
>table</I
></TT
>.
      Write <TT
CLASS="LITERAL"
>*</TT
> to return all columns of the inserted row(s).
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>output_name</I
></TT
></DT
><DD
><P
>      A name to use for a returned column.
     </P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN58261"
></A
><H2
>Outputs</H2
><P
>   On successful completion, an <TT
CLASS="COMMAND"
>INSERT</TT
> command returns a command
   tag of the form
</P><PRE
CLASS="SCREEN"
>INSERT <TT
CLASS="REPLACEABLE"
><I
>oid</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>count</I
></TT
></PRE
><P>
   The <TT
CLASS="REPLACEABLE"
><I
>count</I
></TT
> is the number
   of rows inserted.  If <TT
CLASS="REPLACEABLE"
><I
>count</I
></TT
>
   is exactly one, and the target table has OIDs, then
   <TT
CLASS="REPLACEABLE"
><I
>oid</I
></TT
> is the
   <ACRONYM
CLASS="ACRONYM"
>OID</ACRONYM
> assigned to the inserted row.  Otherwise
   <TT
CLASS="REPLACEABLE"
><I
>oid</I
></TT
> is zero.
  </P
><P
>   If the <TT
CLASS="COMMAND"
>INSERT</TT
> command contains a <TT
CLASS="LITERAL"
>RETURNING</TT
>
   clause, the result will be similar to that of a <TT
CLASS="COMMAND"
>SELECT</TT
>
   statement containing the columns and values defined in the
   <TT
CLASS="LITERAL"
>RETURNING</TT
> list, computed over the row(s) inserted by the
   command.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN58278"
></A
><H2
>Examples</H2
><P
>   Insert a single row into table <TT
CLASS="LITERAL"
>films</TT
>:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO films VALUES
    ('UA502', 'Bananas', 105, '1971-07-13', 'Comedy', '82 minutes');</PRE
><P>
  </P
><P
>   In this example, the <TT
CLASS="LITERAL"
>len</TT
> column is
   omitted and therefore it will have the default value:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO films (code, title, did, date_prod, kind)
    VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');</PRE
><P>
  </P
><P
>   This example uses the <TT
CLASS="LITERAL"
>DEFAULT</TT
> clause for
   the date columns rather than specifying a value:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO films VALUES
    ('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes');
INSERT INTO films (code, title, did, date_prod, kind)
    VALUES ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama');</PRE
><P>
  </P
><P
>   To insert a row consisting entirely of default values:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO films DEFAULT VALUES;</PRE
><P>
  </P
><P
>   To insert multiple rows using the multirow <TT
CLASS="COMMAND"
>VALUES</TT
> syntax:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO films (code, title, did, date_prod, kind) VALUES
    ('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'),
    ('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy');</PRE
><P>
  </P
><P
>   This example inserts some rows into table
   <TT
CLASS="LITERAL"
>films</TT
> from a table <TT
CLASS="LITERAL"
>tmp_films</TT
>
   with the same column layout as <TT
CLASS="LITERAL"
>films</TT
>:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO films SELECT * FROM tmp_films WHERE date_prod &lt; '2004-05-07';</PRE
><P>
  </P
><P
>   This example inserts into array columns:

</P><PRE
CLASS="PROGRAMLISTING"
>-- Create an empty 3x3 gameboard for noughts-and-crosses
INSERT INTO tictactoe (game, board[1:3][1:3])
    VALUES (1, '{{" "," "," "},{" "," "," "},{" "," "," "}}');
-- The subscripts in the above example aren't really needed
INSERT INTO tictactoe (game, board)
    VALUES (2, '{{X," "," "},{" ",O," "},{" ",X," "}}');</PRE
><P>
  </P
><P
>   Insert a single row into table <TT
CLASS="LITERAL"
>distributors</TT
>, returning
   the sequence number generated by the <TT
CLASS="LITERAL"
>DEFAULT</TT
> clause:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
   RETURNING did;</PRE
><P>
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN58305"
></A
><H2
>Compatibility</H2
><P
>   <TT
CLASS="COMMAND"
>INSERT</TT
> conforms to the SQL standard, except that
   the <TT
CLASS="LITERAL"
>RETURNING</TT
> clause is a
   <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> extension.  Also, the case in
   which a column name list is omitted, but not all the columns are
   filled from the <TT
CLASS="LITERAL"
>VALUES</TT
> clause or <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
>,
   is disallowed by the standard.
  </P
><P
>   Possible limitations of the <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
> clause are documented under
   <A
HREF="sql-select.html"
><I
>SELECT</I
></A
>.
  </P
></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-grant.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-listen.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>GRANT</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"
>LISTEN</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>