<!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.0.11 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="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-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="AEN43566" ></A ><H2 >Name</H2 >INSERT -- create new rows in a table</DIV ><A NAME="AEN43569" ></A ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN43571" ></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 > }</PRE ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN43577" ></A ><H2 >Description</H2 ><P > <TT CLASS="COMMAND" >INSERT</TT > inserts new rows into a table. One can insert a single row specified by value expressions, or several rows as a result of a query. </P ><P > The target column names may 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 > You must have <TT CLASS="LITERAL" >INSERT</TT > privilege to a table in order to insert into it. 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="AEN43594" ></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 <TT CLASS="COMMAND" >SELECT</TT > statement for a description of the syntax. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN43632" ></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 ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN43644" ></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 > 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 < '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 -- (these commands create the same board) INSERT INTO tictactoe (game, board[1:3][1:3]) VALUES (1,'{{"","",""},{"","",""},{"","",""}}'); INSERT INTO tictactoe (game, board) VALUES (2,'{{,,},{,,},{,,}}');</PRE ><P> </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN43664" ></A ><H2 >Compatibility</H2 ><P > <TT CLASS="COMMAND" >INSERT</TT > conforms to the SQL standard. 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 >