matching rows differing only by fkey,pkey

From: "Matthew Nuzum" <matt(at)followers(dot)net>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: matching rows differing only by fkey,pkey
Date: 2004-06-22 16:34:35
Message-ID: 200406221634.i5MGYcnb009716@ms-smtp-02.tampabay.rr.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I'm duplicating some fields in the table but the duplicates will have a new
primary key and a new foreign key. For example,

Table "b" looks like this:
bid (pkey default value is a sequence)
aid (fkey)
field1
field2
field3

INSERT INTO b (aid, field1, field2, field3)
SELECT 23, field1, field2, field3 from b where aid = 22;

"b" is the middle table of a many to many relationship. The end result is
to duplicate the data for a particular record in table "a" so that all of
it's related data in tables "b" and "c" is duplicated.

When the relationships are one to one or one to many this process is easy,
however sometimes there's a many to many relationship.

It seems that a helpful tool would be a query that can return just the pkey
of the original record copied from and the pkey of the newly created record.

For example, if the b table looked like this after a copy of 3 rows:
bid | aid | field1 | field2 | field3
1 | 22 | abc | 123 | abc123
2 | 22 | xyz | 456 | xyz456
3 | 22 | pdq | 789 | pdq789
4 | 23 | abc | 123 | abc123
5 | 23 | xyz | 456 | xyz456
6 | 23 | pdq | 789 | pdq789

I'd like to get this:
oldbid | newbid
1 | 4
2 | 5
3 | 6

Any ideas? If someone has an alternate method of achieving the same result
I'd be excited to hear about it.

Matthew Nuzum | ISPs: Make $200 - $5,000 per referral by
www.followers.net | recomending Elite CMS to your customers!
matt(at)followers(dot)net | http://www.followers.net/isp

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Huxton 2004-06-22 17:16:16 Re: matching rows differing only by fkey,pkey
Previous Message Alvaro Sanchez-Mariscal 2004-06-22 14:56:10 Unrecognized node type