Re: INSERT / UPDATE into 2 inner joined table simultaneously

Christopher Swingley <[email protected]> Wed, 6 Mar 2019 11:11:23 -0900
Newsgroups gmane.comp.db.postgresql.sql
Message-ID <CAHsw449uo18j9fSxd-KZgwWSSf3nTqe2v6ho86U7OGNXFC4S2g@mail.gmail.com>
Lou,

On Wed, Mar 6, 2019 at 10:59 AM Lou <[email protected]> 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
-- 
Christopher Swingley
Fairbanks, Alaska
http://swingleydev.com/
[email protected]