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) 
    
  • 相关阅读:
    由一段代码说开去——set
    由一段代码说开去——list
    day 11
    day 16
    day 10
    day 13
    day 9
    day 18
    day 12
    day 14
  • 原文地址:https://www.cnblogs.com/AganRun/p/11816127.html
Copyright © 2011-2022 走看看