Skip site navigation (1) Skip section navigation (2)

SQL tree duplication

From: "Philippe Lang" <philippe(dot)lang(at)attiksystem(dot)ch>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: SQL tree duplication
Date: 2008-01-24 09:42:26
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-sql

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.


Philippe Lang


pgsql-sql by date

Next:From: tvDate: 2008-01-24 14:09:09
Subject: Re: SQL tree duplication
Previous:From: A. KretschmerDate: 2008-01-24 08:21:02
Subject: Re: date format

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group