Delete all records NOT referenced by Foreign Keys

From: "D(dot) Dante Lorenso" <dante(at)lorenso(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Delete all records NOT referenced by Foreign Keys
Date: 2003-12-13 08:20:15
Message-ID: 3FDACBBF.8050901@lorenso.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I'd like to run a clean up command on my tables to
eliminate rows that I'm no longer using in the database.

I want to do something like this:

DELETE FROM tablename
WHERE IS_REFERENCED_BY_FOREIGN_KEY IS FALSE;

Does anyone know how something like this could be done
in PostgreSQL? I know I can search all the tables that
I know refer to this table and see if my primary key
exists, but I want a solution that does not require me to
rewrite my code every time a new foreign key constraint
is added to the database.

There must be a way to ask PostgreSQL for a reference count
on a given row or something.

Thanks,

Dante

---------
D. Dante Lorenso
dante(at)lorenso(dot)com

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Keith C. Perry 2003-12-13 09:19:46 Re: tablespaces in 7.5?
Previous Message Matthew T. O'Connor 2003-12-13 05:22:51 Re: Windows Version of PostgreSQL