Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hTSP9-0004w7-Hz for pgsql-general@arkaria.postgresql.org; Wed, 22 May 2019 14:39:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hTSP8-0008Bo-6w for pgsql-general@arkaria.postgresql.org; Wed, 22 May 2019 14:39:42 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hTSO1-0003kp-Rb for pgsql-general@lists.postgresql.org; Wed, 22 May 2019 14:38:33 +0000 Received: from mail.appl-ecosys.com ([50.126.108.78]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hTSNz-0007mD-EL for pgsql-general@lists.postgresql.org; Wed, 22 May 2019 14:38:32 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id 68A8D99387 for ; Wed, 22 May 2019 07:38:29 -0700 (PDT) Date: Wed, 22 May 2019 07:38:29 -0700 (PDT) From: Rich Shepard To: "pgsql-generallists.postgresql.org" Subject: Re: Bulk inserts into two (related) tables In-Reply-To: Message-ID: References: User-Agent: Alpine 2.20 (LNX 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8BIT List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, 22 May 2019, Jeremy Finzel wrote: > There's absolutely no need to use anything beyond SQL here, though you > could if you want to. Jeremy, This is a new experience for me so I didn't think of a SQL solution. > I really wonder how much we are just talking past each other simply because > we don't know what your data looks like, so we can't show you how our > examples apply to your use case. If you provided a sample scrubbed data > file, this whole thread probably would have been much shorter :). Can you > do that? Not necessary; see below. Also, these data come from a regulator and provided as an Excel spreadsheet. If they were extracted from a database then that was very poorly designed because there's no consistency in how fields/columns are formatted. This requires manual cleaning. Each row in the source file (exported from the spreadsheet as .csv and renamed to .txt for processing in emacs and awk) is a mixture of attributes that belong in either or both of the organization and people tables in my database. An awk script will extract the appropriate fields for each table. > You told Francisco that the data file does not have a unique org name that > could be used as a unique organization identifier. However you seem to > have contradicted that by responding favorably to this solution: The org_name is not the PK; the org_id is. This was assigned by postgres when the original rows were inserted. Now, I can add the org_id in the values to be inserted as I know the maximum org_id number in that table. > INSERT INTO PEOPLE (id, name, org_id) VALUES (1, ‘Bob’, (SELECT org_id FROM > org WHERE org_name=‘Main Office’)) Question: do I use this same syntax for each row to be inserted or can I make it one long insert statement by separating the parenthesized values with commas as I do when I update multiple rows in a table? Thanks very much, Rich