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,目的也达到了。

相关专题

更多
Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

37

2026.01.14

php与html混编教程大全
php与html混编教程大全

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

19

2026.01.13

PHP 高性能
PHP 高性能

本专题整合了PHP高性能相关教程大全,阅读专题下面的文章了解更多详细内容。

37

2026.01.13

MySQL数据库报错常见问题及解决方法大全
MySQL数据库报错常见问题及解决方法大全

本专题整合了MySQL数据库报错常见问题及解决方法,阅读专题下面的文章了解更多详细内容。

19

2026.01.13

PHP 文件上传
PHP 文件上传

本专题整合了PHP实现文件上传相关教程,阅读专题下面的文章了解更多详细内容。

16

2026.01.13

PHP缓存策略教程大全
PHP缓存策略教程大全

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

6

2026.01.13

jQuery 正则表达式相关教程
jQuery 正则表达式相关教程

本专题整合了jQuery正则表达式相关教程大全,阅读专题下面的文章了解更多详细内容。

3

2026.01.13

交互式图表和动态图表教程汇总
交互式图表和动态图表教程汇总

本专题整合了交互式图表和动态图表的相关内容,阅读专题下面的文章了解更多详细内容。

45

2026.01.13

nginx配置文件详细教程
nginx配置文件详细教程

本专题整合了nginx配置文件相关教程详细汇总,阅读专题下面的文章了解更多详细内容。

9

2026.01.13

热门下载

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

精品课程

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

共48课时 | 7.1万人学习

PostgreSQL 手册
PostgreSQL 手册

共0课时 | 0人学习

Node.js 教程
Node.js 教程

共57课时 | 8.6万人学习

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

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