zoukankan      html  css  js  c++  java
  • PostgreSQL中的onflict

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

    1、不存在则插入,存在则更新

    insert into test values (1,'test',now()) on conflict (id) do update set info=excluded.info,crt_time=excluded.crt_time; 执行操作:INSERT 0 1 查看结果:select * from test; id | info | crt_time ----+------+---------------------------- 1 | test | 2017-04-24 15:27:25.393948 (1 row) insert into test values (1,'hello digoal',now()) on conflict (id) do update set info=excluded.info,crt_time=excluded.crt_time; INSERT 0 1 查看结果:select * from test; id | info | crt_time ----+--------------+---------------------------- 1 | hello digoal | 2017-04-24 15:27:39.140877 (1 row) 
    

    EXCLUDED 代指要插入的记录

    2、不存在则插入,存在则直接返回(不做任何处理)

    insert into test values (1,'hello digoal',now()) on conflict (id) do nothing; INSERT 0 0 insert into test values (1,'pu',now()) on conflict (id) do nothing; INSERT 0 0 insert into test values (2,'pu',now()) on conflict (id) do nothing; INSERT 0 1 select * from test; id | info | crt_time ----+--------------+---------------------------- 1 | hello digoal | 2017-04-24 15:27:39.140877 2 | pu | 2017-04-24 15:28:20.37392 (2 rows) 
    
  • 相关阅读:
    类成员函数的重载、覆盖和隐藏区别 (C++)(转)
    man时括号里的数字是啥意思
    Redis事务
    功能接口
    持久化方式
    宿主
    路由
    静态文件
    Log4Net 配置
    Redis命令与配置
  • 原文地址:https://www.cnblogs.com/AganRun/p/11816127.html
Copyright © 2011-2022 走看看