delete.7
来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 161 行
7
161 行
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "DELETE" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMEDELETE \- delete rows of a table.SH SYNOPSIS.sp.nfDELETE FROM [ ONLY ] \fItable\fR [ [ AS ] \fIalias\fR ] [ USING \fIusinglist\fR ] [ WHERE \fIcondition\fR ] [ RETURNING * | \fIoutput_expression\fR [ AS \fIoutput_name\fR ] [, ...] ].sp.fi.SH "DESCRIPTION".PP\fBDELETE\fR deletes rows that satisfy theWHERE clause from the specified table. If theWHERE clause is absent, the effect is to deleteall rows in the table. The result is a valid, but empty table..sp.RS.B "Tip:"TRUNCATE [\fBtruncate\fR(7)] is aPostgreSQL extension that provides afaster mechanism to remove all rows from a table..RE.sp.PPBy default, \fBDELETE\fR will delete rows in thespecified table and all its child tables. If you wish to delete onlyfrom the specific table mentioned, you must use theONLY clause..PPThere are two ways to delete rows in a table using informationcontained in other tables in the database: using sub-selects, orspecifying additional tables in the USING clause.Which technique is more appropriate depends on the specificcircumstances..PPThe optional RETURNING clause causes \fBDELETE\fRto compute and return value(s) based on each row actually deleted.Any expression using the table's columns, and/or columns of othertables mentioned in USING, can be computed.The syntax of the RETURNING list is identical to that of theoutput list of \fBSELECT\fR..PPYou must have the DELETE privilege on the tableto delete from it, as well as the SELECTprivilege for any table in the USING clause orwhose values are read in the \fIcondition\fR..SH "PARAMETERS".TP\fBONLY\fRIf specified, delete rows from the named table only. When notspecified, any tables inheriting from the named table are also processed..TP\fB\fItable\fB\fRThe name (optionally schema-qualified) of an existing table..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 DELETE FROM foo AS f, the remainderof the \fBDELETE\fR statement must refer to thistable as f not foo..TP\fB\fIusinglist\fB\fRA list of table expressions, allowing columns from other tablesto appear in the WHERE condition. This is similarto the list of tables that can be specified in the FROM Clause [\fBselect\fR(7)] of a\fBSELECT\fR statement; for example, an alias forthe table name can be specified. Do not repeat the target tablein the \fIusinglist\fR,unless you wish to set up a self-join..TP\fB\fIcondition\fB\fRAn expression returning a value of type\fBboolean\fR, which determines the rows that are to bedeleted..TP\fB\fIoutput_expression\fB\fRAn expression to be computed and returned by the \fBDELETE\fRcommand after each row is deleted. The expression may use anycolumn names of the \fItable\fRor table(s) listed in USING.Write * to return all columns..TP\fB\fIoutput_name\fB\fRA name to use for a returned column..SH "OUTPUTS".PPOn successful completion, a \fBDELETE\fR command returns a commandtag of the form.sp.nfDELETE \fIcount\fR.sp.fiThe \fIcount\fR is the numberof rows deleted. If \fIcount\fR is0, no rows matched the \fIcondition\fR (this is not consideredan error)..PPIf the \fBDELETE\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) deleted by thecommand..SH "NOTES".PPPostgreSQL lets you reference columns ofother tables in the WHERE condition by specifying theother tables in the USING clause. For example,to delete all films produced by a given producer, one might do.sp.nfDELETE FROM films USING producers WHERE producer_id = producers.id AND producers.name = 'foo';.sp.fiWhat is essentially happening here is a join between \fBfilms\fRand \fBproducers\fR, with all successfully joined\fBfilms\fR rows being marked for deletion.This syntax is not standard. A more standard way to do it is.sp.nfDELETE FROM films WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');.sp.fiIn some cases the join style is easier to write or faster toexecute than the sub-select style..SH "EXAMPLES".PPDelete all films but musicals:.sp.nfDELETE FROM films WHERE kind <> 'Musical';.sp.fi.PPClear the table films:.sp.nfDELETE FROM films;.sp.fi.PPDelete completed tasks, returning full details of the deleted rows:.sp.nfDELETE FROM tasks WHERE status = 'DONE' RETURNING *;.sp.fi.SH "COMPATIBILITY".PPThis command conforms to the SQL standard, exceptthat the USING and RETURNING clausesare PostgreSQL extensions.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?