Re: INSERT / UPDATE into 2 inner joined table simultaneously

From: Christopher Swingley <cswingle(at)swingleydev(dot)com>
To: Lou <lou(at)dayspringpublisher(dot)com>
Cc: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: Re: INSERT / UPDATE into 2 inner joined table simultaneously
Date: 2019-03-06 21:41:13
Message-ID: CAHsw449htrKYSpGa8ZpNh1W+DjZF1=uO3FFGeHvVmK9e2iUEdg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Lou,

On Wed, Mar 6, 2019 at 12:36 PM Lou <lou(at)dayspringpublisher(dot)com> wrote:
> 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.

I think what you want to do is use a transaction (BEGIN), insert the
data into table c RETURNING id, then insert the data into table p
using the id returned from the first query. If there were no errors,
COMMIT the transaction, otherwise ROLLBACK. It's two queries, but
because of the transaction, it's happening atomically within the
database.

Cheers,

Chris
--
Christopher Swingley
Fairbanks, Alaska
http://swingleydev.com/
cswingle(at)swingleydev(dot)com

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Steve Midgley 2019-03-06 22:45:41 Re: INSERT / UPDATE into 2 inner joined table simultaneously
Previous Message Lou 2019-03-06 21:36:21 Re: INSERT / UPDATE into 2 inner joined table simultaneously