iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >MySQL中的insert ignore into讲解
  • 515
分享到

MySQL中的insert ignore into讲解

mysql 2023-10-04 19:10:48 515人浏览 泡泡鱼
摘要

最近工作中,使用到了insert ignore into语法,感觉这个语法还是挺有用的,就记录下来做个总结。 insert ignore into : 忽略重复的记录,直接插入数据。 包括两种场景: 插入的数据是主键冲突时 insert i

最近工作中,使用到了insert ignore into语法,感觉这个语法还是挺有用的,就记录下来做个总结

insert ignore into : 忽略重复的记录,直接插入数据。

包括两种场景:

插入的数据是主键冲突时

insert ignore into会给出warnings,show warnings就可以看到提示主键冲突;并且本次插入无效。

[test]> create table tt(c1 int primary key, c2 varchar(50))engine = xx;Query OK, 0 rows affected (0.21 sec)[test]> insert into tt values(1, "aaa"), (2, "bbb"), (3, "ccc");Query OK, 3 rows affected (0.02 sec)Records: 3  Duplicates: 0  Warnings: 0[test]> select * from tt;+----+------+| c1 | c2   |+----+------+|  1 | aaa  ||  2 | bbb  ||  3 | ccc  |+----+------+3 rows in set (0.01 sec)[test]> [test]> insert ignore into tt values(1, "aaa"), (2, "bbb"), (3, "ccc");Query OK, 0 rows affected, 3 warnings (0.01 sec)Records: 3  Duplicates: 3  Warnings: 3[test]> [test]> select * from tt;+----+------+| c1 | c2   |+----+------+|  1 | aaa  ||  2 | bbb  ||  3 | ccc  |+----+------+3 rows in set (0.00 sec)

使用insert ignore into语句时,如果主键冲突,只是提示"warnings"。

如果使用insert into语句时,如果主键冲突直接报错。

没有主键冲突时,直接插入数据

insert into 与 insert ignore into 都是直接插入数据

[test]> create table t2(c1 int, c2 varchar(50))engine = xxx;Query OK, 0 rows affected (0.05 sec)[test]> insert into t2 values(1, "aaa"), (2, "bbb"), (3, "ccc");Query OK, 3 rows affected (0.03 sec)Records: 3  Duplicates: 0  Warnings: 0GreatDB Cluster[test]> select * from t2;+------+------+| c1   | c2   |+------+------+|    1 | aaa  ||    2 | bbb  ||    3 | ccc  |+------+------+3 rows in set (0.00 sec)[test]> insert into t2 values(1, "aaa"), (2, "bbb"), (3, "ccc");Query OK, 3 rows affected (0.02 sec)Records: 3  Duplicates: 0  Warnings: 0[test]> insert into t2 values(1, "aaa"), (2, "bbb"), (3, "ccc");Query OK, 3 rows affected (0.02 sec)Records: 3  Duplicates: 0  Warnings: 0[test]> select * from t2;+------+------+| c1   | c2   |+------+------+|    1 | aaa  ||    2 | bbb  ||    3 | ccc  ||    1 | aaa  ||    2 | bbb  ||    3 | ccc  ||    1 | aaa  ||    2 | bbb  ||    3 | ccc  |+------+------+9 rows in set (0.00 sec)[test]> insert ignore into t2 values(1, "aaa"), (2, "bbb"), (3, "ccc");Query OK, 3 rows affected (0.03 sec)Records: 3  Duplicates: 0  Warnings: 0

因此,insert ignore into主要是忽略重复的记录,直接插入数据。

来源地址:https://blog.csdn.net/zgaoq/article/details/124432806

您可能感兴趣的文档:

--结束END--

本文标题: MySQL中的insert ignore into讲解

本文链接: https://www.lsjlt.com/news/423486.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作