Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id D0D4FC60765 for ; Wed, 15 Feb 2012 21:14:12 -0400 (AST) Received: from smtp110.prem.mail.ac4.yahoo.com ([76.13.13.93]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1RxpvE-0003JX-TC for pgsql-sql@postgresql.org; Thu, 16 Feb 2012 01:14:11 +0000 Received: (qmail 41536 invoked from network); 16 Feb 2012 01:13:55 -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:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=Xx0LUuS86olO+h2VIAGp7DJnuFB7UVsaju860jJi+UjxAPKUyAupujFSubNDlzKJ954YshgYpwDqs+UWr2I2IEr4leZe942TEMVb1h4IH75rqf6aeDg5MPH2rAUUnoi5EZ1nNbMAuNqx2rEdk1oH3q2MH5mPxs41xqxowDbcW+4= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1329354835; bh=733ErFmvdXUp+QPxNxPvc5bVKpmSAVAduSpFEQ1McaY=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=Fgr5rUZpLmmba/g4K+rNQD95YlLQ8gBHAUfhyXxiF9bNGf55bgw4gkNUf4T8JGHwA5Sn91sI0vhyJ1gMzIZyKFwf97B1Nyz5o4GUiR65jCyeokclOUidC9nq0ZvjAf/5Vo2qEervowvK5s47uLx+IeouWv0GdcMeFBdYRL3qfO8= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: XYyX7PMVM1mnya4NPVPoRELvSTtsWfSS68W20TC.cuxw7EG X3FTn1_LQTVUH9yvyW5y9eIUgqZ5_HYmNMs1pbQTgtUYSQJKmsW3RJ0gZ6UX IrsTc_ym7xZrqW2Ut64hrGR5GtYclFqwpdWyHfF8GAloso8u1NlXRkAcmIe. szk9alSu8WkLv6c8z_S6e9geyFscPY8V00WXsLhBJIs3MkaJeWEigG.F4UmH kCrDs6VNo.PrN.kjHyZTOGXw1ipNkS7YDcPcNSOYlY2PvEGirjoAn27irPBX Qnb4_fXBSBusWdWeChlnFZkT3vm5_bxdOJKvb8JQj2Vyc4sfAAsFTsTr4LOS zT5nBFWU259PugTc0UKnPqz9adB5cn8pEt8ndFECI67gXCGVJaLml1OG1rIN fPLX4i7cqd_sRXT_90XQS4VFcNGlFJ8F.DZ1OkvDZl2HDqJW0ZoP5aDmDbq8 jLkOfNz9bUzxcFJv_PDkCx.lC5T7PkW5TsDNb9ejICi.nmRTWbGLZSpJ34MI - X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from WolfDog (polobo@24.93.23.188 with login) by smtp110.prem.mail.ac4.yahoo.com with SMTP; 15 Feb 2012 17:13:54 -0800 PST From: "David Johnston" To: "'Andreas'" , References: <4F3C55C2.8070005@gmx.net> In-Reply-To: <4F3C55C2.8070005@gmx.net> Subject: Re: need help with import Date: Wed, 15 Feb 2012 20:13:49 -0500 Message-ID: <013001ccec48$3db033e0$b9109ba0$@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQGfRsUZaCQhGEVcqovzQMYbsh3RcJaaAQgA Content-Language: en-us X-Pg-Spam-Score: -2.0 (--) X-Archive-Number: 201202/45 X-Sequence-Number: 36338 -----Original Message----- From: pgsql-sql-owner@postgresql.org = [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of Andreas Sent: Wednesday, February 15, 2012 8:03 PM To: pgsql-sql@postgresql.org Subject: [SQL] need help with import 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. -------------------------------------------- You will need to use the temporary table and perform multiple = insert+select. I do not understand where you are confused. It would help to provide = more 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 usually having some descriptive meaning. Is time one of = your components that you are not showing us? Dave -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make = changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql