在MySql中,这四种方法可以避免重复插入数据!
生活随笔
收集整理的這篇文章主要介紹了
在MySql中,这四种方法可以避免重复插入数据!
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
MySql 在存在主鍵沖突或唯一鍵沖突的情況下,根據插入方式,一般有以下四種插入方式避免錯誤。
insert ignore。
replace into
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,其他列沒有改變。
有道無術,術可成;有術無道,止于術
歡迎大家關注Java之道公眾號
好文章,我在看??
總結
以上是生活随笔為你收集整理的在MySql中,这四种方法可以避免重复插入数据!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: idea删除module
- 下一篇: 002 python准备做题的一些准备