赞
踩
说明
在PostgreSQL下使用 conflict。
需要先设置唯一键。(也可以设置多个字段的唯一键)
alter table stu add constraint name_cons unique(name);
如果 没有设置唯一键,错误信息如下:
insert into stu values('王二',35) on conflict(name) do update set age= 35
> ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
> 时间: 0.009s
insert into stu values('王二',35) on conflict(name) do update set age=35;
其他案例参考:
insert into tbl (sid, v1, crt_time) values (:sid, :v1, now())
on conflict (sid) do update set
v1=excluded.v1,
crt_time=excluded.crt_time,
cnt=tbl.cnt+1,
sum_v=case tbl.cnt when 1 then tbl.v1+excluded.v1 else tbl.sum_v+excluded.v1 end,
min_v=least(tbl.min_v, excluded.v1),
max_v=greatest(tbl.max_v, excluded.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。