pg.ddx.io pgsql-admin@postgresql.org mailing list archive
help / color / mirror / Atom feedRemoving records that violate foreign key
3+ messages / 3 participants
[nested] [flat]
* Removing records that violate foreign key
@ 2002-06-18 17:12 Cliff Wells <logiplexsoftware@earthlink.net>
2002-06-18 19:47 ` Re: Removing records that violate foreign key Stephan Szabo <sszabo@megazone23.bigpanda.com>
0 siblings, 1 reply; 3+ messages in thread
From: Cliff Wells @ 2002-06-18 17:12 UTC (permalink / raw)
To: pgsql-admin
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
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Removing records that violate foreign key
2002-06-18 17:12 Removing records that violate foreign key Cliff Wells <logiplexsoftware@earthlink.net>
@ 2002-06-18 19:47 ` Stephan Szabo <sszabo@megazone23.bigpanda.com>
0 siblings, 0 replies; 3+ messages in thread
From: Stephan Szabo @ 2002-06-18 19:47 UTC (permalink / raw)
To: Cliff Wells <logiplexsoftware@earthlink.net>; +Cc: pgsql-admin
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?
For match full maybe something like:
delete from fk where not exists (
select * from pk where pk.pkkeycol1=fk.fkkeycol1 [and ...]
);
For the unspecified match type, I think you need to add
fk.fkkeycol1 is not null [and ...]
clauses to the top level where.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Removing records that violate foreign key
@ 2002-06-18 20:38 Brian McCane <bmccane@mccons.net>
0 siblings, 0 replies; 3+ messages in thread
From: Brian McCane @ 2002-06-18 20:38 UTC (permalink / raw)
To: pgsql-admin
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"
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2002-06-18 20:38 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-06-18 17:12 Removing records that violate foreign key Cliff Wells <logiplexsoftware@earthlink.net>
2002-06-18 19:47 ` Stephan Szabo <sszabo@megazone23.bigpanda.com>
2002-06-18 20:38 Re: Removing records that violate foreign key Brian McCane <bmccane@mccons.net>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox