Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
sql-delete.html145 linesDownload Raw Back to html
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>DELETE</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-declare.html" title="DECLARE" /><link rel="next" href="sql-discard.html" title="DISCARD" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">DELETE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-declare.html" title="DECLARE">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-discard.html" title="DISCARD">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-DELETE"><div class="titlepage"></div><a id="id-1.9.3.100.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">DELETE</span></h2><p>DELETE — delete rows of a table</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">3[ WITH [ RECURSIVE ] <em class="replaceable"><code>with_query</code></em> [, ...] ]4DELETE FROM [ ONLY ] <em class="replaceable"><code>table_name</code></em> [ * ] [ [ AS ] <em class="replaceable"><code>alias</code></em> ]5    [ USING <em class="replaceable"><code>from_item</code></em> [, ...] ]6    [ WHERE <em class="replaceable"><code>condition</code></em> | WHERE CURRENT OF <em class="replaceable"><code>cursor_name</code></em> ]7    [ RETURNING * | <em class="replaceable"><code>output_expression</code></em> [ [ AS ] <em class="replaceable"><code>output_name</code></em> ] [, ...] ]8</pre></div><div class="refsect1" id="id-1.9.3.100.5"><h2>Description</h2><p>9   <code class="command">DELETE</code> deletes rows that satisfy the10   <code class="literal">WHERE</code> clause from the specified table.  If the11   <code class="literal">WHERE</code> clause is absent, the effect is to delete12   all rows in the table.  The result is a valid, but empty table.13  </p><div class="tip"><h3 class="title">Tip</h3><p>14     <a class="link" href="sql-truncate.html" title="TRUNCATE"><code class="command">TRUNCATE</code></a> provides a15     faster mechanism to remove all rows from a table.16    </p></div><p>17   There are two ways to delete rows in a table using information18   contained in other tables in the database: using sub-selects, or19   specifying additional tables in the <code class="literal">USING</code> clause.20   Which technique is more appropriate depends on the specific21   circumstances.22  </p><p>23   The optional <code class="literal">RETURNING</code> clause causes <code class="command">DELETE</code>24   to compute and return value(s) based on each row actually deleted.25   Any expression using the table's columns, and/or columns of other26   tables mentioned in <code class="literal">USING</code>, can be computed.27   The syntax of the <code class="literal">RETURNING</code> list is identical to that of the28   output list of <code class="command">SELECT</code>.29  </p><p>30   You must have the <code class="literal">DELETE</code> privilege on the table31   to delete from it, as well as the <code class="literal">SELECT</code>32   privilege for any table in the <code class="literal">USING</code> clause or33   whose values are read in the <em class="replaceable"><code>condition</code></em>.34  </p></div><div class="refsect1" id="id-1.9.3.100.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>with_query</code></em></span></dt><dd><p>35      The <code class="literal">WITH</code> clause allows you to specify one or more36      subqueries that can be referenced by name in the <code class="command">DELETE</code>37      query. See <a class="xref" href="queries-with.html" title="7.8. WITH Queries (Common Table Expressions)">Section 7.8</a> and <a class="xref" href="sql-select.html" title="SELECT"><span class="refentrytitle">SELECT</span></a>38      for details.39     </p></dd><dt><span class="term"><em class="replaceable"><code>table_name</code></em></span></dt><dd><p>40      The name (optionally schema-qualified) of the table to delete rows41      from.  If <code class="literal">ONLY</code> is specified before the table name,42      matching rows are deleted from the named table only.  If43      <code class="literal">ONLY</code> is not specified, matching rows are also deleted44      from any tables inheriting from the named table.  Optionally,45      <code class="literal">*</code> can be specified after the table name to explicitly46      indicate that descendant tables are included.47     </p></dd><dt><span class="term"><em class="replaceable"><code>alias</code></em></span></dt><dd><p>48      A substitute name for the target table. When an alias is49      provided, it completely hides the actual name of the table.  For50      example, given <code class="literal">DELETE FROM foo AS f</code>, the remainder51      of the <code class="command">DELETE</code> statement must refer to this52      table as <code class="literal">f</code> not <code class="literal">foo</code>.53     </p></dd><dt><span class="term"><em class="replaceable"><code>from_item</code></em></span></dt><dd><p>54      A table expression allowing columns from other tables to appear55      in the <code class="literal">WHERE</code> condition.  This uses the same56      syntax as the <a class="link" href="sql-select.html#SQL-FROM" title="FROM Clause"><code class="literal">FROM</code></a>57      clause of a <code class="command">SELECT</code> statement; for example, an alias58      for the table name can be specified.  Do not repeat the target59      table as a <em class="replaceable"><code>from_item</code></em>60      unless you wish to set up a self-join (in which case it must appear61      with an alias in the <em class="replaceable"><code>from_item</code></em>).62     </p></dd><dt><span class="term"><em class="replaceable"><code>condition</code></em></span></dt><dd><p>63      An expression that returns a value of type <code class="type">boolean</code>.64      Only rows for which this expression returns <code class="literal">true</code>65      will be deleted.66     </p></dd><dt><span class="term"><em class="replaceable"><code>cursor_name</code></em></span></dt><dd><p>67      The name of the cursor to use in a <code class="literal">WHERE CURRENT OF</code>68      condition.  The row to be deleted is the one most recently fetched69      from this cursor.  The cursor must be a non-grouping70      query on the <code class="command">DELETE</code>'s target table.71      Note that <code class="literal">WHERE CURRENT OF</code> cannot be72      specified together with a Boolean condition.  See73      <a class="xref" href="sql-declare.html" title="DECLARE"><span class="refentrytitle">DECLARE</span></a>74      for more information about using cursors with75      <code class="literal">WHERE CURRENT OF</code>.76     </p></dd><dt><span class="term"><em class="replaceable"><code>output_expression</code></em></span></dt><dd><p>77      An expression to be computed and returned by the <code class="command">DELETE</code>78      command after each row is deleted.  The expression can use any79      column names of the table named by <em class="replaceable"><code>table_name</code></em>80      or table(s) listed in <code class="literal">USING</code>.81      Write <code class="literal">*</code> to return all columns.82     </p></dd><dt><span class="term"><em class="replaceable"><code>output_name</code></em></span></dt><dd><p>83      A name to use for a returned column.84     </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.100.7"><h2>Outputs</h2><p>85   On successful completion, a <code class="command">DELETE</code> command returns a command86   tag of the form87</p><pre class="screen">88DELETE <em class="replaceable"><code>count</code></em>89</pre><p>90   The <em class="replaceable"><code>count</code></em> is the number91   of rows deleted.  Note that the number may be less than the number of92   rows that matched the <em class="replaceable"><code>condition</code></em> when deletes were93   suppressed by a <code class="literal">BEFORE DELETE</code> trigger.  If <em class="replaceable"><code>count</code></em> is 0, no rows were deleted by94   the query (this is not considered an error).95  </p><p>96   If the <code class="command">DELETE</code> command contains a <code class="literal">RETURNING</code>97   clause, the result will be similar to that of a <code class="command">SELECT</code>98   statement containing the columns and values defined in the99   <code class="literal">RETURNING</code> list, computed over the row(s) deleted by the100   command.101  </p></div><div class="refsect1" id="id-1.9.3.100.8"><h2>Notes</h2><p>102   <span class="productname">PostgreSQL</span> lets you reference columns of103   other tables in the <code class="literal">WHERE</code> condition by specifying the104   other tables in the <code class="literal">USING</code> clause.  For example,105   to delete all films produced by a given producer, one can do:106</p><pre class="programlisting">107DELETE FROM films USING producers108  WHERE producer_id = producers.id AND producers.name = 'foo';109</pre><p>110   What is essentially happening here is a join between <code class="structname">films</code>111   and <code class="structname">producers</code>, with all successfully joined112   <code class="structname">films</code> rows being marked for deletion.113   This syntax is not standard.  A more standard way to do it is:114</p><pre class="programlisting">115DELETE FROM films116  WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');117</pre><p>118   In some cases the join style is easier to write or faster to119   execute than the sub-select style.120  </p></div><div class="refsect1" id="id-1.9.3.100.9"><h2>Examples</h2><p>121   Delete all films but musicals:122</p><pre class="programlisting">123DELETE FROM films WHERE kind &lt;&gt; 'Musical';124</pre><p>125  </p><p>126   Clear the table <code class="literal">films</code>:127</p><pre class="programlisting">128DELETE FROM films;129</pre><p>130  </p><p>131   Delete completed tasks, returning full details of the deleted rows:132</p><pre class="programlisting">133DELETE FROM tasks WHERE status = 'DONE' RETURNING *;134</pre><p>135  </p><p>136   Delete the row of <code class="structname">tasks</code> on which the cursor137   <code class="literal">c_tasks</code> is currently positioned:138</p><pre class="programlisting">139DELETE FROM tasks WHERE CURRENT OF c_tasks;140</pre></div><div class="refsect1" id="id-1.9.3.100.10"><h2>Compatibility</h2><p>141   This command conforms to the <acronym class="acronym">SQL</acronym> standard, except142   that the <code class="literal">USING</code> and <code class="literal">RETURNING</code> clauses143   are <span class="productname">PostgreSQL</span> extensions, as is the ability144   to use <code class="literal">WITH</code> with <code class="command">DELETE</code>.145  </p></div><div class="refsect1" id="id-1.9.3.100.11"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-truncate.html" title="TRUNCATE"><span class="refentrytitle">TRUNCATE</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-declare.html" title="DECLARE">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-discard.html" title="DISCARD">Next</a></td></tr><tr><td width="40%" align="left" valign="top">DECLARE </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> DISCARD</td></tr></table></div></body></html>
codekingpro/portable-devtools · Team Ai