自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL插入處理重復鍵值的2種不同處理方案

數(shù)據(jù)庫 MySQL
此文章主要向大家描述的是MySQL插入處理重復鍵值方法,其中包括REPLACE INTO,INSERT INTO ON DUPLICATE KEY UPDATE這兩種方法。

我們大家都知道當unique列在一個UNIQUE鍵上需要插入包含重復值的記錄的時候,其默認insert的時候有時報錯誤,MySQL插入處理重復鍵值的2種不同的處理方法,下面我們對其進行分別介紹。

先建立2個測試表,在id列上創(chuàng)建unique約束。

  1. MySQL> create table test1(id int,name varchar(5),type int,primary key(id));   
  2. Query OK, 0 rows affected (0.01 sec)   
  3. MySQL> create table test2(id int,name varchar(5),type int,primary key(id));   
  4. Query OK, 0 rows affected (0.01 sec)   
  5. MySQL> select * from test1;   
  6. +-----+------+------+   
  7. | id | name | type |   
  8. +-----+------+------+   
  9. | 101 | aaa | 1 |   
  10. | 102 | bbb | 2 |   
  11. | 103 | ccc | 3 |   
  12. +-----+------+------+   
  13. 3 rows in set (0.00 sec)   
  14. MySQL> select * from test2;   
  15. +-----+------+------+   
  16. | id | name | type |   
  17. +-----+------+------+   
  18. | 201 | aaa | 1 |   
  19. | 202 | bbb | 2 |   
  20. | 203 | ccc | 3 |   
  21. | 101 | xxx | 5 |   
  22. +-----+------+------+   
  23. 4 rows in set (0.00 sec)  

MySQL插入處理重復鍵值方法1、REPLACE INTO

發(fā)現(xiàn)重復的先刪除再插入,如果記錄有多個字段,在插入的時候如果有的字段沒有賦值,那么新插入的記錄這些字段為空。

  1. MySQL> replace into test1(id,name)(select id,name from test2);   
  2. Query OK, 5 rows affected (0.04 sec)   
  3. Records: 4 Duplicates: 1 Warnings: 0   
  4. MySQL> select * from test1;   
  5. +-----+------+------+   
  6. | id | name | type |   
  7. +-----+------+------+   
  8. | 101 | xxx | NULL |   
  9. | 102 | bbb | 2 |   
  10. | 103 | ccc | 3 |   
  11. | 201 | aaa | NULL |   
  12. | 202 | bbb | NULL |   
  13. | 203 | ccc | NULL |   
  14. +-----+------+------+   
  15. 6 rows in set (0.00 sec)  

 

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

  1. MySQL> replace into test1(id,name,type)(select id,name,type from test2);   
  2. Query OK, 8 rows affected (0.04 sec)   
  3. Records: 4 Duplicates: 4 Warnings: 0   
  4. MySQL> select * from test1;   
  5. +-----+------+------+   
  6. | id | name | type |   
  7. +-----+------+------+   
  8. | 101 | xxx | 5 |   
  9. | 102 | bbb | 2 |   
  10. | 103 | ccc | 3 |   
  11. | 201 | aaa | 1 |   
  12. | 202 | bbb | 2 |   
  13. | 203 | ccc | 3 |   
  14. +-----+------+------+   
  15. 6 rows in set (0.00 sec)  

 

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

MySQL插入處理重復鍵值方法2、INSERT INTO ON DUPLICATE KEY UPDATE

發(fā)現(xiàn)重復的是更新操作。在原有記錄基礎上,更新指定字段內(nèi)容,其它字段內(nèi)容保留。例如我只想插入test2表的id,name字段,但是要保留test1表的type字段:

  1. MySQL> replace into test1(id,name,type)(select id,name,type from test2);   
  2. Query OK, 8 rows affected (0.04 sec)   
  3. Records: 4 Duplicates: 4 Warnings: 0   
  4. MySQL> select * from test1;   
  5. +-----+------+------+   
  6. | id | name | type |   
  7. +-----+------+------+   
  8. | 101 | xxx | 5 |   
  9. | 102 | bbb | 2 |   
  10. | 103 | ccc | 3 |   
  11. | 201 | aaa | 1 |   
  12. | 202 | bbb | 2 |   
  13. | 203 | ccc | 3 |   
  14. +-----+------+------+   
  15. 6 rows in set (0.00 sec)  

如果INSERT的時候,只想插入原表沒有的數(shù)據(jù),那么可以使用其他的MySQL插入處理重復鍵值的方法。

【編輯推薦】

  1. 實現(xiàn)MySQL 數(shù)據(jù)庫啟動在linux下
  2. MySQL連接報錯問題的正確解決方案
  3. mssql轉(zhuǎn)換為MySQL數(shù)據(jù)庫經(jīng)驗漫談
  4. MySQL免安裝版-添加服務實操演示
  5. 啟動與關(guān)閉MySQL的一大絕招

 

責任編輯:佚名 來源: ctocio
相關(guān)推薦

2010-06-07 13:20:39

MySQL插入處理重復

2010-07-02 10:33:18

SQL Server單

2010-10-13 17:13:17

MySQL重復記錄

2010-06-09 16:46:37

MySQL 亂碼處理

2024-06-17 07:41:43

2009-10-12 13:22:15

unique列

2024-10-10 10:07:07

2010-06-01 17:14:28

2010-06-02 13:46:19

MySQL慢查詢

2021-01-26 13:40:44

mysql數(shù)據(jù)庫

2009-06-24 07:51:56

Hibernate重復

2024-09-12 15:36:57

2015-07-29 13:37:41

妹子處理

2022-05-23 11:35:16

jiekou冪等性

2021-04-08 10:55:53

MySQL數(shù)據(jù)庫代碼

2010-12-23 13:56:55

SharePointIntranet

2017-05-23 16:26:26

MySQL優(yōu)化處理

2022-12-06 08:18:59

2021-06-17 09:32:39

重復請求并發(fā)請求Java

2024-04-23 08:40:00

數(shù)據(jù)積壓數(shù)據(jù)重復Kafka
點贊
收藏

51CTO技術(shù)棧公眾號