话不多说直接上sql
insert into student values(4,'d') on conflict(id) do update set name='as';
以上就是实现如果id存在冲突时,则更新name字段。不存在冲突时就插入的SQL语句。
不过大家很可以能回遇到下列问题
ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
这是提示我们要求该字段(id)定义为唯一(unique)
以下是实现定义字段为唯一的字段
alter table student add constraint id_cons unique(id);