Re: Deleting conflicting rows when creating a foreign key

Поиск
Список
Период
Сортировка
От Craig Ringer
Тема Re: Deleting conflicting rows when creating a foreign key
Дата
Msg-id 4992546F.2070408@postnewspapers.com.au
обсуждение исходный текст
Ответ на Re: Deleting conflicting rows when creating a foreign key  (Richard Huxton <dev@archonet.com>)
Ответы Re: Deleting conflicting rows when creating a foreign key  (Igor Katson <descentspb@gmail.com>)
Список pgsql-general
Richard Huxton wrote:

> DELETE FROM table1 WHERE col1 NOT IN (SELECT id from table2);

Just as a side note: If you have a large number of missing IDs and don't
want to wait a long time, you may be better off with something like
(untested, but I think it's right - TEST FIRST):

SELECT *
FROM table1 t1 LEFT OUTER JOIN table2 t2 ON (t1.ref_col=t2.id)
WHERE t2.id IS NULL;

-- check that the rows to be deleted are OK

DELETE FROM table1
USING table1 t1 LEFT OUTER JOIN table2 t2 ON (t1.ref_col=t2.id)
WHERE table1.id = t1.id AND t2.id IS NULL;

(by the way, being able to specify an explicit join method in a DELETE
... USING or update ... USING would be *great*).

--
Craig Ringer

В списке pgsql-general по дате отправления:

Предыдущее
От: Pavel Stehule
Дата:
Сообщение: Re: Referencing Cursor/Row/Record Fields in PL/PgSQL
Следующее
От: Craig Ringer
Дата:
Сообщение: Re: Slow cross-machine read on one table