简体   繁体   中英

Insert column data rows (all) in another column table where each row matches the common ID

Table1                                      Table2
column1ID | column1                 column2ID |    column2
     1    |   A                          1    |    copyofA
     2    |   B                          2    |    copyofB

As requested i want to insert in table2 the copyofA copyofB rows etc (around 54k rows) but column2ID has to match column1ID that is already populated.

As per title i want to Insert column data rows (all of them) in another column table where each row matches the common ID column value

I used the usual command

INSERT INTO table2 (column2)
SELECT column1
FROM table1

but unfortunately it seems that the column with the ID value needs some sort of comparing in order to be successful

Output Error

[Err] ERROR:  insert or update on table "table2" violates foreign key constraint "table2_column2ID_fkey"
DETAIL:  Key (column2ID)=(400992) is not present in table "table2".

Last to add that table2 obviously has a foreign key attached to the primary key ID of table1

I think you want an update :

update table2 t2
    set column2 = t1.column1
    from table1 t1
    where t2.id = t1.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM