Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 637632E011C for ; Thu, 24 Jan 2008 10:07:55 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 56667-02 for ; Thu, 24 Jan 2008 10:07:46 -0400 (AST) Received: from nathalia.gransy.com (nathalia.gransy.com [82.208.29.194]) by postgresql.org (Postfix) with ESMTP id 7BCF62E007F for ; Thu, 24 Jan 2008 10:07:51 -0400 (AST) Received: from localhost (localhost [127.0.0.1]) by nathalia.gransy.com (Postfix) with ESMTP id 044817F61D; Thu, 24 Jan 2008 15:09:10 +0100 (CET) Received: from nathalia.gransy.com ([127.0.0.1]) by localhost (nathalia.gransy.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id wkNmKlFkr6ni; Thu, 24 Jan 2008 15:09:09 +0100 (CET) Received: from mail.fuzzy.cz (localhost [127.0.0.1]) by nathalia.gransy.com (Postfix) with ESMTP id A93777F615; Thu, 24 Jan 2008 15:09:09 +0100 (CET) Received: from 217.77.161.17 (SquirrelMail authenticated user tv@fuzzy.cz) by mail.fuzzy.cz with HTTP; Thu, 24 Jan 2008 15:09:09 +0100 (CET) Message-ID: <23962.217.77.161.17.1201183749.squirrel@mail.fuzzy.cz> In-Reply-To: <6C0CF58A187DA5479245E0830AF84F4218D00A@poweredge.attiksystem.ch> References: <6C0CF58A187DA5479245E0830AF84F4218D00A@poweredge.attiksystem.ch> Date: Thu, 24 Jan 2008 15:09:09 +0100 (CET) Subject: Re: SQL tree duplication From: tv@fuzzy.cz To: "Philippe Lang" Cc: pgsql-sql@postgresql.org User-Agent: SquirrelMail/1.4.10a MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-2 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/181 X-Sequence-Number: 30034 > Hi, > > I have the following kind of sql structure, in 3 levels: > > ----------------------- > create table documents ( > id serial, > name varchar(50), > primary key (id) > ); > > create table lines ( > id serial, > name varchar(50), > document_id integer, > primary key (id), > foreign key (document_id) references documents (id) > ); > > create table line_details ( > id serial, > name varchar(50), > line_id integer, > primary key (id), > foreign key (line_id) references lines (id) > ); > ----------------------- > > I'd like to be able to "duplicate" a document, with all of its lines and > line details. > > Is there any easy way to do that with Postgresql? The only solution I > can think of at the moment is to loop through all lines and line > details, and replace foreign keys properly with values fetch with > "currval". It should work just fine, but I was wondering if some > advanced features of Postgresql could help in this situation. You will have to do that in 3 steps (one for each table), but looping may not be necessary - just use INSERT ... SELECT ... syntax. Something like INSERT INTO Lines SELECT FROM Lines WHERE document_id = OLD_ID; But it depends on primary keys in the Lines and Line_details tables - if the primary keys are composed (and the document_id is part of them) then there is no problem with duplicities. Otherwise you'll have to solve it somehow, and looping may be necessary. Tomas