最近工作中,使用到了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主要是忽略重复的记录,直接插入数据。
--结束END--
本文标题: MySQL中的insert ignore into讲解
本文链接: https://www.lsjlt.com/news/423486.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-22
2024-05-22
2024-05-22
2024-05-22
2024-05-22
2024-05-22
2024-05-22
2024-05-21
2024-05-21
2024-05-21
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
一口价域名售卖能注册吗?域名是网站的标识,简短且易于记忆,为在线用户提供了访问我们网站的简单路径。一口价是在域名交易中一种常见的模式,而这种通常是针对已经被注册的域名转售给其他人的一种方式。
一口价域名买卖的过程通常包括以下几个步骤:
1.寻找:买家需要在域名售卖平台上找到心仪的一口价域名。平台通常会为每个可售的域名提供详细的描述,包括价格、年龄、流
443px" 443px) https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294.jpg https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294-768x413.jpg 域名售卖 域名一口价售卖 游戏音频 赋值/切片 框架优势 评估指南 项目规模
0