1DELETE(7)                        SQL Commands                        DELETE(7)
2
3
4

NAME

6       DELETE - delete rows of a table
7
8

SYNOPSIS

10       DELETE FROM [ ONLY ] table [ * ] [ [ AS ] alias ]
11           [ USING usinglist ]
12           [ WHERE condition | WHERE CURRENT OF cursor_name ]
13           [ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
14
15

DESCRIPTION

17       DELETE  deletes  rows  that satisfy the WHERE clause from the specified
18       table. If the WHERE clause is absent, the effect is to delete all  rows
19       in the table. The result is a valid, but empty table.
20
21              Tip:  TRUNCATE [truncate(7)] is a PostgreSQL extension that pro‐
22              vides a faster mechanism to remove all rows from a table.
23
24
25       There are two ways to delete rows in a  table  using  information  con‐
26       tained  in other tables in the database: using sub-selects, or specify‐
27       ing additional tables in the USING clause.   Which  technique  is  more
28       appropriate depends on the specific circumstances.
29
30       The  optional  RETURNING  clause  causes  DELETE  to compute and return
31       value(s) based on each row actually deleted.  Any expression using  the
32       table's columns, and/or columns of other tables mentioned in USING, can
33       be computed.  The syntax of the RETURNING list is identical to that  of
34       the output list of SELECT.
35
36       You  must  have the DELETE privilege on the table to delete from it, as
37       well as the SELECT privilege for any table in the USING clause or whose
38       values are read in the condition.
39

PARAMETERS

41       table  The  name  (optionally  schema-qualified) of the table to delete
42              rows from. If ONLY is specified before the table name,  matching
43              rows are deleted from the named table only. If ONLY is not spec‐
44              ified, matching rows are also deleted from any tables inheriting
45              from  the  named table. Optionally, * can be specified after the
46              table name to explicitly indicate  that  descendant  tables  are
47              included.
48
49       alias  A  substitute  name  for the target table. When an alias is pro‐
50              vided, it completely hides the actual name  of  the  table.  For
51              example, given DELETE FROM foo AS f, the remainder of the DELETE
52              statement must refer to this table as f not foo.
53
54       usinglist
55              A list of table expressions, allowing columns from other  tables
56              to appear in the WHERE condition. This is similar to the list of
57              tables that can be specified in the FROM Clause [select(7)] of a
58              SELECT  statement;  for example, an alias for the table name can
59              be specified. Do not repeat the target table in  the  usinglist,
60              unless you wish to set up a self-join.
61
62       condition
63              An  expression  that returns a value of type boolean.  Only rows
64              for which this expression returns true will be deleted.
65
66       cursor_name
67              The name of the cursor to use in a WHERE CURRENT  OF  condition.
68              The row to be deleted is the one most recently fetched from this
69              cursor. The cursor must be a non-grouping query on the  DELETE's
70              target  table.   Note  that WHERE CURRENT OF cannot be specified
71              together with a Boolean condition. See DECLARE [declare(7)]  for
72              more information about using cursors with WHERE CURRENT OF.
73
74       output_expression
75              An  expression to be computed and returned by the DELETE command
76              after each row is deleted. The expression  can  use  any  column
77              names  of  the  table  or  table(s) listed in USING.  Write * to
78              return all columns.
79
80       output_name
81              A name to use for a returned column.
82

OUTPUTS

84       On successful completion, a DELETE command returns a command tag of the
85       form
86
87       DELETE count
88
89       The count is the number of rows deleted. If count is 0, no rows matched
90       the condition (this is not considered an error).
91
92       If the DELETE command contains a RETURNING clause, the result  will  be
93       similar to that of a SELECT statement containing the columns and values
94       defined in the RETURNING list, computed over the row(s) deleted by  the
95       command.
96

NOTES

98       PostgreSQL lets you reference columns of other tables in the WHERE con‐
99       dition by specifying the other tables in the USING clause. For example,
100       to delete all films produced by a given producer, one can do:
101
102       DELETE FROM films USING producers
103         WHERE producer_id = producers.id AND producers.name = 'foo';
104
105       What  is essentially happening here is a join between films and produc‐
106       ers, with all successfully joined films rows being marked for deletion.
107       This syntax is not standard. A more standard way to do it is:
108
109       DELETE FROM films
110         WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');
111
112       In  some  cases  the join style is easier to write or faster to execute
113       than the sub-select style.
114

EXAMPLES

116       Delete all films but musicals:
117
118       DELETE FROM films WHERE kind <> 'Musical';
119
120
121       Clear the table films:
122
123       DELETE FROM films;
124
125
126       Delete completed tasks, returning full details of the deleted rows:
127
128       DELETE FROM tasks WHERE status = 'DONE' RETURNING *;
129
130
131       Delete the row of tasks on which the cursor c_tasks is currently  posi‐
132       tioned:
133
134       DELETE FROM tasks WHERE CURRENT OF c_tasks;
135
136

COMPATIBILITY

138       This  command  conforms  to the SQL standard, except that the USING and
139       RETURNING clauses are PostgreSQL extensions.
140
141
142
143SQL - Language Statements         2014-02-17                         DELETE(7)
Impressum