MYSQL插入處理重複鍵值的幾種方法

2021-08-25 23:16:15 字數 3074 閱讀 9990

當unique列在乙個unique鍵上插入包含重複值的記錄時,預設insert的時候會報1062錯誤,mysql有三種不同的處理方法,下面我們分別介紹。

先建立2個測試表,在id列上建立unique約束。

mysql>createtabletest1(idint,namevarchar(5),typeint,primarykey(id));

queryok,0rowsaffected(0.01sec)

mysql>createtabletest2(idint,namevarchar(5),typeint,primarykey(id));

queryok,0rowsaffected(0.01sec)

mysql>select*fromtest1;

+-----+------+------+

|id|name|type|

+-----+------+------+

|101|aaa|1|

|102|bbb|2|

|103|ccc|3|

+-----+------+------+

3rowsinset(0.00sec)

mysql>select*fromtest2;

+-----+------+------+

|id|name|type|

+-----+------+------+

|201|aaa|1|

|202|bbb|2|

|203|ccc|3|

|101|***|5|

+-----+------+------+

4rowsinset(0.00sec)

1、replaceinto

發現重複的先刪除再插入,如果記錄有多個字段,在插入的時候如果有的字段沒有賦值,那麼新插入的記錄這些欄位為空。

mysql>replaceintotest1(id,name)(selectid,namefromtest2);

queryok,5rowsaffected(0.04sec)

records:4duplicates:1warnings:0

mysql>select*fromtest1;

+-----+------+------+

|id|name|type|

+-----+------+------+

|101|***|null|

|102|bbb|2|

|103|ccc|3|

|201|aaa|null|

|202|bbb|null|

|203|ccc|null|

+-----+------+------+

6rowsinset(0.00sec)

需要注意的是,當你replace的時候,如果被插入的表如果沒有指定列,會用null表示,而不是這個表原來的內容。如果插入的內容列和被插入的表列一樣,則不會出現null。例如

mysql>replaceintotest1(id,name,type)(selectid,name,typefromtest2);

queryok,8rowsaffected(0.04sec)

records:4duplicates:4warnings:0

mysql>select*fromtest1;

+-----+------+------+

|id|name|type|

+-----+------+------+

|101|***|5|

|102|bbb|2|

|103|ccc|3|

|201|aaa|1|

|202|bbb|2|

|203|ccc|3|

+-----+------+------+

6rowsinset(0.00sec)

如果insert的時候,需要保留被插入表的列,只更新指定列,那麼就可以使用第二種方法。

2、insertintoonduplicatekeyupdate

發現重複的是更新操作。在原有記錄基礎上,更新指定字段內容,其它字段內容保留。例如我只想插入test2表的id,name欄位,但是要保留test1表的type欄位:

mysql>insertintotest1(id,name,type)(selectid,name,typefromtest2)onduplicatekeyupdatetest1.name=test2.name;

queryok,5rowsaffected(0.04sec)

records:4duplicates:1warnings:0

mysql>select*fromtest1;

+-----+------+------+

|id|name|type|

+-----+------+------+

|101|***|1|

|102|bbb|2|

|103|ccc|3|

|203|ccc|3|

|202|bbb|2|

|201|aaa|1|

+-----+------+------+

6rowsinset(0.00sec)

如果insert的時候,只想插入原表沒有的資料,那麼可以使用第三種方法。

3、ignoreinto

判斷是否存在,存在不插入,否則插入。很容易理解,當插入的時候,違反唯一性約束,mysql不會嘗試去執行這條語句。例如:

mysql>insertignoreintotest1(id,name,type)(selectid,name,typefromtest2);

queryok,3rowsaffected(0.01sec)

records:4duplicates:1warnings:0

mysql>select*fromtest1;

+-----+------+------+

|id|name|type|

+-----+------+------+

|101|aaa|1|

|102|bbb|2|

|103|ccc|3|

|203|ccc|3|

|202|bbb|2|

|201|aaa|1|

+-----+------+------+

6rowsinset(0.00sec)

MYSQL插入處理重複鍵值的幾種方法

當unique列在乙個unique鍵上插入包含重複值的記錄時,預設insert的時候會報1062錯誤,mysql有三種不同的處理方法,下面我們分別介紹。先建立2個測試表,在id列上建立unique約束。mysql create table test1 id int,name varchar 5 ty...

mysql插入防重複

普遍解決方法,要麼靠資料庫sql,要麼就是靠程式控制,新增分布式鎖,當然資料庫事務也需要配合使用 1 程式方面,新增version redis 或者資料庫冗餘表 2 資料庫方面 a 插入時新增exist判斷,比如 insert into test name select 4 from dual wh...

mysql 計算非重複 MySQL 不重複插入

sql unique 約束 unique 約束唯一標識資料庫表中的每條記錄。unique 和 primary key 約束均為列或列集合提供了唯一性的保證。primary key 擁有自動定義的 unique 約束。請注意,每個表可以有多個 unique 約束,但是每個表只能有乙個 primary ...