3 PostgreSQL documentation
6 <refentry id=
"SQL-DELETE">
8 <refentrytitle id=
"SQL-DELETE-TITLE">DELETE
</refentrytitle>
9 <refmiscinfo>SQL - Language Statements
</refmiscinfo>
13 <refname>DELETE
</refname>
14 <refpurpose>delete rows of a table
</refpurpose>
17 <indexterm zone=
"sql-delete">
18 <primary>DELETE
</primary>
23 DELETE FROM [ ONLY ]
<replaceable class=
"PARAMETER">table
</replaceable> [ [ AS ]
<replaceable class=
"parameter">alias
</replaceable> ]
24 [ USING
<replaceable class=
"PARAMETER">usinglist
</replaceable> ]
25 [ WHERE
<replaceable class=
"PARAMETER">condition
</replaceable> | WHERE CURRENT OF
<replaceable class=
"PARAMETER">cursor_name
</replaceable> ]
26 [ RETURNING * |
<replaceable class=
"parameter">output_expression
</replaceable> [ [ AS ]
<replaceable class=
"parameter">output_name
</replaceable> ] [, ...] ]
31 <title>Description
</title>
34 <command>DELETE
</command> deletes rows that satisfy the
35 <literal>WHERE
</literal> clause from the specified table. If the
36 <literal>WHERE
</literal> clause is absent, the effect is to delete
37 all rows in the table. The result is a valid, but empty table.
42 <xref linkend=
"sql-truncate" endterm=
"sql-truncate-title"> is a
43 <productname>PostgreSQL
</productname> extension that provides a
44 faster mechanism to remove all rows from a table.
49 By default,
<command>DELETE
</command> will delete rows in the
50 specified table and all its child tables. If you wish to delete only
51 from the specific table mentioned, you must use the
52 <literal>ONLY
</literal> clause.
56 There are two ways to delete rows in a table using information
57 contained in other tables in the database: using sub-selects, or
58 specifying additional tables in the
<literal>USING
</literal> clause.
59 Which technique is more appropriate depends on the specific
64 The optional
<literal>RETURNING<
/> clause causes
<command>DELETE<
/>
65 to compute and return value(s) based on each row actually deleted.
66 Any expression using the table's columns, and/or columns of other
67 tables mentioned in
<literal>USING
</literal>, can be computed.
68 The syntax of the
<literal>RETURNING<
/> list is identical to that of the
69 output list of
<command>SELECT<
/>.
73 You must have the
<literal>DELETE
</literal> privilege on the table
74 to delete from it, as well as the
<literal>SELECT
</literal>
75 privilege for any table in the
<literal>USING
</literal> clause or
76 whose values are read in the
<replaceable
77 class=
"parameter">condition
</replaceable>.
82 <title>Parameters
</title>
86 <term><literal>ONLY<
/></term>
89 If specified, delete rows from the named table only. When not
90 specified, any tables inheriting from the named table are also processed.
96 <term><replaceable class=
"parameter">table
</replaceable></term>
99 The name (optionally schema-qualified) of an existing table.
105 <term><replaceable class=
"parameter">alias
</replaceable></term>
108 A substitute name for the target table. When an alias is
109 provided, it completely hides the actual name of the table. For
110 example, given
<literal>DELETE FROM foo AS f<
/>, the remainder
111 of the
<command>DELETE
</command> statement must refer to this
112 table as
<literal>f<
/> not
<literal>foo<
/>.
118 <term><replaceable class=
"PARAMETER">usinglist
</replaceable></term>
121 A list of table expressions, allowing columns from other tables
122 to appear in the
<literal>WHERE<
/> condition. This is similar
123 to the list of tables that can be specified in the
<xref
124 linkend=
"sql-from" endterm=
"sql-from-title"> of a
125 <command>SELECT
</command> statement; for example, an alias for
126 the table name can be specified. Do not repeat the target table
127 in the
<replaceable class=
"PARAMETER">usinglist
</replaceable>,
128 unless you wish to set up a self-join.
134 <term><replaceable class=
"parameter">condition
</replaceable></term>
137 An expression that returns a value of type
<type>boolean
</type>.
138 Only rows for which this expression returns
<literal>true<
/>
145 <term><replaceable class=
"PARAMETER">cursor_name
</replaceable></term>
148 The name of the cursor to use in a
<literal>WHERE CURRENT OF<
/>
149 condition. The row to be deleted is the one most recently fetched
150 from this cursor. The cursor must be a simple (non-join, non-aggregate)
151 query on the
<command>DELETE<
/>'s target table.
152 Note that
<literal>WHERE CURRENT OF<
/> cannot be
153 specified together with a Boolean condition.
159 <term><replaceable class=
"PARAMETER">output_expression
</replaceable></term>
162 An expression to be computed and returned by the
<command>DELETE<
/>
163 command after each row is deleted. The expression can use any
164 column names of the
<replaceable class=
"PARAMETER">table
</replaceable>
165 or table(s) listed in
<literal>USING<
/>.
166 Write
<literal>*<
/> to return all columns.
172 <term><replaceable class=
"PARAMETER">output_name
</replaceable></term>
175 A name to use for a returned column.
183 <title>Outputs
</title>
186 On successful completion, a
<command>DELETE<
/> command returns a command
189 DELETE
<replaceable class=
"parameter">count
</replaceable>
191 The
<replaceable class=
"parameter">count
</replaceable> is the number
192 of rows deleted. If
<replaceable class=
"parameter">count
</replaceable> is
193 0, no rows matched the
<replaceable
194 class=
"parameter">condition
</replaceable> (this is not considered
199 If the
<command>DELETE<
/> command contains a
<literal>RETURNING<
/>
200 clause, the result will be similar to that of a
<command>SELECT<
/>
201 statement containing the columns and values defined in the
202 <literal>RETURNING<
/> list, computed over the row(s) deleted by the
211 <productname>PostgreSQL
</productname> lets you reference columns of
212 other tables in the
<literal>WHERE<
/> condition by specifying the
213 other tables in the
<literal>USING
</literal> clause. For example,
214 to delete all films produced by a given producer, one can do:
216 DELETE FROM films USING producers
217 WHERE producer_id = producers.id AND producers.name = 'foo';
219 What is essentially happening here is a join between
<structname>films<
/>
220 and
<structname>producers<
/>, with all successfully joined
221 <structname>films<
/> rows being marked for deletion.
222 This syntax is not standard. A more standard way to do it is:
225 WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');
227 In some cases the join style is easier to write or faster to
228 execute than the sub-select style.
233 <title>Examples
</title>
236 Delete all films but musicals:
238 DELETE FROM films WHERE kind
<> 'Musical';
243 Clear the table
<literal>films
</literal>:
250 Delete completed tasks, returning full details of the deleted rows:
252 DELETE FROM tasks WHERE status = 'DONE' RETURNING *;
257 Delete the row of
<structname>tasks<
/> on which the cursor
258 <literal>c_tasks<
/> is currently positioned:
260 DELETE FROM tasks WHERE CURRENT OF c_tasks;
266 <title>Compatibility
</title>
269 This command conforms to the
<acronym>SQL
</acronym> standard, except
270 that the
<literal>USING
</literal> and
<literal>RETURNING<
/> clauses
271 are
<productname>PostgreSQL
</productname> extensions.