Server IP : 103.119.228.120 / Your IP : 18.221.167.11 Web Server : Apache System : Linux v8.techscape8.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64 User : nobody ( 99) PHP Version : 5.6.40 Disable Function : shell_exec,symlink,system,exec,proc_get_status,proc_nice,proc_terminate,define_syslog_variables,syslog,openlog,closelog,escapeshellcmd,passthru,ocinum cols,ini_alter,leak,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dl,dll,myshellexec,proc_open,socket_bind,proc_close,escapeshellarg,parse_ini_filepopen,fpassthru,exec,passthru,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,popen,show_source,proc_nice,proc_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,dl,symlink,shell_exec,system,dl,passthru,escapeshellarg,escapeshellcmd,myshellexec,c99_buff_prepare,c99_sess_put,fpassthru,getdisfunc,fx29exec,fx29exec2,is_windows,disp_freespace,fx29sh_getupdate,fx29_buff_prepare,fx29_sess_put,fx29shexit,fx29fsearch,fx29ftpbrutecheck,fx29sh_tools,fx29sh_about,milw0rm,imagez,sh_name,myshellexec,checkproxyhost,dosyayicek,c99_buff_prepare,c99_sess_put,c99getsource,c99sh_getupdate,c99fsearch,c99shexit,view_perms,posix_getpwuid,posix_getgrgid,posix_kill,parse_perms,parsesort,view_perms_color,set_encoder_input,ls_setcheckboxall,ls_reverse_all,rsg_read,rsg_glob,selfURL,dispsecinfo,unix2DosTime,addFile,system,get_users,view_size,DirFiles,DirFilesWide,DirPrintHTMLHeaders,GetFilesTotal,GetTitles,GetTimeTotal,GetMatchesCount,GetFileMatchesCount,GetResultFiles,fs_copy_dir,fs_copy_obj,fs_move_dir,fs_move_obj,fs_rmdir,SearchText,getmicrotime MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/local/ssl/local/ssl/local/ssl/share/doc/postgresql-9.2.24/html/ |
Upload File : |
<!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 9.2.24 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="2017-11-06T22:43: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" ><A HREF="index.html" >PostgreSQL 9.2.24 Documentation</A ></TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A TITLE="GRANT" HREF="sql-grant.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="LISTEN" 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="AEN76049" ></A ><H2 >Name</H2 >INSERT -- create new rows in a table</DIV ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN76054" ></A ><H2 >Synopsis</H2 ><PRE CLASS="SYNOPSIS" >[ WITH [ RECURSIVE ] <TT CLASS="REPLACEABLE" ><I >with_query</I ></TT > [, ...] ] INSERT INTO <TT CLASS="REPLACEABLE" ><I >table_name</I ></TT > [ ( <TT CLASS="REPLACEABLE" ><I >column_name</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="AEN76063" ></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. If a column list is specified, you only need <TT CLASS="LITERAL" >INSERT</TT > privilege on the listed columns. Use of the <TT CLASS="LITERAL" >RETURNING</TT > clause requires <TT CLASS="LITERAL" >SELECT</TT > privilege on all columns mentioned in <TT CLASS="LITERAL" >RETURNING</TT >. If you use the <TT CLASS="REPLACEABLE" ><I >query</I ></TT > clause to insert rows from a query, you of course need to have <TT CLASS="LITERAL" >SELECT</TT > privilege on any table or column used in the query. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN76089" ></A ><H2 >Parameters</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="REPLACEABLE" ><I >with_query</I ></TT ></DT ><DD ><P > The <TT CLASS="LITERAL" >WITH</TT > clause allows you to specify one or more subqueries that can be referenced by name in the <TT CLASS="COMMAND" >INSERT</TT > query. See <A HREF="queries-with.html" >Section 7.8</A > and <A HREF="sql-select.html" >SELECT</A > for details. </P ><P > It is possible for the <TT CLASS="REPLACEABLE" ><I >query</I ></TT > (<TT CLASS="COMMAND" >SELECT</TT > statement) to also contain a <TT CLASS="LITERAL" >WITH</TT > clause. In such a case both sets of <TT CLASS="REPLACEABLE" ><I >with_query</I ></TT > can be referenced within the <TT CLASS="REPLACEABLE" ><I >query</I ></TT >, but the second one takes precedence since it is more closely nested. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >table_name</I ></TT ></DT ><DD ><P > The name (optionally schema-qualified) of an existing table. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >column_name</I ></TT ></DT ><DD ><P > The name of a column in the table named by <TT CLASS="REPLACEABLE" ><I >table_name</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 column. </P ></DD ><DT ><TT CLASS="LITERAL" >DEFAULT</TT ></DT ><DD ><P > The corresponding column 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" >SELECT</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 table named by <TT CLASS="REPLACEABLE" ><I >table_name</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="AEN76153" ></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="AEN76170" ></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 < '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 ><P > Increment the sales count of the salesperson who manages the account for Acme Corporation, and record the whole updated row along with current time in a log table: </P><PRE CLASS="PROGRAMLISTING" >WITH upd AS ( UPDATE employees SET sales_count = sales_count + 1 WHERE id = (SELECT sales_person FROM accounts WHERE name = 'Acme Corporation') RETURNING * ) INSERT INTO employees_log SELECT *, current_timestamp FROM upd;</PRE ><P></P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN76199" ></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, as is the ability to use <TT CLASS="LITERAL" >WITH</TT > with <TT CLASS="COMMAND" >INSERT</TT >. 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" >SELECT</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 >