Unix Technical Forum

SQL tree duplication

This is a discussion on SQL tree duplication within the pgsql Sql forums, part of the PostgreSQL category; --> Hi, I have the following kind of sql structure, in 3 levels: ----------------------- create table documents ( id serial, ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 06:00 PM
Philippe Lang
 
Posts: n/a
Default SQL tree duplication

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.

Regards,

Philippe Lang

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 06:00 PM
tv@fuzzy.cz
 
Posts: n/a
Default Re: SQL tree duplication

> 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


---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:29 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com