Postgres UPSERT (INSERT or UPDATE) only if value is different

前端 未结 6 1564
你的背包
你的背包 2021-02-05 17:26

I\'m updating a Postgres 8.4 database (from C# code) and the basic task is simple enough: either UPDATE an existing row or INSERT a new one if one doesn\'t exist yet. Normally I

6条回答
  •  醉话见心
    2021-02-05 17:44

    INSERT INTO table_name(column_list) VALUES(value_list) ON CONFLICT target action;

    https://www.postgresqltutorial.com/postgresql-upsert/

    Dummy example :

    insert into user_profile (user_id, resident_card_no, last_name) values
     (103, '14514367', 'joe_inserted' ) 
    on conflict on constraint user_profile_pk do 
    update set resident_card_no = '14514367', last_name = 'joe_updated';
    

提交回复
热议问题