MySQL中的insert ignore into使用
MySQL中的insert ignore into
最近工作中,使用到了insert ignore into語法,感覺這個語法還是挺有用的,就記錄下來做個總結(jié)。
insert ignore into
: 忽略重復(fù)的記錄,直接插入數(shù)據(jù)。
包括兩種場景:
1、插入的數(shù)據(jù)是主鍵沖突時
insert ignore into會給出warnings,show warnings就可以看到提示主鍵沖突;
[test]> create table tt(c1 int primary key, c2 varchar(50))engine = xx; Query OK, 0 rows affected (0.21 sec) ? [test]> insert into tt values(1, "aaa"), (2, "bbb"), (3, "ccc"); Query OK, 3 rows affected (0.02 sec) Records: 3 ?Duplicates: 0 ?Warnings: 0 ? [test]> select * from tt; +----+------+ | c1 | c2 ? | +----+------+ | ?1 | aaa ?| | ?2 | bbb ?| | ?3 | ccc ?| +----+------+ 3 rows in set (0.01 sec) ? [test]>? [test]> insert ignore into tt values(1, "aaa"), (2, "bbb"), (3, "ccc"); Query OK, 0 rows affected, 3 warnings (0.01 sec) Records: 3 ?Duplicates: 3 ?Warnings: 3 ? [test]>? [test]> select * from tt; +----+------+ | c1 | c2 ? | +----+------+ | ?1 | aaa ?| | ?2 | bbb ?| | ?3 | ccc ?| +----+------+ 3 rows in set (0.00 sec)
使用insert ignore into語句時,如果主鍵沖突,只是提示"warnings"。
如果使用insert into語句時,如果主鍵沖突直接報錯。
2、沒有主鍵沖突時,直接插入數(shù)據(jù)
insert into 與 insert ignore into 都是直接插入數(shù)據(jù)
[test]> create table t2(c1 int, c2 varchar(50))engine = xxx; Query OK, 0 rows affected (0.05 sec) ? [test]> insert into t2 values(1, "aaa"), (2, "bbb"), (3, "ccc"); Query OK, 3 rows affected (0.03 sec) Records: 3 ?Duplicates: 0 ?Warnings: 0 ? GreatDB Cluster[test]> select * from t2; +------+------+ | c1 ? | c2 ? | +------+------+ | ? ?1 | aaa ?| | ? ?2 | bbb ?| | ? ?3 | ccc ?| +------+------+ 3 rows in set (0.00 sec) ? [test]> insert into t2 values(1, "aaa"), (2, "bbb"), (3, "ccc"); Query OK, 3 rows affected (0.02 sec) Records: 3 ?Duplicates: 0 ?Warnings: 0 ? ? [test]> insert into t2 values(1, "aaa"), (2, "bbb"), (3, "ccc"); Query OK, 3 rows affected (0.02 sec) Records: 3 ?Duplicates: 0 ?Warnings: 0 ? [test]> select * from t2; +------+------+ | c1 ? | c2 ? | +------+------+ | ? ?1 | aaa ?| | ? ?2 | bbb ?| | ? ?3 | ccc ?| | ? ?1 | aaa ?| | ? ?2 | bbb ?| | ? ?3 | ccc ?| | ? ?1 | aaa ?| | ? ?2 | bbb ?| | ? ?3 | ccc ?| +------+------+ 9 rows in set (0.00 sec) ? [test]> insert ignore into t2 values(1, "aaa"), (2, "bbb"), (3, "ccc"); Query OK, 3 rows affected (0.03 sec) Records: 3 ?Duplicates: 0 ?Warnings: 0
因此,insert ignore into主要是忽略重復(fù)的記錄,直接插入數(shù)據(jù)。
insert ignore into--跳坑
首先,SQL語句:
<insert id="addTerm" parameterType="String"> insert ignore into term(term) VALUES (#{term}) </insert>
然后,數(shù)據(jù)庫表:
簡單的不能再簡單的一張表:解釋一下,id是自增的,同時也是主鍵,term就是term嘍,其他的外鍵、索引什么的都沒有。
要求是:不能使term重復(fù)插入;
剛開始認(rèn)為就是判斷的插入的數(shù)據(jù)是否重復(fù),然后發(fā)現(xiàn)不是這樣的,它判斷的是主鍵或者索引是否重復(fù)(更準(zhǔn)確的來說是忽略主鍵沖突時報的錯誤)。
所以在這里要說說這個自增的id了,因為在insert ignore into插入的時候你的現(xiàn)在將要插入的數(shù)據(jù)的id已經(jīng)是增加一了,所以你的這個自增主鍵id是無論如何也不能相等的,所以自然你的數(shù)據(jù)也就不會聽話的把重復(fù)的數(shù)據(jù)不插入,這也就是為什么,即使ignore時沒有插入數(shù)據(jù)它的自增的鍵也會跳過,所以這個要注意。
解決辦法就是添加個索引就歐克了唄
因為重復(fù)的時候它的自增的鍵依然會增加,所以當(dāng)數(shù)據(jù)的重復(fù)率很高的時候,可以考慮把自增的鍵搞成自己控制的一個因素,手動自增,豈不美滋滋?
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
MySQL中使用SHOW PROFILE命令分析性能的用法整理
這篇文章主要介紹了MySQL中使用show profile命令分析性能的用法整理,show profiles是數(shù)據(jù)庫性能優(yōu)化的常用命令,需要的朋友可以參考下2015-11-11Mysql 切換數(shù)據(jù)存儲目錄的實現(xiàn)方法
這篇文章主要介紹了Mysql 切換數(shù)據(jù)存儲目錄的實現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2017-07-07MySQL數(shù)據(jù)庫如何開啟遠(yuǎn)程連接(多備份)
多備份服務(wù)器在備份你的數(shù)據(jù)庫時,必須能夠遠(yuǎn)程連接上你的數(shù)據(jù)庫。但是一般來說mysql安裝時都是關(guān)閉遠(yuǎn)程連接的,因此,需要你開通mysql數(shù)據(jù)庫的遠(yuǎn)程訪問權(quán)限。那么如何開啟呢2015-01-01MySQL數(shù)據(jù)庫定時備份的實現(xiàn)方法
這篇文章主要介紹了MySQL數(shù)據(jù)庫的定時備份的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04