• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

MySQL的insert ignore into

武飞扬头像
一缕阳光a
帮助1

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

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

包括两种场景:

1、插入的数据是主键冲突时

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

  1.  
    [test]> create table tt(c1 int primary key, c2 varchar(50))engine = xx;
  2.  
    Query OK, 0 rows affected (0.21 sec)
  3.  
     
  4.  
    [test]> insert into tt values(1, "aaa"), (2, "bbb"), (3, "ccc");
  5.  
    Query OK, 3 rows affected (0.02 sec)
  6.  
    Records: 3 Duplicates: 0 Warnings: 0
  7.  
     
  8.  
    [test]> select * from tt;
  9.  
    ---- ------
  10.  
    | c1 | c2 |
  11.  
    ---- ------
  12.  
    | 1 | aaa |
  13.  
    | 2 | bbb |
  14.  
    | 3 | ccc |
  15.  
    ---- ------
  16.  
    3 rows in set (0.01 sec)
  17.  
     
  18.  
    [test]>
  19.  
    [test]> insert ignore into tt values(1, "aaa"), (2, "bbb"), (3, "ccc");
  20.  
    Query OK, 0 rows affected, 3 warnings (0.01 sec)
  21.  
    Records: 3 Duplicates: 3 Warnings: 3
  22.  
     
  23.  
    [test]>
  24.  
    [test]> select * from tt;
  25.  
    ---- ------
  26.  
    | c1 | c2 |
  27.  
    ---- ------
  28.  
    | 1 | aaa |
  29.  
    | 2 | bbb |
  30.  
    | 3 | ccc |
  31.  
    ---- ------
  32.  
    3 rows in set (0.00 sec)
学新通

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

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

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

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

  1.  
    [test]> create table t2(c1 int, c2 varchar(50))engine = xxx;
  2.  
    Query OK, 0 rows affected (0.05 sec)
  3.  
     
  4.  
    [test]> insert into t2 values(1, "aaa"), (2, "bbb"), (3, "ccc");
  5.  
    Query OK, 3 rows affected (0.03 sec)
  6.  
    Records: 3 Duplicates: 0 Warnings: 0
  7.  
     
  8.  
    GreatDB Cluster[test]> select * from t2;
  9.  
    ------ ------
  10.  
    | c1 | c2 |
  11.  
    ------ ------
  12.  
    | 1 | aaa |
  13.  
    | 2 | bbb |
  14.  
    | 3 | ccc |
  15.  
    ------ ------
  16.  
    3 rows in set (0.00 sec)
  17.  
     
  18.  
    [test]> insert into t2 values(1, "aaa"), (2, "bbb"), (3, "ccc");
  19.  
    Query OK, 3 rows affected (0.02 sec)
  20.  
    Records: 3 Duplicates: 0 Warnings: 0
  21.  
     
  22.  
     
  23.  
    [test]> insert into t2 values(1, "aaa"), (2, "bbb"), (3, "ccc");
  24.  
    Query OK, 3 rows affected (0.02 sec)
  25.  
    Records: 3 Duplicates: 0 Warnings: 0
  26.  
     
  27.  
    [test]> select * from t2;
  28.  
    ------ ------
  29.  
    | c1 | c2 |
  30.  
    ------ ------
  31.  
    | 1 | aaa |
  32.  
    | 2 | bbb |
  33.  
    | 3 | ccc |
  34.  
    | 1 | aaa |
  35.  
    | 2 | bbb |
  36.  
    | 3 | ccc |
  37.  
    | 1 | aaa |
  38.  
    | 2 | bbb |
  39.  
    | 3 | ccc |
  40.  
    ------ ------
  41.  
    9 rows in set (0.00 sec)
  42.  
     
  43.  
    [test]> insert ignore into t2 values(1, "aaa"), (2, "bbb"), (3, "ccc");
  44.  
    Query OK, 3 rows affected (0.03 sec)
  45.  
    Records: 3 Duplicates: 0 Warnings: 0
  46.  
     
学新通

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggfjij
系列文章
更多 icon
同类精品
更多 icon
继续加载