'Copy Row into a Different Table before updating it
I have Table aa and bb. The are created identical except for one additional column id in bb of type serial so that it serially increments by default.
aa has Primary Key column of col0. bb also obviously has this too but its Primary Key is id column.
Now my ask is that every time I INSERT INTO aa and if there is a conflict on col0, I want to copy the entire existing row and INSERT INTO bb and after that update the row of aa with the new values.
So net-net the following are the columns of aa (primary key col0):
col0, col1, col2
and the following are the columns of bb (primary key id):
id, col0, col1, col2
In pseudo code I want something like:
INSERT INTO aa (col0, col2) VALUES ('abc', 'def')
ON CONFLICT (col0) DO UPDATE
INSERT INTO bb (col0, col1, col2) VALUES (aa.col0, aa.col1, aa.col2) <<< COPY Existing row
SET (col0, col2) = (EXCLUDED.col0, EXCLUDED.col2) <<< Now update `aa` with the new values
How do I go about doing it?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
