MySQL避免插入重復(fù)記錄的方法
mysql在存在主鍵沖突或者唯一鍵沖突的情況下,根據(jù)插入策略不同,一般有以下三種避免方法。
1、insert ignore
2、replace into
3、insert on duplicate key update
注意,除非表有一個PRIMARY KEY或UNIQUE索引,否則,使用以上三個語句沒有意義,與使用單純的INSERT INTO相同。
一、insert ignore
insert ignore會忽略數(shù)據(jù)庫中已經(jīng)存在的數(shù)據(jù)(根據(jù)主鍵或者唯一索引判斷),如果數(shù)據(jù)庫沒有數(shù)據(jù),就插入新的數(shù)據(jù),如果有數(shù)據(jù)的話就跳過這條數(shù)據(jù).
Case:
表結(jié)構(gòu)如下:
root:test> show create table t3\G *************************** 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
如下,可以看到只插入了(6,'dd',5)這條,同時有一條warning提示有重復(fù)的值。
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)
重新查詢表結(jié)構(gòu),發(fā)現(xiàn)雖然只增加了一條記錄,但是AUTO_INCREMENT還是增加了2個(18變成20)
root:test> show create table t3\G *************************** 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=20 DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
二、replace into
- replace into 首先嘗試插入數(shù)據(jù)到表中。 如果發(fā)現(xiàn)表中已經(jīng)有此行數(shù)據(jù)(根據(jù)主鍵或者唯一索引判斷)則先刪除此行數(shù)據(jù),然后插入新的數(shù)據(jù),否則,直接插入新數(shù)據(jù)。
- 使用replace into,你必須具有delete和insert權(quán)限
Case:
root:test> show create table t3\G *************************** 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)
插入一條與記錄id=3存在唯一鍵(列c1)沖突的數(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)
可以看到原有id=3,c1=3的記錄不見了,新增了一條id=4,c1=3的記錄.
replace into語句執(zhí)行完會返回一個數(shù),來指示受影響的行的數(shù)目。該數(shù)是被刪除和被插入的行數(shù)的和,上面的例子中2 rows affected .
三、insert on duplicate key update
- 如果在insert into 語句末尾指定了on duplicate key update,并且插入行后會導(dǎo)致在一個UNIQUE索引或PRIMARY KEY中出現(xiàn)重復(fù)值,則在出現(xiàn)重復(fù)值的行執(zhí)行UPDATE;如果不會導(dǎo)致重復(fù)的問題,則插入新行,跟普通的insert into一樣。
- 使用insert into,你必須具有insert和update權(quán)限
- 如果有新記錄被插入,則受影響行的值顯示1;如果原有的記錄被更新,則受影響行的值顯示2;如果記錄被更新前后值是一樣的,則受影響行數(shù)的值顯示0
Case:
root:test> show create table t3\G *************************** 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,其他列沒有改變。
結(jié)論:
- 這三種方法都能避免主鍵或者唯一索引重復(fù)導(dǎo)致的插入失敗問題。
- insert ignore能忽略重復(fù)數(shù)據(jù),只插入不重復(fù)的數(shù)據(jù)。
- replace into和insert ... on duplicate key update,都是替換原有的重復(fù)數(shù)據(jù),區(qū)別在于replace into是刪除原有的行后,在插入新行,如有自增id,這個會造成自增id的改變;insert ... on duplicate key update在遇到重復(fù)行時,會直接更新原有的行,具體更新哪些字段怎么更新,取決于update后的語句。
到此這篇關(guān)于MySQL避免插入重復(fù)記錄的方法的文章就介紹到這了,更多相關(guān)MySQL避免插入重復(fù)記錄的方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL數(shù)據(jù)庫查詢進(jìn)階之多表查詢詳解
Mysql數(shù)據(jù)庫是web開發(fā)中最常用的數(shù)據(jù)庫之一,mysql多表查詢是開發(fā)人員必備的技能,下面這篇文章主要給大家介紹了關(guān)于MySQL數(shù)據(jù)庫查詢進(jìn)階之多表查詢的相關(guān)資料,需要的朋友可以參考下2022-04-04用于App服務(wù)端的MySQL連接池(支持高并發(fā))
這篇文章主要介紹了用于App服務(wù)端的MySQL連接池,并支持高并發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-12-12MySQL數(shù)據(jù)庫的高可用方案總結(jié)
這篇文章主要針對MySQL數(shù)據(jù)庫的高可用方案進(jìn)行詳細(xì)總結(jié),高可用架構(gòu)對于互聯(lián)網(wǎng)服務(wù)基本是標(biāo),本文是對各種方案的總結(jié),感興趣的小伙伴們可以參考一下2016-05-05mysql日志系統(tǒng)redo log和bin log介紹
這篇文章主要介紹了mysql日志系統(tǒng)redo log和bin log介紹,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08