0

0

PostgreSQL实现MySQLinsertignore语法

php中文网

php中文网

发布时间:2016-06-07 16:18:31

|

2093人浏览过

|

来源于php中文网

原创

对MySQL熟悉的人可能都知道,MySQL 有一个insert ignore 语法来忽略已经存在的记录。 PostgreSQL暂时不提供这样的语法,但是可以用其他方法来代替。 t_girl=# d insert_ignore Table ytt.insert_ignore Column | Type | Modifiers ----------+--------------

          对mysql熟悉的人可能都知道,mysql 有一个“insert ignore" 语法来忽略已经存在的记录。 postgresql暂时不提供这样的语法,但是可以用其他方法来代替。

t_girl=# d insert_ignore Table "ytt.insert_ignore" Column | Type | Modifiers ----------+------------------------+----------- id | integer | not null log_time | time without time zone | Indexes: "insert_ignore_pkey" PRIMARY KEY, btree (id) t_girl=# select * from insert_ignore; id | log_time ----+---------------- 1 | 14:44:12.37185 (1 row) 我来演示下几种代替方法。 第一种方法, 用自带的规则(RULE)来实现。

t_girl=# create rule r_insert_ignore as on insert to insert_ignore where exists (select 1 from insert_ignore where id = new.id) do instead nothing;   
CREATE RULE

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


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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

t_girl=# create or replace function sp_insert_ignore() returns trigger as

 $ytt$

 begin

   perform  1 from insert_ignore where id = new.id;

   if found then

     return null;

  end if;

  return new;

end;

$ytt$ language 'plpgsql';

CREATE FUNCTION

 

Simple Groupware0.745
Simple Groupware0.745

Simple Groupware 是一个完整的协同工作套件包。它采用PHP,XML,SQL,HTML,CSS和sgsML开发。Simple Groupware与其它同类型系统不同之处在于使用了新的编程语言sgsML。该语言能够实现快速开发Web应用系统。支持MySQL,Oracle和PostgreSQL。

下载

 

对应的触发器如下:

t_girl=# create trigger tr_ib_insert_ignore before insert on insert_ignore for each row execute procedure sp_insert_ignore();

CREATE TRIGGER

 

 

继续插入两条记录。

t_girl=# insert into insert_ignore values (3,current_time),(2,current_time);

INSERT 0 1

t_girl=# select * from insert_ignore;

 id |    log_time    

----+-----------------

  1 | 14:44:12.37185

  2 | 14:48:22.222848

  3 | 15:05:33.198847

(3 rows)

 

 

OK。目的达到了。 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

t_girl=# insert into insert_ignore

        with ytt_test(f1,f2) as (

                            values(6,current_time),(3,current_time)

                            )

                            select a.* from ytt_test as a where a.f1 not in (select id from insert_ignore as b);                         

INSERT 0 1

查看记录,,插入了一条ID为6的记录,忽略了ID为3的记录。

t_girl=# select * from insert_ignore;

 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_ignore ( IN f_id int, IN f_log_time time without time zone ) returns void as $ytt$ begin insert into insert_ignore 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_ignore(1,'14:22:35'::time); NOTICE: Duplicated Key Error on ID:1 sp_insert_ignore ------------------ (1 row) 第二次正常插入。 t_girl=# select sp_insert_ignore(8,'14:22:35'::time); sp_insert_ignore ------------------ (1 row) t_girl=# select * from insert_ignore; 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,目的也达到了。

相关专题

更多
php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

14

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

7

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

6

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

20

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

html编辑相关教程合集
html编辑相关教程合集

本专题整合了html编辑相关教程合集,阅读专题下面的文章了解更多详细内容。

106

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 7.6万人学习

PostgreSQL 手册
PostgreSQL 手册

共0课时 | 0人学习

Node.js 教程
Node.js 教程

共57课时 | 9.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号