Quantcast
Channel: Active questions tagged referential-integrity - Database Administrators Stack Exchange
Viewing all articles
Browse latest Browse all 60

For each row create a linked row in another table with auto-generated ID

$
0
0

In Postgres, I have an existing table:

things

thing_idthing_namedestination_id
10thing 10null
15thing 15null

For each row in that table, I want to add rows to two new related tables one of which has an auto-generated ID. I then want to update rows in the old table to point to related new rows.

Desired result:

things

thing_idthing_namedestination_id
10thing 101
15thing 152

parent_destination

destination_id
1
2

child_destination

destination_iddestination_name
1[destination] thing 10
2[destination] thing 15

I have tried to do it here: https://www.db-fiddle.com/f/6iyvCT7BYXPHPi2N2HvNor/1but I can't work out how to return the correct data from result1.


Viewing all articles
Browse latest Browse all 60

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>