加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql9.5新特性upsert

发布时间:2020-12-13 17:11:49 所属栏目:百科 来源:网络整理
导读:这个特性的意思是insert数据时,如果不冲突,就insert成功,如果冲突,就执行update操作。 先看简单代码: postgres=#createtablet(id1intprimarykey,id2int,d1text,d2text);CREATETABLEpostgres=#insertintotvalues(1,1,'1','1');INSERT01如果insert数据主

这个特性的意思是insert数据时,如果不冲突,就insert成功,如果冲突,就执行update操作。

先看简单代码:

postgres=#createtablet(id1intprimarykey,id2int,d1text,d2text);
CREATETABLE
postgres=#insertintotvalues(1,1,'1','1');
INSERT01
如果insert数据主键冲突,则执行update。
postgres=#insertintotvalues(1,2,'2','2')onconflict(id1)doupdatesetd1=excluded.d1,d2=excluded.d2;
INSERT01
postgres=#select*fromt;
id1|id2|d1|d2
-----+-----+----+----
1|1|2|2
(1row)
如果insert数据主键冲突,不执行任何操作。
postgres=#insertintotvalues(1,'3','3')onconflict(id1)donothing;
INSERT00
postgres=#select*fromt;
id1|id2|d1|d2
-----+-----+----+----
1|1|2|2
(1row)

再来看非主键情况。

postgres=#truncatetablet;
TRUNCATETABLE
postgres=#insertintotvalues(1,'1');
INSERT01
postgres=#insertintotvalues(1,'2')onconflict(id2)doupdatesetd1=excluded.d1,d2=excluded.d2;
ERROR:thereisnouniqueorexclusionconstraintmatchingtheONCONFLICTspecification
STATEMENT:insertintotvalues(1,d2=excluded.d2;
ERROR:thereisnouniqueorexclusionconstraintmatchingtheONCONFLICTspecification
没有排他约束,保存。添加唯一索引后执行。
postgres=#createuniqueindexidx_t_id2ont(id2);
CREATEINDEX
postgres=#insertintotvalues(1,d2=excluded.d2;
INSERT01
postgres=#select*fromt;
id1|id2|d1|d2
-----+-----+----+----
1|1|2|2
(1row)

唯一索引为null的情况

postgres=#truncatetablet;
TRUNCATETABLE
postgres=#insertintotvalues(1,null,'1');
INSERT01
postgres=#insertintotvalues(2,d2=excluded.d2;
INSERT01
postgres=#select*fromt;
id1|id2|d1|d2
-----+-----+----+----
1||1|1
2||2|2
(2rows)

update常量的情况

postgres=#truncatetablet;
TRUNCATETABLE
postgres=#insertintotvalues(1,'2')onconflict(id1)doupdatesetd1='a',d2='b';
INSERT01
postgres=#select*fromt;
id1|id2|d1|d2
-----+-----+----+----
1|1|a|b
(1row)

后记

要了解更详细的信息,还是看官网文档:http://www.postgresql.org/docs/9.5/static/sql-insert.html ,我这里只写了一部分简单的情况。

如果一个表有30个字段,只有第一个字段是主键,那么insert冲突的时候,是否应该写29个update赋值语句呢?这个感觉还有待改进。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读