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

淺談unique列上插入重復(fù)值的MySQL解決方案

數(shù)據(jù)庫 MySQL 數(shù)據(jù)庫運(yùn)維
本文將介紹在MySQL中unique列上插入重復(fù)值的解決方案,希望對(duì)廣大數(shù)據(jù)庫開發(fā)人員有所幫助。

本文的unique列上插入重復(fù)值解決方案,主要基于MySQL平臺(tái)。通過這些,可以做到一些新的功能和應(yīng)用。希望本文能對(duì)大家有所幫助。

當(dāng)unique列在一個(gè)UNIQUE鍵上插入包含重復(fù)值的記錄時(shí),我們可以控制MySQL如何處理這種情況:使用IGNORE關(guān)鍵字或者ON DUPLICATE KEY UPDATE子句跳過INSERT、中斷操作或者更新舊記錄為新值。

  1. mysql> create table menus(id tinyint(4) not null auto_increment,  
  2.    -> label varchar(10) null,url varchar(20) null,unique key(id));  
  3. Query OK, 0 rows affected (0.13 sec)  
  4. mysql> insert into menus(label,url) values('Home','home.html');  
  5. Query OK, 1 row affected (0.06 sec)  
  6. mysql> insert into menus(label,url) values('About us','aboutus.html');  
  7. Query OK, 1 row affected (0.05 sec)  
  8. mysql> insert into menus(label,url) values('Services','services.html');  
  9. Query OK, 1 row affected (0.05 sec)  
  10. mysql> insert into menus(label,url) values('Feedback','feedback.html');  
  11. Query OK, 1 row affected (0.05 sec) 
  1. mysql> select * from menus;  
  2. +----+----------+---------------+  
  3. | id | label   | url          |  
  4. +----+----------+---------------+  
  5. | 1 | Home    | home.html    |  
  6. | 2 | About us | aboutus.html |  
  7. | 3 | Services | services.html |  
  8. | 4 | Feedback | feedback.html |  
  9. +----+----------+---------------+  
  10. rows in set (0.00 sec) 

如果現(xiàn)在在unique列插入一條違背***約束的記錄,MySQL會(huì)中斷操作,提示出錯(cuò):

  1. mysql> insert into menus(id,label,url) values(4,'Contact us','contactus.html');  
  2. ERROR 1062 (23000): Duplicate entry '4' for key 'id' 

在前面的INSERT語句添加IGNORE關(guān)鍵字時(shí),如果認(rèn)為語句違背了***約束,MySQL甚至不會(huì)嘗試去執(zhí)行這條語句,因此,下面的語句不會(huì)返回錯(cuò)誤:

  1. mysql> insert ignore into menus(id,label,url) values(4,'Contact us','contactus.html');  
  2. Query OK, 0 rows affected (0.00 sec)  
  3. mysql> select * from menus;  
  4. +----+----------+---------------+  
  5. | id | label   | url          |  
  6. +----+----------+---------------+  
  7. | 1 | Home    | home.html    |  
  8. | 2 | About us | aboutus.html |  
  9. | 3 | Services | services.html |  
  10. | 4 | Feedback | feedback.html |  
  11. +----+----------+---------------+  
  12. rows in set (0.00 sec) 

當(dāng)有很多的INSERT語句需要被順序地執(zhí)行時(shí),IGNORE關(guān)鍵字就使操作變得很方便。使用它可以保證不管哪一個(gè)INSERT包含了重復(fù)的鍵值,MySQL都回跳過它(而不是放棄全部操作)。

在這種情況下,我們還可以通過添加MySQL4.1新增加的ON DUPLICATE KEY UPDATE子句,使MySQL自動(dòng)把INSERT操作轉(zhuǎn)換為UPDATE操作。這個(gè)子句必須具有需要更新的字段列表,這個(gè)列表和UPDATE語句使用的列表相同。

  1. mysql> insert into menus(id,label,url) values(4,'Contact us','contactus.html')  
  2.    -> on duplicate key update label='Contact us',url='contactus.html';  
  3. Query OK, 2 rows affected (0.05 sec) 

在這種情況下,如果MySQL發(fā)現(xiàn)表已經(jīng)包含具有相同***鍵的記錄,它會(huì)自動(dòng)更新舊的記錄為ON DUPLICATE KEY UPDATE從句中指定的新值:

  1. mysql> select * from menus;  
  2. +----+------------+----------------+  
  3. | id | label     | url           |  
  4. +----+------------+----------------+  
  5. | 1 | Home      | home.html     |  
  6. | 2 | About us  | aboutus.html  |  
  7. | 3 | Services  | services.html |  
  8. | 4 | Contact us | contactus.html |  
  9. +----+------------+----------------+  
  10. rows in set (0.01 sec) 

【編輯推薦】

  1. MySQL全文檢索中Like索引的實(shí)現(xiàn)
  2. 使用調(diào)度和鎖定進(jìn)行MySQL查詢優(yōu)化
  3. MySQL基本調(diào)度策略淺析
  4. MySQL一派添“輕功”Drizzle+PHP搶先體驗(yàn)
  5. 學(xué)會(huì)設(shè)置五大類MySQL參數(shù)
責(zé)任編輯:彭凡 來源: ITPUB
相關(guān)推薦

2017-04-13 11:20:37

圖片寬度解決方案前端

2011-06-20 13:24:11

網(wǎng)站快照

2021-10-18 07:58:33

MyBatis Plu數(shù)據(jù)庫批量插入

2010-05-17 09:49:46

MySQL中文問題

2014-03-06 10:52:57

Windows Ser重復(fù)數(shù)據(jù)

2010-06-07 13:08:53

MySQL插入處理重復(fù)

2010-05-28 19:39:28

MySQL 編碼轉(zhuǎn)換

2010-05-28 13:04:04

MySQL root

2019-07-16 06:30:19

MySQL同步延遲數(shù)據(jù)庫

2009-01-12 12:16:34

重復(fù)數(shù)據(jù)刪除選購方案

2009-07-15 17:00:49

JDBC查詢

2010-05-17 14:49:43

MySQL中文亂碼

2010-05-17 14:17:25

MySQL pytho

2010-05-11 17:56:59

MySQL Table

2010-04-28 11:48:13

Oracle MySQ

2010-05-12 16:47:54

MySQL 中文亂碼

2010-05-28 19:25:11

MySQL移植問題

2012-05-27 16:21:31

IDC華為

2018-12-03 12:17:27

Semptian解決方案

2018-12-03 11:59:42

Inventec解決方案
點(diǎn)贊
收藏

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