1DELETE()                         SQL Commands                         DELETE()
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 ]
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       By default, DELETE will delete rows in the specified table and all  its
26       child  tables.  If you wish to delete only from the specific table men‐
27       tioned, you must use the ONLY clause.
28
29       There are two ways to delete rows in a  table  using  information  con‐
30       tained  in other tables in the database: using sub-selects, or specify‐
31       ing additional tables in the USING clause.   Which  technique  is  more
32       appropriate depends on the specific circumstances.
33
34       The  optional  RETURNING  clause  causes  DELETE  to compute and return
35       value(s) based on each row actually deleted.  Any expression using  the
36       table's columns, and/or columns of other tables mentioned in USING, can
37       be computed.  The syntax of the RETURNING list is identical to that  of
38       the output list of SELECT.
39
40       You  must  have the DELETE privilege on the table to delete from it, as
41       well as the SELECT privilege for any table in the USING clause or whose
42       values are read in the condition.
43

PARAMETERS

45       ONLY   If  specified,  delete  rows from the named table only. When not
46              specified, any tables inheriting from the named table  are  also
47              processed.
48
49       table  The name (optionally schema-qualified) of an existing table.
50
51       alias  A  substitute  name  for the target table. When an alias is pro‐
52              vided, it completely hides the actual name  of  the  table.  For
53              example, given DELETE FROM foo AS f, the remainder of the DELETE
54              statement must refer to this table as f not foo.
55
56       usinglist
57              A list of table expressions, allowing columns from other  tables
58              to appear in the WHERE condition. This is similar to the list of
59              tables that can be specified in the FROM Clause [select(7)] of a
60              SELECT  statement;  for example, an alias for the table name can
61              be specified. Do not repeat the target table in  the  usinglist,
62              unless you wish to set up a self-join.
63
64       condition
65              An  expression  returning  a value of type boolean, which deter‐
66              mines the rows that are to be deleted.
67
68       output_expression
69              An expression to be computed and returned by the DELETE  command
70              after  each  row  is  deleted. The expression may use any column
71              names of the table or table(s) listed  in  USING.   Write  *  to
72              return all columns.
73
74       output_name
75              A name to use for a returned column.
76

OUTPUTS

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

NOTES

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

EXAMPLES

110       Delete all films but musicals:
111
112       DELETE FROM films WHERE kind <> 'Musical';
113
114
115       Clear the table films:
116
117       DELETE FROM films;
118
119
120       Delete completed tasks, returning full details of the deleted rows:
121
122       DELETE FROM tasks WHERE status = 'DONE' RETURNING *;
123
124

COMPATIBILITY

126       This  command  conforms  to the SQL standard, except that the USING and
127       RETURNING clauses are PostgreSQL extensions.
128
129
130
131SQL - Language Statements         2008-06-08                          DELETE()
Impressum