insert.7

来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 190 行

7
190
字号
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "INSERT" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMEINSERT \- create new rows in a table.SH SYNOPSIS.sp.nfINSERT INTO \fItable\fR [ ( \fIcolumn\fR [, ...] ) ]    { DEFAULT VALUES | VALUES ( { \fIexpression\fR | DEFAULT } [, ...] ) [, ...] | \fIquery\fR }    [ RETURNING * | \fIoutput_expression\fR [ AS \fIoutput_name\fR ] [, ...] ].sp.fi.SH "DESCRIPTION".PP\fBINSERT\fR 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..PPThe target column names may be listed in any order. If no list ofcolumn names is given at all, the default is all the columns of thetable in their declared order; or the first \fIN\fR columnnames, if there are only \fIN\fR columns supplied by theVALUES clause or \fIquery\fR. The valuessupplied by the VALUES clause or \fIquery\fR areassociated with the explicit or implicit column list left-to-right..PPEach column not present in the explicit or implicit column list will befilled with a default value, either its declared default valueor null if there is none..PPIf the expression for any column is not of the correct data type,automatic type conversion will be attempted..PPThe optional RETURNING clause causes \fBINSERT\fRto compute and return value(s) based on each row actually inserted.This is primarily useful for obtaining values that were supplied bydefaults, such as a serial sequence number. However, any expressionusing the table's columns is allowed. The syntax of theRETURNING list is identical to that of the output listof \fBSELECT\fR..PPYou must have INSERT privilege on a table inorder to insert into it, and SELECT privilege on it touse RETURNING. If you use the \fIquery\fR clause to insert rows from aquery, you also need to have SELECT privilege onany table used in the query..SH "PARAMETERS".TP\fB\fItable\fB\fRThe name (optionally schema-qualified) of an existing table..TP\fB\fIcolumn\fB\fRThe name of a column in \fItable\fR.The column name can be qualified with a subfield name or arraysubscript, if needed. (Inserting into only some fields of acomposite column leaves the other fields null.).TP\fBDEFAULT VALUES\fRAll columns will be filled with their default values..TP\fB\fIexpression\fB\fRAn expression or value to assign to the corresponding \fIcolumn\fR..TP\fBDEFAULT\fRThe corresponding \fIcolumn\fR will be filled withits default value..TP\fB\fIquery\fB\fRA query (\fBSELECT\fR statement) that supplies therows to be inserted. Refer to theSELECT [\fBselect\fR(7)]statement for a description of the syntax..TP\fB\fIoutput_expression\fB\fRAn expression to be computed and returned by the \fBINSERT\fRcommand after each row is inserted. The expression may use anycolumn names of the \fItable\fR.Write * to return all columns of the inserted row(s)..TP\fB\fIoutput_name\fB\fRA name to use for a returned column..SH "OUTPUTS".PPOn successful completion, an \fBINSERT\fR command returns a commandtag of the form.sp.nfINSERT \fIoid\fR \fIcount\fR.sp.fiThe \fIcount\fR is the numberof rows inserted. If \fIcount\fRis exactly one, and the target table has OIDs, then\fIoid\fR is theOID assigned to the inserted row. Otherwise\fIoid\fR is zero..PPIf the \fBINSERT\fR command contains a RETURNINGclause, the result will be similar to that of a \fBSELECT\fRstatement containing the columns and values defined in theRETURNING list, computed over the row(s) inserted by thecommand..SH "EXAMPLES".PPInsert a single row into table films:.sp.nfINSERT INTO films VALUES    ('UA502', 'Bananas', 105, '1971-07-13', 'Comedy', '82 minutes');.sp.fi.PPIn this example, the len column isomitted and therefore it will have the default value:.sp.nfINSERT INTO films (code, title, did, date_prod, kind)    VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');.sp.fi.PPThis example uses the DEFAULT clause forthe date columns rather than specifying a value:.sp.nfINSERT 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');.sp.fi.PPTo insert a row consisting entirely of default values:.sp.nfINSERT INTO films DEFAULT VALUES;.sp.fi.PPTo insert multiple rows using the multirow \fBVALUES\fR syntax:.sp.nfINSERT INTO films (code, title, did, date_prod, kind) VALUES    ('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'),    ('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy');.sp.fi.PPThis example inserts some rows into tablefilms from a table tmp_filmswith the same column layout as films:.sp.nfINSERT INTO films SELECT * FROM tmp_films WHERE date_prod < '2004-05-07';.sp.fi.PPThis example inserts into array columns:.sp.nf-- Create an empty 3x3 gameboard for noughts-and-crossesINSERT INTO tictactoe (game, board[1:3][1:3])    VALUES (1, '{{" "," "," "},{" "," "," "},{" "," "," "}}');-- The subscripts in the above example aren't really neededINSERT INTO tictactoe (game, board)    VALUES (2, '{{X," "," "},{" ",O," "},{" ",X," "}}');.sp.fi.PPInsert a single row into table distributors, returningthe sequence number generated by the DEFAULT clause:.sp.nfINSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')   RETURNING did;.sp.fi.SH "COMPATIBILITY".PP\fBINSERT\fR conforms to the SQL standard, except thatthe RETURNING clause is aPostgreSQL extension. Also, the case inwhich a column name list is omitted, but not all the columns arefilled from the VALUES clause or \fIquery\fR,is disallowed by the standard..PPPossible limitations of the \fIquery\fR clause are documented underSELECT [\fBselect\fR(7)].

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?