Re: copy row tree

From: Richard Huxton <dev(at)archonet(dot)com>
To: Toni Casueps <casueps(at)hotmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: copy row tree
Date: 2007-01-17 12:23:06
Message-ID: 45AE152A.5030501@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Toni Casueps wrote:
>
> I have a set of tables with one-to-many relationships between them:
>
> T1 <-->> T2 <-->> T3 <-->> T4
>
> I need to copy some rows of these tables to another set of tables which
> have the same fields.

There's no shortcut.

BEGIN;
INSERT INTO copy_t1 SELECT * FROM t1 WHERE id IN (123, 456);
INSERT INTO copy_t2 SELECT * FROM t2 WHERE t2_t1_ref IN (SELECT id FROM
t1 WHERE id IN (123, 456));
INSERT INTO copy_t3 SELECT * FROM t3 WHERE t3_t2_ref IN (SELECT ... FROM
t2 WHERE ...)
...etc...
COMMIT;

--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Thobiyas 2007-01-17 12:33:15 help me to solve the problem
Previous Message Richard Huxton 2007-01-17 12:20:24 Re: Controlling memory of session