Re: Question on imports with foreign keys - Mailing list pgsql-sql

From Jasen Betts
Subject Re: Question on imports with foreign keys
Date
Msg-id jc0obn$521$1@reversiblemaps.ath.cx
Whole thread Raw
In response to Question on imports with foreign keys  (Andreas <maps.on@gmx.net>)
List pgsql-sql
On 2011-12-08, Andreas <maps.on@gmx.net> wrote:
> Hi,
>
> suppose you need to import a csv with standard ciolums like name, 
> adress, phone, ... and some additional text columns that need to be 
> split off into referenced tables.
...
> How is the easiest way to to find the customer.id of the new customers 
> so I can insert the projectinfos?

create table tmp.customer (id integer, name text, addr text)
copy tmp.customer ( id,name,addr ) from stdin ;... alter table tmp.customer add column new_id integer default
nextval('customer_id.seq'::regclass);

(here the default is the same default that the customer table uses for
its id.)
now you can use "insert ... select ..." to insert these new records
explicitly using new_id to fill the id column of the customer table.

iport the other csv data into similar tables also 
and use join on the old id in tmp.customer to get the new id for
copying the other imported tabled.




-- 
⚂⚃ 100% natural



pgsql-sql by date:

Previous
From: Jasen Betts
Date:
Subject: Re: conditional FROM
Next
From: Jasen Betts
Date:
Subject: Re: partitions versus databases