need help with import - Mailing list pgsql-sql

From Andreas
Subject need help with import
Date
Msg-id 4F3C55C2.8070005@gmx.net
Whole thread Raw
Responses Re: need help with import  ("David Johnston" <polobo@yahoo.com>)
Re: need help with import  ("Raj Mathur (राज माथुर)" <raju@linux-delhi.org>)
List 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.



pgsql-sql by date:

Previous
From: John Fabiani
Date:
Subject: Re: foreign key is it a real key
Next
From: "David Johnston"
Date:
Subject: Re: need help with import