MySql三種避免重復(fù)插入數(shù)據(jù)的方法
前言
MySql 在存在主鍵沖突或唯一鍵沖突的情況下,根據(jù)插入方式,一般有以下三種插入方式避免錯(cuò)誤。
- insert ignore。
- replace into
- insert on duplicate key update
insert ignore
insert ignore 會(huì)忽視數(shù)據(jù)庫(kù)中已經(jīng)存在的數(shù)據(jù),根據(jù)主鍵或者唯一索引判斷,如果數(shù)據(jù)庫(kù)沒(méi)有數(shù)據(jù),就會(huì)插入新的數(shù)據(jù),如果有數(shù)據(jù)的話就跳過(guò)這條數(shù)據(jù)
小case
表結(jié)構(gòu)
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)
插入沖突數(shù)據(jù)
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
查看結(jié)果
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 會(huì)嘗試先插入數(shù)據(jù),如果發(fā)現(xiàn)沖突進(jìn)行刪除。否則不做任何操作。
小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)
插入沖突數(shù)據(jù)
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)
可以看到原有的記錄已經(jīng)沒(méi)有了,新的記錄又有了。
insert on duplicate key update
如果在insert into 語(yǔ)句末尾指定了 insert on duplicate key update 如果出現(xiàn)了重復(fù)值,則會(huì)在出現(xiàn)重復(fù)值以后進(jìn)行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)沖突的數(shù)據(jù)
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的記錄發(fā)生了改變,c1=原有的c1+3,其他列沒(méi)有改變。
以上就是MySql四種避免重復(fù)插入數(shù)據(jù)的方法的詳細(xì)內(nèi)容,更多關(guān)于MySQL 避免插入重復(fù)數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Windows XP系統(tǒng)安裝MySQL5.5.28圖解教程
很多朋友在winxp系統(tǒng)中開發(fā)php等,需要安裝mysql數(shù)據(jù)庫(kù),這里簡(jiǎn)單介紹下,如何在xp下安裝mysql軟件,其實(shí)跟其它系統(tǒng)都差不多,主要是軟件對(duì)系統(tǒng)的兼容性2013-05-05分享很少見(jiàn)很有用的SQL功能CORRESPONDING
這篇文章主要介紹了分享很少見(jiàn)很有用的SQL功能CORRESPONDING,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08MySQL初級(jí)入門篇之視圖的相關(guān)概念及應(yīng)用實(shí)例
Mysql中的視圖其實(shí)是一個(gè)虛擬表,使用時(shí)動(dòng)態(tài)檢索查詢數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于MySQL初級(jí)入門篇之視圖的相關(guān)概念及應(yīng)用實(shí)例的相關(guān)資料,需要的朋友可以參考下2022-04-04mysql ERROR 1045 (28000)問(wèn)題的解決方法
這篇文章主要介紹了mysql ERROR 1045 (28000)問(wèn)題的解決方法,文中步驟介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10sql中select into和insert select的用法小結(jié)
在工作中,我們經(jīng)常需要備份表,本文主要介紹了sql中select into和insert select的用法小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08mysql 5.7.9 winx64在windows上安裝遇到的問(wèn)題
mysql5.7.9版本以上在windwos上安裝時(shí)會(huì)遇到無(wú)法啟動(dòng)但是沒(méi)有任何報(bào)錯(cuò)的問(wèn)題,怎么回事呢?接下來(lái)通過(guò)本文給大家介紹mysql 5.7.9 winx64在windows上安裝遇到的問(wèn)題及解決方法,需要的朋友可以參考下2016-10-10關(guān)于MySQL性能調(diào)優(yōu)你必須了解的15個(gè)重要變量(小結(jié))
MYSQL 應(yīng)該是比較流行的 WEB 后端數(shù)據(jù)庫(kù)。雖然 NOSQL 最近越來(lái)越多的被提到,但是相信大部分架構(gòu)師還是會(huì)選擇 MYSQL 來(lái)做數(shù)據(jù)存儲(chǔ)。本文作者總結(jié)梳理MySQL性能調(diào)優(yōu)的15個(gè)重要變量,感興趣的可以了解一下2019-07-07