adtoctor
adtoctor

Reputation: 58

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

Upvotes: 0

Views: 35

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269893

I think you want an update:

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

Upvotes: 1

Related Questions