PostgreSQL multi-value upserts

后端 未结 2 2017
北海茫月
北海茫月 2021-01-14 17:09

is it possible to perform a multi-value upsert in PostgreSQL? I know multi-value inserts exist, as do the \"ON CONFLICT\" key words to perform an update if the key is viola

相关标签:
2条回答
  • 2021-01-14 18:00

    Multi-valued upsert is definitely possible, and a significant part of why insert ... on conflict ... was implemented.

    CREATE TABLE table1(col1 int, col2 text, constraint theconstraint unique(col1));
    
    INSERT INTO table1 VALUES (1, 'parrot'), (4, 'turkey');
    
    INSERT INTO table1 VALUES (1, 'foo'), (2,'bar'), (3,'baz')
    ON CONFLICT ON CONSTRAINT theconstraint
    DO UPDATE SET col2 = EXCLUDED.col2;
    

    results in

    regress=> SELECT * FROM table1 ORDER BY col1;
     col1 | col2 
    ------+------
        1 | foo
        2 | bar
        3 | baz
        4 | turkey
    (4 rows)
    

    If the docs were unclear, please submit appropriate feedback to the pgsql-general mailing list. Or even better, propose a patch to the docs.

    0 讨论(0)
  • 2021-01-14 18:03

    1.Before the insert

    2.Command

    3.After the insert

    0 讨论(0)
提交回复
热议问题