暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

LightDB23.3新特性MySQL模式支持insert ignore特性

原创 姚崇 2023-08-08
150

我们知道MySQL支持insert ignore语法,场景为如果遇到唯一约束,insert 插入多值遇到冲突,不影响当前语句事务的运行

MySQL环境:

mysql> create table test(id int,name varchar(100));
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql>  alter table test add primary key (id);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> insert ignore into test values (1,'a'),(1,'bb'),(2,'cc');
Query OK, 2 rows affected, 1 warning (0.00 sec)
Records: 3  Duplicates: 1  Warnings: 1

mysql> select * from test;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | cc   |
+----+------+
2 rows in set (0.00 sec)

LightDB23.3 MySQL模式:

当LightDB的数据库为MySQL模式时

lightdb@mydb=# show %compatible_type%;
                  name                  | setting |                                   description                                
   
----------------------------------------+---------+---------------------------------------------------------------------------------
 lightdb_dblevel_syntax_compatible_type | MySql   | Show syntax compatible type for current database.
 lightdb_syntax_compatible_type         | Oracle  | Default syntax compatible type when create database.  Support MySQL and Oracle.

insert ignore语句

dev@mydb=# \c mydb;
You are now connected to database "mydb" as user "dev".
compatible type: mysql
dev@mydb=# create table test(id int,name varchar(100));
CREATE TABLE
dev@mydb=# alter table test add primary key (id);
ALTER TABLE
dev@mydb=# insert ignore into test values (1,'a'),(1,'bb'),(2,'cc');
WARNING:  duplicate key value violates unique constraint "test_pkey"
DETAIL:  Key (id)=(1) already exists.
INSERT 0 2
dev@mydb=# select * from test;
 id | name
----+------
  1 | a
  2 | cc
(2 rows)
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论