postgresql实现replace into功能

PostgreSQL 9.5-

使用函数或with实现

create table test(id int primary key, info text, crt_time timestamp);
with upsert as (update test set info='test',crt_time=now() where id=1 returning *) insert into test select 1,'test',now() where not exists (select 1 from upsert where id=1);  

PostgreSQL 9.5+

PostgreSQL 9.5 引入了一项新功能,UPSERT(insert on conflict do),当插入遇到约束错误时,直接返回,或者改为执行UPDATE。

INSERT INTO table_name VALUES() ON conflict (唯一索引字段) DO
UPDATE ...
原创文章 11 获赞 1 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_39810091/article/details/105966200