Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 6F1A56A5FC1 for ; Wed, 15 Feb 2012 21:03:13 -0400 (AST) Received: from mailout-de.gmx.net ([213.165.64.22]) by makus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Rxpkd-0006vY-JF for pgsql-sql@postgresql.org; Thu, 16 Feb 2012 01:03:12 +0000 Received: (qmail invoked by alias); 16 Feb 2012 01:02:58 -0000 Received: from mue-88-130-3-068.dsl.tropolys.de (EHLO [192.168.1.113]) [88.130.3.68] by mail.gmx.net (mp032) with SMTP; 16 Feb 2012 02:02:58 +0100 X-Authenticated: #14269776 X-Provags-ID: V01U2FsdGVkX19h199NCUJh27SagyAMJ1AQB6oZEpswTYAWR3BjMB Q17P2KXbSK73kr Message-ID: <4F3C55C2.8070005@gmx.net> Date: Thu, 16 Feb 2012 02:02:58 +0100 From: Andreas User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:10.0.1) Gecko/20120208 Thunderbird/10.0.1 MIME-Version: 1.0 To: "pgsql-sql@postgresql.org" Subject: need help with import Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201202/44 X-Sequence-Number: 36337 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.