need help with import

From: Andreas <maps(dot)on(at)gmx(dot)net>
To: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: need help with import
Date: 2012-02-16 01:02:58
Message-ID: 4F3C55C2.8070005@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi
I get CSV files to import.
Th structure is like this.
main part, sub part
Could be like this

A, a1
A, a2
A, a3
B, b1
B, b2

The database has a table for main_part and one for sub_part.
The relation needs to be n:m so there is a relation table that holds (
main_id, sub_id ).
The 2 primary keys main_part.id and sub_part.id are both serials.

Is there a way to do an import with SQL?

I can read the CSV into a temporary table
and I can do a
INSERT INTO main_part ( ... ) SELECT DISTINCT main columns FROM import;
as well as a
INSERT INTO sub_part ( ... ) SELECT sub columns FROM import;

But how would I know what main_id and sub_id to insert into the n:m
relation?

At first when I do the import the relation is actually 1:n.

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message David Johnston 2012-02-16 01:13:49 Re: need help with import
Previous Message John Fabiani 2012-02-13 17:30:38 Re: foreign key is it a real key