Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 578E3C9F2CF for ; Wed, 15 Feb 2012 22:42:02 -0400 (AST) Received: from nm15-vm3.bullet.mail.ne1.yahoo.com ([98.138.91.145]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1RxrIE-000556-Fp for pgsql-sql@postgresql.org; Thu, 16 Feb 2012 02:42:00 +0000 Received: from [98.138.90.50] by nm15.bullet.mail.ne1.yahoo.com with NNFMP; 16 Feb 2012 02:41:44 -0000 Received: from [98.138.89.250] by tm3.bullet.mail.ne1.yahoo.com with NNFMP; 16 Feb 2012 02:41:44 -0000 Received: from [127.0.0.1] by omp1042.mail.ne1.yahoo.com with NNFMP; 16 Feb 2012 02:41:43 -0000 X-Yahoo-Newman-Id: 993607.24961.bm@omp1042.mail.ne1.yahoo.com Received: (qmail 40389 invoked from network); 16 Feb 2012 02:41:43 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:In-Reply-To:Mime-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Cc:X-Mailer:From:Subject:Date:To; b=tZu+GDYASMWS8G/I/yf2kYvTYFutZOmfnTZ6oOvCYFyHl1rfhspdeu2r19XX4tLnMg8s4MB0P+KJvphJnTB3I8J0tqkppooEiOUEjBjEh7C4yKh7mLW7tPSXNoLeoYrbx4R2NZ0EWOLgBKQtSoqISiKsNuoN9FDFGrgFmZVvc3M= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1329360103; bh=jLAJt/rAi3xc1M/FyWC/0JBzKYZzPvmNPktGwiS3uHY=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:In-Reply-To:Mime-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Cc:X-Mailer:From:Subject:Date:To; b=mSALqfpl2OVhpojkFtGEzMT8epxXWK3rNEg/5gOKfjGk1gSbpNBcSy+rDkgmz+ZGW0iJhHqTcEnMI9FTMQ1A/Wc1fmSunu4VrnKE/yPqD4VQOaMRv+yn6kAI6bg9ULkVWcBPzCM+vOHHzBauDImzU7YPH/Gplxi/87YHcnQpQwU= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: V7DYmi4VM1nnS75DeYelpmDR81rf.MrQEJSrerIUiCeaoZg DhUbohED_pLIkX310rCofGezWEUkdRx.qyVDbST7b_lytXTt9kXAxTXCe4kc Q1A245tAIQlV5I.80hRWJ99x9rOYLBapBRyis89hoxPiE5iKY4.r_tKD4t2V PdINeGdifLKLXmaMrKIWDzXftVA.aS7a2YgS8hFXNm0pxoHBVTsyq8PaBqbe 47PlwgdoRzOlPBJGaP8E4rPNhH9.H_9R85PEybQbBOqCYB96GYKJosC3MjXj BpN7lFKQqpFBd_C0OddFjxKJl2CFDVeB6yW5j5E1eMUmoc2JP9FVkxn0eT2w cjpKWszaN137ecO7WBozArqYj0RtIFx_Qt0gc0OqFysbXVDYqjR4XBUQAUKC XiVQJ2HRQEL.Vz31ABlz_HAbBdoxlVxvlBGJdNxwNKyxb86G4hiWKVZp7waV oz.9kq01fmNJq2qJsYTv9eSpWPP5WhzR8Exv4vUyb7SBQks6xJyUh1VHdvyE - X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from [192.12.17.102] (polobo@24.93.23.188 with xymcookie) by smtp114-mob.biz.mail.ne1.yahoo.com with SMTP; 15 Feb 2012 18:41:43 -0800 PST References: <4F3C55C2.8070005@gmx.net> <013001ccec48$3db033e0$b9109ba0$@yahoo.com> <4F3C6480.7040301@gmx.net> In-Reply-To: <4F3C6480.7040301@gmx.net> Mime-Version: 1.0 (1.0) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Message-Id: Cc: "pgsql-sql@postgresql.org" X-Mailer: iPad Mail (9A405) From: David Johnston Subject: Re: need help with import Date: Wed, 15 Feb 2012 21:41:43 -0500 To: Andreas X-Pg-Spam-Score: -2.0 (--) X-Archive-Number: 201202/47 X-Sequence-Number: 36340 On Feb 15, 2012, at 21:05, Andreas wrote: > Am 16.02.2012 02:13, schrieb David Johnston: >> -----Original Message----- >> From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.o= rg] On Behalf Of Andreas >> Sent: Wednesday, February 15, 2012 8:03 PM >> To: pgsql-sql@postgresql.org >> Subject: [SQL] need help with import >>=20 >> Hi >> I get CSV files to import. >> Th structure is like this. >> main part, sub part >> Could be like this >>=20 >> A, a1 >> A, a2 >> A, a3 >> B, b1 >> B, b2 >>=20 >> 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 ( ma= in_id, sub_id ). >> The 2 primary keys main_part.id and sub_part.id are both serials. >>=20 >> Is there a way to do an import with SQL? >>=20 >> 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 I= NTO sub_part ( ... ) SELECT sub columns FROM import; >>=20 >> But how would I know what main_id and sub_id to insert into the n:m relat= ion? >>=20 >> At first when I do the import the relation is actually 1:n. >>=20 >> -------------------------------------------- >>=20 >> You will need to use the temporary table and perform multiple insert+sele= ct. >>=20 >> I do not understand where you are confused. It would help to provide mor= e meaningful sample data and/or the final result you are trying to achieve. = Keep in mind any n:m setup requires three tables with the joining table usu= ally having some descriptive meaning. Is time one of your components that y= ou are not showing us? >>=20 >>=20 > As you say there are 3 tables > main_part ( id serial primary key, ... ) > sub_part ( id serial primary key, ... ) > main_to_sub ( main_id, sub_id ) >=20 > I would read the csv into a temporary table "import" and insert the main c= olumns into main_part (). > Then there are new tuples in main_part() > 42, A > 43, B >=20 > Now I insert the sub columns into sub_part() > I'll get e.g. > 1000, a1 > 1001, a2 > 1002, a3 > 1003, b1 > 1004, b2 >=20 > To fill up main_to_sub ( ... ) I needed to know which sub_id belongs to wh= ich main_id. > ( 42, 1000 ) > ( 42, 1001 ) > ( 42, 1002 ) > ( 43, 1003 ) > ( 43, 1004 ) >=20 > I could compare every main-column in "import" to every related data-column= in main_part to get the newly created main_id and do the same with every su= b-data-column but this seems to be a wee bit tedious. >=20 > Is there a more elegant way hat I don't see, yet? >=20 Ditch the whole idea of using a sequence and take your primary keys from the= source data. Otherwise yes, you will need to perform the join between the i= mport and live tables to determine the newly created identifier. The question to answer is if you see the same values in subsequent import fi= les do you create a new sequence value or reuse the existing value? Why? David J.=