Search Postgresql Archives

elegant way to fill a table with serial

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I've to fill something like:

create table DESTtable1 (
  pk1 serial primary key,
--  rest of stuff
);

create table DESTtable2 (
  pk2 serial primary key,
  fk1 int references DESTtable1(pk1)
-- rest of stuff
);

from data that are such way

create table SRCtable1 (
  pk1 serial primary key,
--  rest of stuff
);

create table SRCtable2 (
  fk1 int references DESTtable1(pk1)
  ak int not null,
-- rest of stuff
  unique(fk1,aa)
);

substantially (fk1,aa) -> pk2

the best way I thought about it is

create table temp1 (
  pk2 serial primary key,
  fk1 int references DESTtable1(pk1),
  ak int not null
);

insert into temp1 (fk1,ak) select ....

insert into DESTtable2 (pk2,fk1,...)
  select (pk2,fk1...) from SRC2
  join temp1 ...
);

alter sequence seq_DESTtable2 RESTART currval('seq_temp1');

But it looks awful.

-- 
Ivan Sergio Borgonovo
http://www.webthatworks.it


---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
       subscribe-nomail command to majordomo@xxxxxxxxxxxxxx so that your
       message can get through to the mailing list cleanly

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux