⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 update.7

📁 PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开发团队说,该版本将加速更多企业向该数据库移植.核心开发成员之一Bruce Momjian表示,在新版PostgreSQL
💻 7
字号:
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "UPDATE" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMEUPDATE \- update rows of a table.SH SYNOPSIS.sp.nfUPDATE [ ONLY ] \fItable\fR [ [ AS ] \fIalias\fR ]    SET { \fIcolumn\fR = { \fIexpression\fR | DEFAULT } |          ( \fIcolumn\fR [, ...] ) = ( { \fIexpression\fR | DEFAULT } [, ...] ) } [, ...]    [ FROM \fIfromlist\fR ]    [ WHERE \fIcondition\fR ]    [ RETURNING * | \fIoutput_expression\fR [ AS \fIoutput_name\fR ] [, ...] ].sp.fi.SH "DESCRIPTION".PP\fBUPDATE\fR changes the values of the specifiedcolumns in all rows that satisfy the condition. Only the columns tobe modified need be mentioned in the SET clause;columns not explicitly modified retain their previous values..PPBy default, \fBUPDATE\fR will update rows in thespecified table and all its subtables. If you wish to only updatethe specific table mentioned, you must use the ONLYclause..PPThere are two ways to modify a table using information contained inother tables in the database: using sub-selects, or specifyingadditional tables in the FROM clause. Whichtechnique is more appropriate depends on the specificcircumstances..PPThe optional RETURNING clause causes \fBUPDATE\fRto compute and return value(s) based on each row actually updated.Any expression using the table's columns, and/or columns of othertables mentioned in FROM, can be computed.The new (post-update) values of the table's columns are used.The syntax of the RETURNING list is identical to that of theoutput list of \fBSELECT\fR..PPYou must have the UPDATE privilege on the tableto update it, as well as the SELECTprivilege to any table whose values are read in the\fIexpressions\fR or\fIcondition\fR..SH "PARAMETERS".TP\fB\fItable\fB\fRThe name (optionally schema-qualified) of the table to update..TP\fB\fIalias\fB\fRA substitute name for the target table. When an alias isprovided, it completely hides the actual name of the table. Forexample, given UPDATE foo AS f, the remainder of the\fBUPDATE\fR statement must refer to this table asf not foo..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. Do not include the table's name in thespecification of a target column \(em for example,UPDATE tab SET tab.col = 1 is invalid..TP\fB\fIexpression\fB\fRAn expression to assign to the column. The expression may use theold values of this and other columns in the table..TP\fBDEFAULT\fRSet the column to its default value (which will be NULL if nospecific default expression has been assigned to it)..TP\fB\fIfromlist\fB\fRA list of table expressions, allowing columns from other tablesto appear in the WHERE condition and the updateexpressions. This is similar to the list of tables that can bespecified in the FROM Clause [\fBselect\fR(7)] of a \fBSELECT\fRstatement. Note that the target table must not appear in the\fIfromlist\fR, unless you intend a self-join (in whichcase it must appear with an alias in the \fIfromlist\fR)..TP\fB\fIcondition\fB\fRAn expression that returns a value of type \fBboolean\fR.Only rows for which this expression returns truewill be updated..TP\fB\fIoutput_expression\fB\fRAn expression to be computed and returned by the \fBUPDATE\fRcommand after each row is updated. The expression may use anycolumn names of the \fItable\fRor table(s) listed in FROM.Write * to return all columns..TP\fB\fIoutput_name\fB\fRA name to use for a returned column..SH "OUTPUTS".PPOn successful completion, an \fBUPDATE\fR command returns a commandtag of the form.sp.nfUPDATE \fIcount\fR.sp.fiThe \fIcount\fR is the numberof rows updated. If \fIcount\fR is0, no rows matched the \fIcondition\fR (this is not consideredan error)..PPIf the \fBUPDATE\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) updated by thecommand..SH "NOTES".PPWhen a FROM clause is present, what essentially happensis that the target table is joined to the tables mentioned in the\fIfromlist\fR, and each output row of the joinrepresents an update operation for the target table. When usingFROM you should ensure that the joinproduces at most one output row for each row to be modified. Inother words, a target row shouldn't join to more than one row fromthe other table(s). If it does, then only one of the join rowswill be used to update the target row, but which one will be usedis not readily predictable..PPBecause of this indeterminacy, referencing other tables only withinsub-selects is safer, though often harder to read and slower thanusing a join..SH "EXAMPLES".PPChange the word Drama to Dramatic in thecolumn \fBkind\fR of the table \fBfilms\fR:.sp.nfUPDATE films SET kind = 'Dramatic' WHERE kind = 'Drama';.sp.fi.PPAdjust temperature entries and reset precipitation to its defaultvalue in one row of the table \fBweather\fR:.sp.nfUPDATE weather SET temp_lo = temp_lo+1, temp_hi = temp_lo+15, prcp = DEFAULT  WHERE city = 'San Francisco' AND date = '2003-07-03';.sp.fi.PPPerform the same operation and return the updated entries:.sp.nfUPDATE weather SET temp_lo = temp_lo+1, temp_hi = temp_lo+15, prcp = DEFAULT  WHERE city = 'San Francisco' AND date = '2003-07-03'  RETURNING temp_lo, temp_hi, prcp;.sp.fi.PPUse the alternative column-list syntax to do the same update:.sp.nfUPDATE weather SET (temp_lo, temp_hi, prcp) = (temp_lo+1, temp_lo+15, DEFAULT)  WHERE city = 'San Francisco' AND date = '2003-07-03';.sp.fi.PPIncrement the sales count of the salesperson who manages theaccount for Acme Corporation, using the FROMclause syntax:.sp.nfUPDATE employees SET sales_count = sales_count + 1 FROM accounts  WHERE accounts.name = 'Acme Corporation'  AND employees.id = accounts.sales_person;.sp.fi.PPPerform the same operation, using a sub-select in theWHERE clause:.sp.nfUPDATE employees SET sales_count = sales_count + 1 WHERE id =  (SELECT sales_person FROM accounts WHERE name = 'Acme Corporation');.sp.fi.PPAttempt to insert a new stock item along with the quantity of stock. Ifthe item already exists, instead update the stock count of the existingitem. To do this without failing the entire transaction, use savepoints..sp.nfBEGIN;-- other operationsSAVEPOINT sp1;INSERT INTO wines VALUES('Chateau Lafite 2003', '24');-- Assume the above fails because of a unique key violation,-- so now we issue these commands:ROLLBACK TO sp1;UPDATE wines SET stock = stock + 24 WHERE winename = 'Chateau Lafite 2003';-- continue with other operations, and eventuallyCOMMIT;.sp.fi.SH "COMPATIBILITY".PPThis command conforms to the SQL standard, exceptthat the FROM and RETURNING clausesare PostgreSQL extensions..PPAccording to the standard, the column-list syntax should allow a listof columns to be assigned from a single row-valued expression, suchas a sub-select:.sp.nfUPDATE accounts SET (contact_last_name, contact_first_name) =    (SELECT last_name, first_name FROM salesmen     WHERE salesmen.id = accounts.sales_id);.sp.fiThis is not currently implemented \(em the source must be a listof independent expressions..PPSome other database systems offer a FROM option in whichthe target table is supposed to be listed again within FROM.That is not how PostgreSQL interpretsFROM. Be careful when porting applications that use thisextension.

⌨️ 快捷键说明

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