国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > MySQL > 正文

Mysql中 unique列插入重復(fù)值該怎么解決呢

2024-07-24 13:08:04
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文給大家介紹mysql中unique列插入重復(fù)值的解決方案,主要基于mysql平臺(tái),通過(guò)這些,可以做到一些新的功能和應(yīng)用。特此把本文本文分享給廣大開(kāi)發(fā)人員

當(dāng)unique列在一個(gè)UNIQUE鍵上插入包含重復(fù)值的記錄時(shí),我們可以控制MySQL如何處理這種情況:使用IGNORE關(guān)鍵字或者ON DUPLICATE KEY UPDATE子句跳過(guò)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)  
  12. mysql> select * from menus;  
  13. +----+----------+---------------+  
  14. | id | label | url |  
  15. +----+----------+---------------+  
  16. | 1 | Home | home.html |  
  17. | 2 | About us | aboutus.html |  
  18. | 3 | Services | services.html |  
  19. | 4 | Feedback | feedback.html |  
  20. +----+----------+---------------+  
  21. 4 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語(yǔ)句添加IGNORE關(guān)鍵字時(shí),如果認(rèn)為語(yǔ)句違背了唯一約束,MySQL甚至不會(huì)嘗試去執(zhí)行這條語(yǔ)句,因此,下面的語(yǔ)句不會(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. 4 rows in set (0.00 sec) 

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

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

 

 
  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. 4 rows in set (0.01 sec) 

以上內(nèi)容是小編給大家介紹的Mysql中 unique列插入重復(fù)值該怎么解決的全部教程,希望對(duì)大家有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到MYSQL教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 田阳县| 两当县| 呼图壁县| 盖州市| 民勤县| 肇庆市| 永靖县| 卢湾区| 安平县| 德安县| 崇阳县| 邹平县| 扶沟县| 河源市| 新密市| 乌苏市| 灌云县| 大荔县| 繁昌县| 柯坪县| 商丘市| 津南区| 广南县| 河间市| 彭州市| 乐都县| 思南县| 潮安县| 邹城市| 拉萨市| 湘潭县| 德清县| 临海市| 上林县| 平昌县| 乾安县| 道孚县| 金川县| 大连市| 宜春市| 高雄市|