From f5ab0a14ea83eb6c27196b0c5d600b7f8b8b75fc Mon Sep 17 00:00:00 2001 From: Neil Conway Date: Thu, 7 Apr 2005 01:51:41 +0000 Subject: Add a "USING" clause to DELETE, which is equivalent to the FROM clause in UPDATE. We also now issue a NOTICE if a query has _any_ implicit range table entries -- in the past, we would only warn about implicit RTEs in SELECTs with at least one explicit RTE. As a result of the warning change, 25 of the regression tests had to be updated. I also took the opportunity to remove some bogus whitespace differences between some of the float4 and float8 variants. I believe I have correctly updated all the platform-specific variants, but let me know if that's not the case. Original patch for DELETE ... USING from Euler Taveira de Oliveira, reworked by Neil Conway. --- doc/src/sgml/ref/delete.sgml | 56 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 13 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/ref/delete.sgml b/doc/src/sgml/ref/delete.sgml index 6b6c8bf66d8..954391a228b 100644 --- a/doc/src/sgml/ref/delete.sgml +++ b/doc/src/sgml/ref/delete.sgml @@ -1,5 +1,5 @@ @@ -20,7 +20,9 @@ PostgreSQL documentation -DELETE FROM [ ONLY ] table [ WHERE condition ] +DELETE FROM [ ONLY ] table + [ USING usinglist ] + [ WHERE condition ] @@ -49,10 +51,19 @@ DELETE FROM [ ONLY ] table [ WHERE ONLY clause. + + 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 USING clause. + Which technique is more appropriate depends on the specific + circumstances. + + You must have the DELETE privilege on the table to delete from it, as well as the SELECT - privilege for any table whose values are read in the USING clause or + whose values are read in the condition. @@ -70,6 +81,20 @@ DELETE FROM [ ONLY ] table [ WHERE + + usinglist + + + A list of table expressions, allowing columns from other tables + to appear in the WHERE condition. This is similar + to the list of tables that can be specified in the of a + SELECT statement; for example, an alias for + the table name can be specified. + + + + condition @@ -105,10 +130,11 @@ DELETE count PostgreSQL lets you reference columns of - other tables in the WHERE condition. For example, to - delete all films produced by a given producer, one might do + other tables in the WHERE condition by specifying the + other tables in the USING clause. For example, + to delete all films produced by a given producer, one might do -DELETE FROM films +DELETE FROM films USING producers WHERE producer_id = producers.id AND producers.name = 'foo'; What is essentially happening here is a join between films @@ -120,10 +146,13 @@ DELETE FROM films WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo'); 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. + execute than the sub-select style. + + + + If add_missing_from is enabled, any relations + mentioned in the WHERE condition will be + implicitly added to the USING clause. @@ -149,9 +178,10 @@ DELETE FROM films; Compatibility - This command conforms to the SQL standard, except that the ability to - reference other tables in the WHERE clause is a - PostgreSQL extension. + This command conforms to the SQL standard, except that the + USING clause and the ability to reference other tables + in the WHERE clause are PostgreSQL + extensions. -- cgit v1.2.3