Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id E96C0476623 for ; Tue, 18 Jun 2002 17:11:08 -0400 (EDT) Received: from fw.mccons.net (adsl-65-64-105-41.dsl.kscymo.swbell.net [65.64.105.41]) by postgresql.org (Postfix) with ESMTP id 8EFBA475A16 for ; Tue, 18 Jun 2002 16:38:42 -0400 (EDT) Received: (from root@localhost) by fw.mccons.net (8.11.3/8.11.4) id g5IKciX11515 for pgsql-admin@postgresql.org; Tue, 18 Jun 2002 15:38:44 -0500 (CDT) (envelope-from bmccane@mccons.net) Received: from localhost (localhost [127.0.0.1]) by fw.mccons.net (8.11.3/8.11.4av) with ESMTP id g5IKcbw11506 for ; Tue, 18 Jun 2002 15:38:37 -0500 (CDT) (envelope-from bmccane@mccons.net) Date: Tue, 18 Jun 2002 15:38:36 -0500 (CDT) From: Brian McCane To: Subject: Re: Removing records that violate foreign key Message-ID: <20020618153820.M1631-100000@fw.mccons.net> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by AMaViS perl-11 X-Razor-Spam-ID: 9ffe7c2a9a67b03577eadd6d88402dc83fc25024 X-Archive-Number: 200206/125 X-Sequence-Number: 4560 I usually use an SELECT with an OUTER JOIN that uses the foreign key with a USING or ON clause. Then in the where clause I look for any records where some field from the primary key table that is NOT NULL is null. Something like: SELECT oid FROM fktbl LEFT OUTER JOIN pktbl USING(keyname) WHERE notnullfield IS NULL ; You can then use it as a sub-select in a DELETE command or pump the result out to a file so you can see if you can tell where it all came from. I used this once to figure out that a trigger I had firing "BEFORE UPDATE" had a boundary condition in it. - brian On Tue, 18 Jun 2002, Cliff Wells wrote: > > Hello, > > I'm porting an application's data from a proprietary format into PostgreSQL > 7.2.1. I'm using ecpg and I basically follow this process: > > create tables > create indexes > import data > add foreign key constraints > > I have to add the foreign key constraint last as otherwise the import will fail > as the records being referenced won't necessarily exist in PostgreSQL yet. The > problem is that now I'd like to delete any records that violate the constraint. > Any suggestions on a quick way to do this? > > -- > Cliff Wells, Software Engineer > Logiplex Corporation (www.logiplex.net) > (503) 978-6726 x308 (800) 735-0555 x308 > > ---------------------------(end of broadcast)--------------------------- > TIP 6: Have you searched our list archives? > > http://archives.postgresql.org > Wm. Brian McCane | Life is full of doors that won't open Search http://recall.maxbaud.net/ | when you knock, equally spaced amid those Usenet http://freenews.maxbaud.net/ | that open when you don't want them to. Auction http://www.sellit-here.com/ | - Roger Zelazny "Blood of Amber"