<!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/delete.sgml,v 1.10 2000/03/26 18:32:27 petere Exp $
Postgres documentation
-->

<refentry id="SQL-DELETE">
 <refmeta>
  <refentrytitle id="SQL-DELETE-TITLE">
   DELETE
  </refentrytitle>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>
 <refnamediv>
  <refname>
   DELETE
  </refname>
  <refpurpose>
   Removes rows from a table
  </refpurpose>

 </refnamediv>
 <refsynopsisdiv>
  <refsynopsisdivinfo>
   <date>1999-07-20</date>
  </refsynopsisdivinfo>
  <synopsis>
DELETE FROM <replaceable class="PARAMETER">table</replaceable> [ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
  </synopsis>

  <refsect2 id="R2-SQL-DELETE-1">
   <refsect2info>
    <date>1998-04-15</date>
   </refsect2info>
   <title>
    Inputs
   </title>
   <para>
    <variablelist>
     <varlistentry>
      <term><replaceable class="parameter">table</replaceable></term>
      <listitem>
       <para>
	The name of an existing table.
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><replaceable class="parameter">condition</replaceable></term>
      <listitem>
       <para>
	This is an SQL selection query which returns the rows which
	are to be deleted.
       </para>
       <para>
	Refer to the SELECT statement for further description
	of the WHERE clause.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>
  </refsect2>

  <refsect2 id="R2-SQL-DELETE-2">
   <refsect2info>
    <date>1998-04-15</date>
   </refsect2info>
   <title>
    Outputs
   </title>
   <para>
    <variablelist>
     <varlistentry>
      <term><computeroutput>
DELETE <replaceable class="parameter">count</replaceable>
       </computeroutput></term>
      <listitem>
       <para>
	Message returned if items are successfully deleted. The
	<replaceable class="parameter">count</replaceable> is the number
	of rows deleted.
       </para>
       <para>
	If <replaceable class="parameter">count</replaceable> is 0,
	no rows were deleted.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>
  </refsect2>
 </refsynopsisdiv>

 <refsect1 id="R1-SQL-DELETE-1">
  <refsect1info>
   <date>1998-04-15</date>
  </refsect1info>
  <title>
   Description
  </title>

  <para>
   <command>DELETE</command> removes rows which satisfy the WHERE
   clause from the specified table.
  </para>

  <para>
   If the <firstterm>condition</firstterm> (WHERE clause) is absent,
   the effect is to delete all rows in the table.
   The result is a valid, but empty table.

   <tip>
    <para>
     <xref linkend="sql-truncate" endterm="sql-truncate-title"> is a
     <productname>Postgres</productname> extension which provides a
     faster mechanism to remove all rows from a table.
    </para>
   </tip>
  </para>

  <para>
   You must have write access to the table in order to modify
   it, as well as read access to any table whose values are
   read in the <replaceable class="parameter">condition</replaceable>.
  </para>
 </refsect1>

 <refsect1 id="R1-SQL-DELETE-2">
  <title>
   Usage
  </title>
  <para>
   Remove all films but musicals:
<programlisting>
DELETE FROM films WHERE kind &lt;&gt; 'Musical';
SELECT * FROM films;

<computeroutput>
 code  |           title           | did | date_prod  |  kind   | len
-------+---------------------------+-----+------------+---------+-------
 UA501 | West Side Story           | 105 | 1961-01-03 | Musical | 02:32
 TC901 | The King and I            | 109 | 1956-08-11 | Musical | 02:13
 WD101 | Bed Knobs and Broomsticks | 111 |            | Musical | 01:57
(3 rows)
</computeroutput>
</programlisting>
  </para>

  <para>
   Clear the table <literal>films</literal>:
<programlisting>
DELETE FROM films;
SELECT * FROM films;

<computeroutput>
 code | title | did | date_prod | kind | len
------+-------+-----+-----------+------+-----
(0 rows)
</computeroutput>
</programlisting>      
  </para>
 </refsect1>

 <refsect1 id="R1-SQL-DELETE-3">
  <title>
   Compatibility
  </title>

  <refsect2 id="R2-SQL-DELETE-4">
   <refsect2info>
    <date>1998-04-15</date>
   </refsect2info>
   <title>
    SQL92
   </title>
   <para>
    <acronym>SQL92</acronym> allows a positioned DELETE statement:
   
    <synopsis>
DELETE FROM <replaceable class="parameter">table</replaceable> WHERE
    CURRENT OF <replaceable class="parameter">cursor</replaceable>
    </synopsis>

    where <replaceable class="parameter">cursor</replaceable>
    identifies an open cursor.
    Interactive cursors in <productname>Postgres</productname> are read-only.
   </para>
  </refsect2>
 </refsect1>
</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:nil
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/catalog"
sgml-local-ecat-files:nil
End:
-->
