PostgreSQL实现MySQL"insertignore"语法

          对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
 
 
对应的触发器如下:
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

<strong>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)</strong>
 
<strong>第四种,用存储过程来代替INSERT处理。</strong>
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,目的也达到了。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索insert
, select
, ignore
, from
, Insert()t方法
, 14
, mysql触发器insert
$(&quot;#id&quot;)
postgresql mysql、mysql和postgresql、postgresql vs mysql、mysql转postgresql、mysql与postgresql,以便于您获取更多的相关知识。

时间: 2024-08-01 10:36:16

PostgreSQL实现MySQL&quot;insertignore&quot;语法的相关文章

信息- VBScript 运行时错误 类型不匹配: &amp;amp;#39;[string: &amp;amp;quot;&amp;amp;quot;]&amp;amp;#39; line 12

问题描述 VBScript 运行时错误 类型不匹配: '[string: ""]' line 12 <%Option Explicit%> <%dim cartstrcartstr = getCartFromCookie()if (cartstr=NULL or cartstr ="""") then response.redirect ""Cart.asp"" response.ende

请问mysql支持with...as...语法么

问题描述 请问mysql支持with...as...语法么 解决方案 不支持你也可以参考下面http://stackoverflow.com/questions/1382573/how-do-you-use-the-with-clause-in-mysqlhttp://stackoverflow.com/questions/324935/mysql-with-clause

PostgreSQL和MySQL的存储层深度解析

PostgreSQL和MySQL的存储层深度解析 李海翔  DTCC2014 PostgreSQL是以加州大学伯克利分校计算机系开发的 POSTGRES,现在已经更名为PostgreSQL,版本 4.2为基础的对象关系型数据库管理系统(ORDBMS).PostgreSQL支持大部分 SQL标准并且提供了许多其他现代特性:复杂查询.外键.触发器.视图.事务完整性.MVCC.同样,PostgreSQL 可以用许多方法扩展,比如, 通过增加新的数据类型.函数.操作符.聚集函数.索引免费使用.修改.和分

Mysql Insert Or Update语法实例

有的时候会需要写一段insert的sql,如果主键存在,则update:如果主键不存在,则insert.Mysql中提供了这样的用法:ON DUPLICATE KEY UPDATE.下面就看看它是如何使用的吧! 首先数据库的原始数据如下: a b c 1 b1 c1 2 b2 c2 3 b3 c3 此时如果执行下面的sql就会报错 INSERT INTO test VALUES(1,'b4','c4'); 报错信息如下,提示无法重复插入: 1 queries executed, 0 succes

error-mysql InnoDB: Error: &amp;amp;quot;mysql&amp;amp;quot;.&amp;amp;quot;innodb_table_stats

问题描述 mysql InnoDB: Error: "mysql"."innodb_table_stats 附上错误日志: 2015-06-23 22:39:06 1252 [Note] Giving 0 client threads a chance to die gracefully 2015-06-23 22:39:06 1252 [Note] Shutting down slave threads 2015-06-23 22:39:06 1252 [Note] For

mysql &amp;quot;group by&amp;quot;与&amp;quot;order by&amp;quot;的研究--分类中最新的内容_数据库其它

这两天让一个数据查询难了.主要是对group by 理解的不够深入.才出现这样的情况 这种需求,我想很多人都遇到过.下面是我模拟我的内容表 复制代码 代码如下: CREATE TABLE `test` ( `id` INT(10) NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) NOT NULL, `category_id` INT(10) NOT NULL, `date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMES

MySQL 存储过程和&amp;quot;Cursor&amp;quot;的使用方法_Mysql

示例如下: 复制代码 代码如下: CREATE PROCEDURE `justifyGroupNum`() NOT DETERMINISTIC SQL SECURITY DEFINER COMMENT '' BEGIN /*how to run:call justifyGroupNum()*/ DECLARE p_group_id int; declare p_num int; declare stopFlag int; DECLARE cursor_name CURSOR FOR select

PostgreSQL 与 MySQL 相比,优势何在?

一. PostgreSQL 的稳定性极强, Innodb 等引擎在崩溃.断电之类的灾难场景下抗打击能力有了长足进步,然而很多 MySQL 用户都遇到过Server级的数据库丢失的场景--mysql系统库是MyISAM的,相比之下,PG数据库这方面要好一些. 二.任何系统都有它的性能极限,在高并发读写,负载逼近极限下,PG的性能指标仍可以维持双曲线甚至对数曲线,到顶峰之后不再下降,而 MySQL 明显出现一个波峰后下滑(5.5版本之后,在企业级版本中有个插件可以改善很多,不过需要付费). 三.PG

PostgreSQL UDF实现IF NOT EXISTS语法

标签 PostgreSQL , Greenplum , DDL , IF NOT EXISTS 背景 当对象存在时,不创建:当对象不存在时,创建. 在数据库中使用IF NOT EXISTS语法进行判断. Syntax: CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [ 有一些较老的版本,可能不支持IF NOT EXISTS语法,那么可以使用UDF