实践 | 在MySql中,这四种方法可以避免重复插入数据!

前言

MySql 在存在主键冲突或唯一键冲突的情况下,根据插入方式,一般有以下四种插入方式避免错误。

  1. insert ignore。

  2. replace into

  3. insert on duplicate key update

insert ignore

insert ignore 会忽视数据库中已经存在的数据,根据主键或者唯一索引判断,如果数据库没有数据,就会插入新的数据,如果有数据的话就跳过这条数据

小case

表结构

root:test> show create table t3G
*************************** 1. row ***************************
       Table: t3
Create Table: CREATE TABLE `t3` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `c1` int(11) DEFAULT NULL,
  `c2` varchar(20) DEFAULT NULL,
  `c3` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uidx_c1` (`c1`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

root:test> select * from t3;
    +----+------+------+------+
    | id | c1   | c2   | c3   |
    +----+------+------+------+
    |  1 |    1 | a    |    1 |
    |  2 |    2 | a    |    1 |
    |  8 | NULL | NULL |    1 |
    | 14 |    4 | bb   | NULL |
    | 17 |    5 | cc   |    4 |
    +----+------+------+------+
    5 rows in set (0.00 sec)

插入冲突数据

root:test> insert ignore into t3 (c1,c2,c3) values(5,'cc',4),(6,'dd',5);     Query OK, 1 row affected, 1 warning (0.01 sec)
Records: 2  Duplicates: 1  Warnings: 1

查看结果

root:test> show warnings;
+---------+------+---------------------------------------+
| Level   | Code | Message                               |
+---------+------+---------------------------------------+
| Warning | 1062 | Duplicate entry '5' for key 'uidx_c1' |
+---------+------+---------------------------------------+
1 row in set (0.00 sec)

root:test> select * from t3;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 | a    |    1 |
|  2 |    2 | a    |    1 |
|  8 | NULL | NULL |    1 |
| 14 |    4 | bb   | NULL |
| 17 |    5 | cc   |    4 |
| 18 |    6 | dd   |    5 |
+----+------+------+------+
6 rows in set (0.00 sec)

replace into

replace into 会尝试先插入数据,如果发现冲突进行删除。否则不做任何操作。

小case

root:test> show create table t3G
*************************** 1. row ***************************
       Table: t3
Create Table: CREATE TABLE `t3` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `c1` int(11) DEFAULT NULL,
  `c2` varchar(20) DEFAULT NULL,
  `c3` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uidx_c1` (`c1`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

root:test> select * from t3;
+----+------+--------+------+
| id | c1   | c2     | c3   |
+----+------+--------+------+
|  1 |    1 | cc     |    4 |
|  2 |    2 | dd     |    5 |
|  3 |    3 | qwewqe |    3 |
+----+------+--------+------+
3 rows in set (0.00 sec)

插入冲突数据

root:test> replace into t3 (c1,c2,c3) values(3,'new',8);
Query OK, 2 rows affected (0.02 sec)

root:test> select * from t3;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 | cc   |    4 |
|  2 |    2 | dd   |    5 |
|  4 |    3 | new  |    8 |
+----+------+------+------+
3 rows in set (0.00 sec)

可以看到原有的记录已经没有了,新的记录又有了。

insert on duplicate key update

如果在insert into 语句末尾指定了 insert on duplicate key update 如果出现了重复值,则会在出现重复值以后进行update。

case

root:test> show create table t3G
*************************** 1. row ***************************
       Table: t3
Create Table: CREATE TABLE `t3` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `c1` int(11) DEFAULT NULL,
  `c2` varchar(20) DEFAULT NULL,
  `c3` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uidx_c1` (`c1`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

root:test> select * from t3;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 | fds  |    4 |
|  2 |    2 | ytu  |    3 |
|  3 |    3 | czx  |    5 |
+----+------+------+------+
3 rows in set (0.00 sec)

插入一条与记录id=3存在唯一键(列c1)冲突的数据

root:test> insert into t3(c1,c2,c3) values (3,'new',5) on duplicate key update c1=c1+3;
Query OK, 2 rows affected (0.01 sec)

root:test> select * from t3;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 | fds  |    4 |
|  2 |    2 | ytu  |    3 |
|  3 |    6 | czx  |    5 |
+----+------+------+------+
3 rows in set (0.00 sec)

可以看到,id=3的记录发生了改变,c1=原有的c1+3,其他列没有改变。

小明菜市场

推荐阅读

● 理论 | 分库分表需要考虑的问题及方案

● 理论 | 六种延迟队列的实现方案

● 实战 | Element UI 父子组件传值与事件绑定(逆向)

● 实战 | Element UI 父子组件传值与事件绑定(正向)

● 实战 | Vue + Element UI 表格组件二次封装

    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值