[LightDB兼容增强]支持MySQL INSERT IGNORE

支持的版本:自LightDB 23.3。

 

背景:

  默认情况下,如果使用 Insert 插入数据遇到唯一性约束错误,整个事务将因出错而被撤销,例子如下:  

mysql> create table t(a int primary key, b int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t(a,b) values(1,1),(1,1);
ERROR 1062 (23000): Duplicate entry '1' for key 't.PRIMARY'

  MySQL INSERT IGNORE功能的引入,使得插入数据遇到唯一性约束出错时,唯一性约束错误信息会被降级为Warning, 进而继续事务的执行。例子如下:

mysql> create table t(a int primary key, b int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert ignore into t(a,b) values(1,1),(1,1);
Query OK, 1 row affected, 1 warning (0.00 sec)
Records: 2  Duplicates: 1  Warnings: 1

mysql> select * from t;
+---+------+
| a | b    |
+---+------+
| 1 |    1 |
+---+------+
1 row in set (0.00 sec)

  

LightDB自23.3起,在MySQL兼容模式下引入了INSERT IGNORE功能。

示例:

dev@postgres=# create database mydb with lightdb_syntax_compatible_type 'mysql';
CREATE DATABASE
dev@postgres=# \c mydb;
You are now connected to database "mydb" as user "dev".
compatible type: mysql
dev@mydb=# create table t(a int primary key, b int);
CREATE TABLE
dev@mydb=# insert ignore into t(a,b) values(1,1),(1,1);
WARNING:  duplicate key value violates unique constraint "t_pkey"
DETAIL:  Key (a)=(1) already exists.
INSERT 0 1
dev@mydb=# table t;
 a | b
---+---
 1 | 1
(1 row)

 

注意:

  1)示例中使用的是primary key, 实际上unique index也适用。

  2)当前不支持分区表以及外键约束错误降级至警告。

  

 

posted on 2023-08-08 14:04  aodb  阅读(21)  评论(0编辑  收藏  举报