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 1h1eDa-0000y2-Jl for pgsql-sql@arkaria.postgresql.org; Wed, 06 Mar 2019 21:36:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h1eDX-000705-Qw for pgsql-sql@arkaria.postgresql.org; Wed, 06 Mar 2019 21:36:47 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h1eDX-0006zr-KB for pgsql-sql@lists.postgresql.org; Wed, 06 Mar 2019 21:36:47 +0000 Received: from server.dayspringpublisher.com ([108.179.223.62]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h1eDT-0007k6-Jr for pgsql-sql@lists.postgresql.org; Wed, 06 Mar 2019 21:36:47 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=dayspringpublisher.com; s=default; h=Content-Type:In-Reply-To:MIME-Version: Date:Message-ID:From:References:Cc:To:Subject:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=vXo+iFp6bkuh8fwe9F0zMmQI+nFbM8sBpgA2IsBebvA=; b=k0OllXdYMPat1bZxwnXtc15Ne kVlJ4zIn81GhYkJ2i/27/BDHI4zChrc8RNz16/AIjQXlPTFnFO/qe5T5+SLQFFKoMVBZP2nnaPrv2 EK6IvUdJLtSrI5UjoI2xAsN3mZ; Received: from [104.218.91.150] (port=46156 helo=[192.168.1.9]) by server.dayspringpublisher.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.91) (envelope-from ) id 1h1eDF-0006yl-8z; Wed, 06 Mar 2019 14:36:29 -0700 Subject: Re: INSERT / UPDATE into 2 inner joined table simultaneously To: Christopher Swingley Cc: pgsql-sql@lists.postgresql.org References: <38a1b75c-2a29-5833-cbd6-6b76dca019da@dayspringpublisher.com> From: Lou Message-ID: Date: Wed, 6 Mar 2019 15:36:21 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------2134BD995E0B91FA974D4FAA" Content-Language: en-US X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.dayspringpublisher.com X-AntiAbuse: Original Domain - lists.postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - dayspringpublisher.com X-Get-Message-Sender-Via: server.dayspringpublisher.com: authenticated_id: lou@dayspringpublisher.com X-Authenticated-Sender: server.dayspringpublisher.com: lou@dayspringpublisher.com X-Source: X-Source-Args: X-Source-Dir: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------2134BD995E0B91FA974D4FAA Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Hi Chris, Thank you for answering so quickly. On 3/6/19 2:11 PM, Christopher Swingley wrote: > Lou, > > On Wed, Mar 6, 2019 at 10:59 AM Lou wrote: >> How can I INSERT new rows into both tables simultaneously with automatically created id numbers, and how can I UPDATE both tables simultaneously? > Although I have no idea why you would want to do this, you can insert > data into two tables with one query using a common table expression: > > WITH cinsert AS ( > INSERT INTO c (id, name) VALUES (1, 'Jones') > RETURNING id, name) > INSERT INTO p (id, name) (SELECT * FROM cinsert); > > Cheers, > > Chris Sorry, I did not clearly explain what I'm trying to do. The two tables contain different data. The c table contains company data, and the p table contains personal data about my contact person in that company. The only data the two tables share is the contents of c.id which must be inserted into the p.c_id field (so that the two tables can later be inner joined by SELECT). I've programmed a data entry screen which shows the fields of both tables together, so that the data for both tables can be inserted or edited in one sitting. The data for both tables needs to be saved at the same time so that the id number of table c can be copied into the c_id field of table p. Lou --------------2134BD995E0B91FA974D4FAA Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit
Hi Chris,

Thank you for answering so quickly.

On 3/6/19 2:11 PM, Christopher Swingley wrote:
Lou,

On Wed, Mar 6, 2019 at 10:59 AM Lou <lou@dayspringpublisher.com> wrote:
How can I INSERT new rows into both tables simultaneously with automatically created id numbers, and how can I UPDATE both tables simultaneously?
Although I have no idea why you would want to do this, you can insert
data into two tables with one query using a common table expression:

WITH cinsert AS (
    INSERT INTO c (id, name) VALUES (1, 'Jones')
    RETURNING id, name)
INSERT INTO p (id, name) (SELECT * FROM cinsert);

Cheers,

Chris

Sorry, I did not clearly explain what I'm trying to do. The two tables contain different data. The c table contains company data, and the p table contains personal data about my contact person in that company. The only data the two tables share is the contents of c.id which must be inserted into the p.c_id field (so that the two tables can later be inner joined by SELECT). I've programmed a data entry screen which shows the fields of both tables together, so that the data for both tables can be inserted or edited in one sitting. The data for both tables needs to be saved at the same time so that the id number of table c can be copied into the c_id field of table p.

Lou



--------------2134BD995E0B91FA974D4FAA--