<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >DELETE</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 8.0.11 Documentation" HREF="index.html"><LINK REL="UP" TITLE="SQL Commands" HREF="sql-commands.html"><LINK REL="PREVIOUS" TITLE="DECLARE" HREF="sql-declare.html"><LINK REL="NEXT" TITLE="DROP AGGREGATE" HREF="sql-dropaggregate.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2007-02-02T03:57:22"></HEAD ><BODY CLASS="REFENTRY" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" >PostgreSQL 8.0.11 Documentation</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-declare.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-declare.html" >Fast Backward</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="sql-dropaggregate.html" >Fast Forward</A ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="sql-dropaggregate.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="SQL-DELETE" ></A >DELETE</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN41827" ></A ><H2 >Name</H2 >DELETE -- delete rows of a table</DIV ><A NAME="AEN41830" ></A ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN41832" ></A ><H2 >Synopsis</H2 ><PRE CLASS="SYNOPSIS" >DELETE FROM [ ONLY ] <TT CLASS="REPLACEABLE" ><I >table</I ></TT > [ WHERE <TT CLASS="REPLACEABLE" ><I >condition</I ></TT > ]</PRE ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN41836" ></A ><H2 >Description</H2 ><P > <TT CLASS="COMMAND" >DELETE</TT > deletes rows that satisfy the <TT CLASS="LITERAL" >WHERE</TT > clause from the specified table. If the <TT CLASS="LITERAL" >WHERE</TT > clause is absent, the effect is to delete all rows in the table. The result is a valid, but empty table. </P ><DIV CLASS="TIP" ><BLOCKQUOTE CLASS="TIP" ><P ><B >Tip: </B > <A HREF="sql-truncate.html" ><I >TRUNCATE</I ></A > is a <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > extension that provides a faster mechanism to remove all rows from a table. </P ></BLOCKQUOTE ></DIV ><P > By default, <TT CLASS="COMMAND" >DELETE</TT > will delete rows in the specified table and all its subtables. If you wish to delete only from the specific table mentioned, you must use the <TT CLASS="LITERAL" >ONLY</TT > clause. </P ><P > You must have the <TT CLASS="LITERAL" >DELETE</TT > privilege on the table to delete from it, as well as the <TT CLASS="LITERAL" >SELECT</TT > privilege for any table whose values are read in the <TT CLASS="REPLACEABLE" ><I >condition</I ></TT >. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN41853" ></A ><H2 >Parameters</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="REPLACEABLE" ><I >table</I ></TT ></DT ><DD ><P > The name (optionally schema-qualified) of an existing table. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >condition</I ></TT ></DT ><DD ><P > A value expression that returns a value of type <TT CLASS="TYPE" >boolean</TT > that determines the rows which are to be deleted. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN41867" ></A ><H2 >Outputs</H2 ><P > On successful completion, a <TT CLASS="COMMAND" >DELETE</TT > command returns a command tag of the form </P><PRE CLASS="SCREEN" >DELETE <TT CLASS="REPLACEABLE" ><I >count</I ></TT ></PRE ><P> The <TT CLASS="REPLACEABLE" ><I >count</I ></TT > is the number of rows deleted. If <TT CLASS="REPLACEABLE" ><I >count</I ></TT > is 0, no rows matched the <TT CLASS="REPLACEABLE" ><I >condition</I ></TT > (this is not considered an error). </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN41876" ></A ><H2 >Notes</H2 ><P > <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > lets you reference columns of other tables in the <TT CLASS="LITERAL" >WHERE</TT > condition. For example, to delete all films produced by a given producer, one might do </P><PRE CLASS="PROGRAMLISTING" >DELETE FROM films WHERE producer_id = producers.id AND producers.name = 'foo';</PRE ><P> What is essentially happening here is a join between <TT CLASS="STRUCTNAME" >films</TT > and <TT CLASS="STRUCTNAME" >producers</TT >, with all successfully joined <TT CLASS="STRUCTNAME" >films</TT > rows being marked for deletion. This syntax is not standard. A more standard way to do it is </P><PRE CLASS="PROGRAMLISTING" >DELETE FROM films WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');</PRE ><P> In some cases the join style is easier to write or faster to execute than the sub-select style. One objection to the join style is that there is no explicit list of what tables are being used, which makes the style somewhat error-prone; also it cannot handle self-joins. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN41886" ></A ><H2 >Examples</H2 ><P > Delete all films but musicals: </P><PRE CLASS="PROGRAMLISTING" >DELETE FROM films WHERE kind <> 'Musical';</PRE ><P> </P ><P > Clear the table <TT CLASS="LITERAL" >films</TT >: </P><PRE CLASS="PROGRAMLISTING" >DELETE FROM films;</PRE ><P> </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN41893" ></A ><H2 >Compatibility</H2 ><P > This command conforms to the SQL standard, except that the ability to reference other tables in the <TT CLASS="LITERAL" >WHERE</TT > clause is a <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > extension. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="sql-declare.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="sql-dropaggregate.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >DECLARE</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="sql-commands.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >DROP AGGREGATE</TD ></TR ></TABLE ></DIV ></BODY ></HTML >