MYSQL插入處理重復(fù)鍵值的幾種方法
更新時間:2012年09月30日 21:34:43 作者:
當(dāng)unique列在一個UNIQUE鍵上插入包含重復(fù)值的記錄時,默認insert的時候會報1062錯誤,MYSQL有三種不同的處理方法,下面我們分別介紹。
先建立2個測試表,在id列上創(chuàng)建unique約束。
mysql> create table test1(id int,name varchar(5),type int,primary key(id));
Query OK, 0 rows affected (0.01 sec)
mysql> create table test2(id int,name varchar(5),type int,primary key(id));
Query OK, 0 rows affected (0.01 sec)
mysql> select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | aaa | 1 |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
+-----+------+------+
3 rows in set (0.00 sec)
mysql> select * from test2;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 201 | aaa | 1 |
| 202 | bbb | 2 |
| 203 | ccc | 3 |
| 101 | xxx | 5 |
+-----+------+------+
4 rows in set (0.00 sec)
1、REPLACE INTO
發(fā)現(xiàn)重復(fù)的先刪除再插入,如果記錄有多個字段,在插入的時候如果有的字段沒有賦值,那么新插入的記錄這些字段為空。
mysql> replace into test1(id,name)(select id,name from test2);
Query OK, 5 rows affected (0.04 sec)
Records: 4 Duplicates: 1 Warnings: 0
mysql> select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | xxx | NULL |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
| 201 | aaa | NULL |
| 202 | bbb | NULL |
| 203 | ccc | NULL |
+-----+------+------+
6 rows in set (0.00 sec)
需要注意的是,當(dāng)你replace的時候,如果被插入的表如果沒有指定列,會用NULL表示,而不是這個表原來的內(nèi)容。如果插入的內(nèi)容列和被插入的表列一樣,則不會出現(xiàn)NULL。例如
mysql> replace into test1(id,name,type)(select id,name,type from test2);
Query OK, 8 rows affected (0.04 sec)
Records: 4 Duplicates: 4 Warnings: 0
mysql> select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | xxx | 5 |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
| 201 | aaa | 1 |
| 202 | bbb | 2 |
| 203 | ccc | 3 |
+-----+------+------+
6 rows in set (0.00 sec)
如果INSERT的時候,需要保留被插入表的列,只更新指定列,那么就可以使用第二種方法。
2、INSERT INTO ON DUPLICATE KEY UPDATE
發(fā)現(xiàn)重復(fù)的是更新操作。在原有記錄基礎(chǔ)上,更新指定字段內(nèi)容,其它字段內(nèi)容保留。例如我只想插入test2表的id,name字段,但是要保留test1表的type字段:
mysql> insert into test1(id,name,type)(select id,name,type from test2) on DUPLICATE KEY UPDATE test1.name=test2.name;
Query OK, 5 rows affected (0.04 sec)
Records: 4 Duplicates: 1 Warnings: 0
mysql> select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | xxx | 1 |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
| 203 | ccc | 3 |
| 202 | bbb | 2 |
| 201 | aaa | 1 |
+-----+------+------+
6 rows in set (0.00 sec)
如果INSERT的時候,只想插入原表沒有的數(shù)據(jù),那么可以使用第三種方法。
3、IGNORE INTO
判斷是否存在,存在不插入,否則插入。很容易理解,當(dāng)插入的時候,違反唯一性約束,MySQL不會嘗試去執(zhí)行這條語句。例如:
mysql> insert ignore into test1(id,name,type)(select id,name,type from test2);
Query OK, 3 rows affected (0.01 sec)
Records: 4 Duplicates: 1 Warnings: 0
mysql> select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | aaa | 1 |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
| 203 | ccc | 3 |
| 202 | bbb | 2 |
| 201 | aaa | 1 |
+-----+------+------+
6 rows in set (0.00 sec)
mysql> create table test1(id int,name varchar(5),type int,primary key(id));
Query OK, 0 rows affected (0.01 sec)
mysql> create table test2(id int,name varchar(5),type int,primary key(id));
Query OK, 0 rows affected (0.01 sec)
mysql> select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | aaa | 1 |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
+-----+------+------+
3 rows in set (0.00 sec)
mysql> select * from test2;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 201 | aaa | 1 |
| 202 | bbb | 2 |
| 203 | ccc | 3 |
| 101 | xxx | 5 |
+-----+------+------+
4 rows in set (0.00 sec)
1、REPLACE INTO
發(fā)現(xiàn)重復(fù)的先刪除再插入,如果記錄有多個字段,在插入的時候如果有的字段沒有賦值,那么新插入的記錄這些字段為空。
mysql> replace into test1(id,name)(select id,name from test2);
Query OK, 5 rows affected (0.04 sec)
Records: 4 Duplicates: 1 Warnings: 0
mysql> select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | xxx | NULL |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
| 201 | aaa | NULL |
| 202 | bbb | NULL |
| 203 | ccc | NULL |
+-----+------+------+
6 rows in set (0.00 sec)
需要注意的是,當(dāng)你replace的時候,如果被插入的表如果沒有指定列,會用NULL表示,而不是這個表原來的內(nèi)容。如果插入的內(nèi)容列和被插入的表列一樣,則不會出現(xiàn)NULL。例如
mysql> replace into test1(id,name,type)(select id,name,type from test2);
Query OK, 8 rows affected (0.04 sec)
Records: 4 Duplicates: 4 Warnings: 0
mysql> select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | xxx | 5 |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
| 201 | aaa | 1 |
| 202 | bbb | 2 |
| 203 | ccc | 3 |
+-----+------+------+
6 rows in set (0.00 sec)
如果INSERT的時候,需要保留被插入表的列,只更新指定列,那么就可以使用第二種方法。
2、INSERT INTO ON DUPLICATE KEY UPDATE
發(fā)現(xiàn)重復(fù)的是更新操作。在原有記錄基礎(chǔ)上,更新指定字段內(nèi)容,其它字段內(nèi)容保留。例如我只想插入test2表的id,name字段,但是要保留test1表的type字段:
mysql> insert into test1(id,name,type)(select id,name,type from test2) on DUPLICATE KEY UPDATE test1.name=test2.name;
Query OK, 5 rows affected (0.04 sec)
Records: 4 Duplicates: 1 Warnings: 0
mysql> select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | xxx | 1 |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
| 203 | ccc | 3 |
| 202 | bbb | 2 |
| 201 | aaa | 1 |
+-----+------+------+
6 rows in set (0.00 sec)
如果INSERT的時候,只想插入原表沒有的數(shù)據(jù),那么可以使用第三種方法。
3、IGNORE INTO
判斷是否存在,存在不插入,否則插入。很容易理解,當(dāng)插入的時候,違反唯一性約束,MySQL不會嘗試去執(zhí)行這條語句。例如:
mysql> insert ignore into test1(id,name,type)(select id,name,type from test2);
Query OK, 3 rows affected (0.01 sec)
Records: 4 Duplicates: 1 Warnings: 0
mysql> select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | aaa | 1 |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
| 203 | ccc | 3 |
| 202 | bbb | 2 |
| 201 | aaa | 1 |
+-----+------+------+
6 rows in set (0.00 sec)
相關(guān)文章
數(shù)據(jù)結(jié)構(gòu)-樹(三):多路搜索樹B樹、B+樹
這篇文章主要介紹了多路搜索樹B樹、B+樹,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04MySQL做讀寫分離提高性能緩解數(shù)據(jù)庫壓力
這篇文章主要為大家介紹了MySQL做讀寫分離提高性能緩解數(shù)據(jù)庫壓力的技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05解決阿里云ECS服務(wù)器下安裝MySQL無法遠程連接的問題
這篇文章介紹了解決阿里云ECS服務(wù)器安裝MySQL無法遠程連接的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07MySQL數(shù)據(jù)庫優(yōu)化之索引實現(xiàn)原理與用法分析
這篇文章主要介紹了MySQL數(shù)據(jù)庫優(yōu)化之索引實現(xiàn)原理與用法,結(jié)合實例形式分析了mysql數(shù)據(jù)庫優(yōu)化操作的索引原理、具體實現(xiàn)與相關(guān)操作注意事項,需要的朋友可以參考下2020-01-01