Server IP : 103.119.228.120 / Your IP : 3.128.31.227 Web Server : Apache System : Linux v8.techscape8.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64 User : nobody ( 99) PHP Version : 5.6.40 Disable Function : shell_exec,symlink,system,exec,proc_get_status,proc_nice,proc_terminate,define_syslog_variables,syslog,openlog,closelog,escapeshellcmd,passthru,ocinum cols,ini_alter,leak,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dl,dll,myshellexec,proc_open,socket_bind,proc_close,escapeshellarg,parse_ini_filepopen,fpassthru,exec,passthru,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,popen,show_source,proc_nice,proc_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,dl,symlink,shell_exec,system,dl,passthru,escapeshellarg,escapeshellcmd,myshellexec,c99_buff_prepare,c99_sess_put,fpassthru,getdisfunc,fx29exec,fx29exec2,is_windows,disp_freespace,fx29sh_getupdate,fx29_buff_prepare,fx29_sess_put,fx29shexit,fx29fsearch,fx29ftpbrutecheck,fx29sh_tools,fx29sh_about,milw0rm,imagez,sh_name,myshellexec,checkproxyhost,dosyayicek,c99_buff_prepare,c99_sess_put,c99getsource,c99sh_getupdate,c99fsearch,c99shexit,view_perms,posix_getpwuid,posix_getgrgid,posix_kill,parse_perms,parsesort,view_perms_color,set_encoder_input,ls_setcheckboxall,ls_reverse_all,rsg_read,rsg_glob,selfURL,dispsecinfo,unix2DosTime,addFile,system,get_users,view_size,DirFiles,DirFilesWide,DirPrintHTMLHeaders,GetFilesTotal,GetTitles,GetTimeTotal,GetMatchesCount,GetFileMatchesCount,GetResultFiles,fs_copy_dir,fs_copy_obj,fs_move_dir,fs_move_obj,fs_rmdir,SearchText,getmicrotime MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/share/doc/postgresql-9.2.24/html/ |
Upload File : |
<!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 9.2.24 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="DISCARD" HREF="sql-discard.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="2017-11-06T22:43:11"></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" ><A HREF="index.html" >PostgreSQL 9.2.24 Documentation</A ></TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A TITLE="DECLARE" HREF="sql-declare.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-commands.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="20%" ALIGN="right" VALIGN="top" ><A TITLE="DISCARD" HREF="sql-discard.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="AEN73005" ></A ><H2 >Name</H2 >DELETE -- delete rows of a table</DIV ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN73010" ></A ><H2 >Synopsis</H2 ><PRE CLASS="SYNOPSIS" >[ WITH [ RECURSIVE ] <TT CLASS="REPLACEABLE" ><I >with_query</I ></TT > [, ...] ] DELETE FROM [ ONLY ] <TT CLASS="REPLACEABLE" ><I >table_name</I ></TT > [ * ] [ [ AS ] <TT CLASS="REPLACEABLE" ><I >alias</I ></TT > ] [ USING <TT CLASS="REPLACEABLE" ><I >using_list</I ></TT > ] [ WHERE <TT CLASS="REPLACEABLE" ><I >condition</I ></TT > | WHERE CURRENT OF <TT CLASS="REPLACEABLE" ><I >cursor_name</I ></TT > ] [ RETURNING * | <TT CLASS="REPLACEABLE" ><I >output_expression</I ></TT > [ [ AS ] <TT CLASS="REPLACEABLE" ><I >output_name</I ></TT > ] [, ...] ]</PRE ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN73020" ></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" >TRUNCATE</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 > There are two ways to delete rows in a table using information contained in other tables in the database: using sub-selects, or specifying additional tables in the <TT CLASS="LITERAL" >USING</TT > clause. Which technique is more appropriate depends on the specific circumstances. </P ><P > The optional <TT CLASS="LITERAL" >RETURNING</TT > clause causes <TT CLASS="COMMAND" >DELETE</TT > to compute and return value(s) based on each row actually deleted. Any expression using the table's columns, and/or columns of other tables mentioned in <TT CLASS="LITERAL" >USING</TT >, can be computed. The syntax of the <TT CLASS="LITERAL" >RETURNING</TT > list is identical to that of the output list of <TT CLASS="COMMAND" >SELECT</TT >. </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 in the <TT CLASS="LITERAL" >USING</TT > clause or whose values are read in the <TT CLASS="REPLACEABLE" ><I >condition</I ></TT >. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN73043" ></A ><H2 >Parameters</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="REPLACEABLE" ><I >with_query</I ></TT ></DT ><DD ><P > The <TT CLASS="LITERAL" >WITH</TT > clause allows you to specify one or more subqueries that can be referenced by name in the <TT CLASS="COMMAND" >DELETE</TT > query. See <A HREF="queries-with.html" >Section 7.8</A > and <A HREF="sql-select.html" >SELECT</A > for details. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >table_name</I ></TT ></DT ><DD ><P > The name (optionally schema-qualified) of the table to delete rows from. If <TT CLASS="LITERAL" >ONLY</TT > is specified before the table name, matching rows are deleted from the named table only. If <TT CLASS="LITERAL" >ONLY</TT > is not specified, matching rows are also deleted from any tables inheriting from the named table. Optionally, <TT CLASS="LITERAL" >*</TT > can be specified after the table name to explicitly indicate that descendant tables are included. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >alias</I ></TT ></DT ><DD ><P > A substitute name for the target table. When an alias is provided, it completely hides the actual name of the table. For example, given <TT CLASS="LITERAL" >DELETE FROM foo AS f</TT >, the remainder of the <TT CLASS="COMMAND" >DELETE</TT > statement must refer to this table as <TT CLASS="LITERAL" >f</TT > not <TT CLASS="LITERAL" >foo</TT >. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >using_list</I ></TT ></DT ><DD ><P > A list of table expressions, allowing columns from other tables to appear in the <TT CLASS="LITERAL" >WHERE</TT > condition. This is similar to the list of tables that can be specified in the <A HREF="sql-select.html#SQL-FROM" ><I ><I >FROM</I > Clause</I ></A > of a <TT CLASS="COMMAND" >SELECT</TT > statement; for example, an alias for the table name can be specified. Do not repeat the target table in the <TT CLASS="REPLACEABLE" ><I >using_list</I ></TT >, unless you wish to set up a self-join. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >condition</I ></TT ></DT ><DD ><P > An expression that returns a value of type <TT CLASS="TYPE" >boolean</TT >. Only rows for which this expression returns <TT CLASS="LITERAL" >true</TT > will be deleted. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >cursor_name</I ></TT ></DT ><DD ><P > The name of the cursor to use in a <TT CLASS="LITERAL" >WHERE CURRENT OF</TT > condition. The row to be deleted is the one most recently fetched from this cursor. The cursor must be a non-grouping query on the <TT CLASS="COMMAND" >DELETE</TT >'s target table. Note that <TT CLASS="LITERAL" >WHERE CURRENT OF</TT > cannot be specified together with a Boolean condition. See <A HREF="sql-declare.html" >DECLARE</A > for more information about using cursors with <TT CLASS="LITERAL" >WHERE CURRENT OF</TT >. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >output_expression</I ></TT ></DT ><DD ><P > An expression to be computed and returned by the <TT CLASS="COMMAND" >DELETE</TT > command after each row is deleted. The expression can use any column names of the table named by <TT CLASS="REPLACEABLE" ><I >table_name</I ></TT > or table(s) listed in <TT CLASS="LITERAL" >USING</TT >. Write <TT CLASS="LITERAL" >*</TT > to return all columns. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >output_name</I ></TT ></DT ><DD ><P > A name to use for a returned column. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN73112" ></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. Note that the number may be less than the number of rows that matched the <TT CLASS="REPLACEABLE" ><I >condition</I ></TT > when deletes were suppressed by a <TT CLASS="LITERAL" >BEFORE DELETE</TT > trigger. If <TT CLASS="REPLACEABLE" ><I >count</I ></TT > is 0, no rows were deleted by the query (this is not considered an error). </P ><P > If the <TT CLASS="COMMAND" >DELETE</TT > command contains a <TT CLASS="LITERAL" >RETURNING</TT > clause, the result will be similar to that of a <TT CLASS="COMMAND" >SELECT</TT > statement containing the columns and values defined in the <TT CLASS="LITERAL" >RETURNING</TT > list, computed over the row(s) deleted by the command. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN73127" ></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 by specifying the other tables in the <TT CLASS="LITERAL" >USING</TT > clause. For example, to delete all films produced by a given producer, one can do: </P><PRE CLASS="PROGRAMLISTING" >DELETE FROM films USING producers 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. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN73138" ></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 ><P > Delete completed tasks, returning full details of the deleted rows: </P><PRE CLASS="PROGRAMLISTING" >DELETE FROM tasks WHERE status = 'DONE' RETURNING *;</PRE ><P> </P ><P > Delete the row of <TT CLASS="STRUCTNAME" >tasks</TT > on which the cursor <TT CLASS="LITERAL" >c_tasks</TT > is currently positioned: </P><PRE CLASS="PROGRAMLISTING" >DELETE FROM tasks WHERE CURRENT OF c_tasks;</PRE ><P></P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN73151" ></A ><H2 >Compatibility</H2 ><P > This command conforms to the <ACRONYM CLASS="ACRONYM" >SQL</ACRONYM > standard, except that the <TT CLASS="LITERAL" >USING</TT > and <TT CLASS="LITERAL" >RETURNING</TT > clauses are <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > extensions, as is the ability to use <TT CLASS="LITERAL" >WITH</TT > with <TT CLASS="COMMAND" >DELETE</TT >. </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-discard.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" >DISCARD</TD ></TR ></TABLE ></DIV ></BODY ></HTML >