微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PostgreSQL 实现MySQL "insert ignore" 语法

MysqL熟悉的人可能都知道,MysqL一个“insert ig@R_404_6462@e" 语法来忽略已经存在的记录。 Postgresql暂时不提供这样的语法,但是可以用其他方法来代替。


以下为本次演示的样例表结构以及数据。
t_girl=# \d insert_ig@R_404_6462@e                         
           Table "ytt.insert_ig@R_404_6462@e"
  Column  |          Type          | Modifiers 
----------+------------------------+-----------
 id       | integer                | not null
 log_time | time without time zone | 
Indexes:
    "insert_ig@R_404_6462@e_pkey" PRIMARY KEY,btree (id)


t_girl=# select * from insert_ig@R_404_6462@e;
 id |    log_time    
----+----------------
  1 | 14:44:12.37185
(1 row)




我来演示下几种代替方法


第一种方法, 用自带的规则(RULE)来实现。


t_girl=# create rule r_insert_ig@R_404_6462@e as on insert to insert_ig@R_404_6462@e where exists (select 1 from insert_ig@R_404_6462@e where id = new.id) do instead nothing;    
CREATE RULE


这时,我们插入两条记录,其中一条的主键值已经存在,直接忽略掉。 实际插入的记录数为1.
t_girl=# insert into insert_ig@R_404_6462@e values(1,current_time),(2,current_time);
INSERT 0 1
t_girl=# select * from insert_ig@R_404_6462@e;
 id |    log_time     
----+-----------------
  1 | 14:44:12.37185
  2 | 14:48:22.222848
(2 rows)



第二种方法, 建立一个返回NULL的触发器函数。 那么函数体如下:


t_girl=# create or replace function sp_insert_ig@R_404_6462@e() returns trigger as 
 $ytt$
 begin
   perform  1 from insert_ig@R_404_6462@e where id = new.id;
   if found then
     return null;
  end if;
  return new;
end;
$ytt$ language 'plpgsql';
CREATE FUNCTION


对应的触发器如下:
t_girl=# create trigger tr_ib_insert_ig@R_404_6462@e before insert on insert_ig@R_404_6462@e for each row execute procedure sp_insert_ig@R_404_6462@e();
CREATE TRIGGER


继续插入两条记录。
t_girl=# insert into insert_ig@R_404_6462@e values (3,current_time);
INSERT 0 1
t_girl=# select * from insert_ig@R_404_6462@e;
 id |    log_time     
----+-----------------
  1 | 14:44:12.37185
  2 | 14:48:22.222848
  3 | 15:05:33.198847
(3 rows)


OK。目的达到了。



第三种方法,用WITH来实现。


t_girl=# insert into insert_ig@R_404_6462@e 
		with ytt_test(f1,f2) as (
							values(6,(3,current_time)
							) 
							select a.* from ytt_test as a where a.f1 not in (select id from insert_ig@R_404_6462@e as b);                          
INSERT 0 1
查看记录,插入了一条ID为6的记录,忽略了ID为3的记录。
t_girl=# select * from insert_ig@R_404_6462@e;
 id |    log_time     
----+-----------------
  1 | 14:44:12.37185
  2 | 14:48:22.222848
  3 | 15:05:33.198847
  6 | 15:15:52.297802
(4 rows)




第四种,用存储过程来代替INSERT处理。

t_girl=# create or replace function sp_insert_ig@R_404_6462@e (
IN f_id int,IN f_log_time time without time zone
) returns void as 
$ytt$
begin
  insert into insert_ig@R_404_6462@e values (f_id,f_log_time);
  exception   when unique_violation  then
    raise notice 'Duplicated Key Error on ID:%',f_id;
    return;
end;
$ytt$ language plpgsql;


第一次调用,抛出了错误。
t_girl=# select sp_insert_ig@R_404_6462@e(1,'14:22:35'::time); 
NOTICE:  Duplicated Key Error on ID:1
 sp_insert_ig@R_404_6462@e 
------------------
 
(1 row)


第二次正常插入。
t_girl=# select sp_insert_ig@R_404_6462@e(8,'14:22:35'::time);
 sp_insert_ig@R_404_6462@e 
------------------
 
(1 row)


t_girl=# select * from insert_ig@R_404_6462@e;
 id |    log_time     
----+-----------------
  1 | 14:44:12.37185
  2 | 14:48:22.222848
  3 | 15:05:33.198847
  6 | 15:15:52.297802
  8 | 14:22:35
(5 rows)


t_girl=# 

OK,目的也达到了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐