{"id":171367,"date":"2024-11-07T02:09:18","date_gmt":"2024-11-06T18:09:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/171367\/"},"modified":"2024-11-07T02:09:18","modified_gmt":"2024-11-06T18:09:18","slug":"%e6%b7%ba%e8%ab%87unique%e5%88%97%e4%b8%8a%e6%8f%92%e5%85%a5%e9%87%8d%e8%a4%87%e5%80%bc%e7%9a%84mysql%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/171367\/","title":{"rendered":"\u6dfa\u8ac7unique\u5217\u4e0a\u63d2\u5165\u91cd\u8907\u503c\u7684MySQL\u89e3\u6c7a\u65b9\u6848"},"content":{"rendered":"<h1 id=\"%e6%b7%ba%e8%ab%87unique%e5%88%97%e4%b8%8a%e6%8f%92%e5%85%a5%e9%87%8d%e8%a4%87%e5%80%bc%e7%9a%84mysql%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-ESxXSNDqDK\">\u6dfa\u8ac7unique\u5217\u4e0a\u63d2\u5165\u91cd\u8907\u503c\u7684MySQL\u89e3\u6c7a\u65b9\u6848<\/h1>\n<p>\u5728\u4f7f\u7528MySQL\u9032\u884c\u6578\u64da\u5eab\u7ba1\u7406\u6642\uff0cunique\u7d04\u675f\u662f\u4e00\u500b\u91cd\u8981\u7684\u7279\u6027\uff0c\u5b83\u78ba\u4fdd\u4e86\u67d0\u4e00\u5217\u4e2d\u7684\u6240\u6709\u503c\u90fd\u662f\u552f\u4e00\u7684\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u9700\u8981\u5728unique\u5217\u4e0a\u63d2\u5165\u91cd\u8907\u503c\u7684\u60c5\u6cc1\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u9019\u4e00\u554f\u984c\u7684\u6839\u672c\u539f\u56e0\u53ca\u5176\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e6%9c%83%e9%9c%80%e8%a6%81%e5%9c%a8unique%e5%88%97%e4%b8%8a%e6%8f%92%e5%85%a5%e9%87%8d%e8%a4%87%e5%80%bc%ef%bc%9f-ESxXSNDqDK\">\u70ba\u4ec0\u9ebc\u6703\u9700\u8981\u5728unique\u5217\u4e0a\u63d2\u5165\u91cd\u8907\u503c\uff1f<\/h2>\n<p>\u5728\u6578\u64da\u5eab\u8a2d\u8a08\u4e2d\uff0cunique\u7d04\u675f\u901a\u5e38\u7528\u65bc\u78ba\u4fdd\u6578\u64da\u7684\u5b8c\u6574\u6027\u3002\u4f8b\u5982\uff0c\u96fb\u5b50\u90f5\u4ef6\u5730\u5740\u6216\u7528\u6236\u540d\u901a\u5e38\u88ab\u8a2d\u7f6e\u70baunique\uff0c\u4ee5\u9632\u6b62\u91cd\u8907\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u958b\u767c\u8005\u53ef\u80fd\u9700\u8981\u5728\u9019\u4e9b\u5217\u4e2d\u63d2\u5165\u91cd\u8907\u503c\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u6578\u64da\u9077\u79fb\uff1a\u5728\u5c07\u6578\u64da\u5f9e\u4e00\u500b\u7cfb\u7d71\u9077\u79fb\u5230\u53e6\u4e00\u500b\u7cfb\u7d71\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u91cd\u8907\u7684unique\u503c\u3002<\/li>\n<li>\u6e2c\u8a66\u74b0\u5883\uff1a\u5728\u6e2c\u8a66\u74b0\u5883\u4e2d\uff0c\u958b\u767c\u8005\u53ef\u80fd\u9700\u8981\u63d2\u5165\u91cd\u8907\u6578\u64da\u4ee5\u9032\u884c\u6e2c\u8a66\u3002<\/li>\n<li>\u696d\u52d9\u9700\u6c42\u8b8a\u66f4\uff1a\u96a8\u8457\u696d\u52d9\u9700\u6c42\u7684\u8b8a\u5316\uff0c\u53ef\u80fd\u9700\u8981\u91cd\u65b0\u8003\u616eunique\u7d04\u675f\u7684\u8a2d\u7f6e\u3002<\/li>\n<\/ul>\n<h2 id=\"%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-ESxXSNDqDK\">\u89e3\u6c7a\u65b9\u6848<\/h2>\n<p>\u8981\u5728unique\u5217\u4e0a\u63d2\u5165\u91cd\u8907\u503c\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8003\u616e\u4ee5\u4e0b\u5e7e\u7a2e\u89e3\u6c7a\u65b9\u6848\uff1a<\/p>\n<h3 id=\"1-%e6%9a%ab%e6%99%82%e7%a7%bb%e9%99%a4unique%e7%b4%84%e6%9d%9f-ESxXSNDqDK\">1. \u66ab\u6642\u79fb\u9664unique\u7d04\u675f<\/h3>\n<p>\u6700\u76f4\u63a5\u7684\u89e3\u6c7a\u65b9\u6848\u662f\u66ab\u6642\u79fb\u9664unique\u7d04\u675f\uff0c\u7136\u5f8c\u9032\u884c\u6578\u64da\u63d2\u5165\uff0c\u6700\u5f8c\u518d\u91cd\u65b0\u6dfb\u52a0\u7d04\u675f\u3002\u9019\u7a2e\u65b9\u6cd5\u96d6\u7136\u7c21\u55ae\uff0c\u4f46\u9700\u8981\u8b39\u614e\u64cd\u4f5c\uff0c\u4ee5\u907f\u514d\u6578\u64da\u4e0d\u4e00\u81f4\u7684\u60c5\u6cc1\u3002<\/p>\n<pre><code>ALTER TABLE your_table DROP INDEX unique_index_name;\n-- \u63d2\u5165\u91cd\u8907\u503c\nINSERT INTO your_table (column1, column2) VALUES ('value1', 'value2');\nALTER TABLE your_table ADD UNIQUE (your_unique_column);<\/code><\/pre>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8insert-ignore-ESxXSNDqDK\">2. \u4f7f\u7528INSERT IGNORE<\/h3>\n<p>\u5982\u679c\u5e0c\u671b\u5728\u63d2\u5165\u6642\u5ffd\u7565\u91cd\u8907\u7684unique\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528INSERT IGNORE\u8a9e\u53e5\u3002\u9019\u6a23\uff0c\u7576\u63d2\u5165\u91cd\u8907\u503c\u6642\uff0cMySQL\u5c07\u4e0d\u6703\u5831\u932f\uff0c\u800c\u662f\u7c21\u55ae\u5730\u5ffd\u7565\u8a72\u63d2\u5165\u64cd\u4f5c\u3002<\/p>\n<pre><code>INSERT IGNORE INTO your_table (your_unique_column, other_column) VALUES ('duplicate_value', 'other_value');<\/code><\/pre>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8replace-into-ESxXSNDqDK\">3. \u4f7f\u7528REPLACE INTO<\/h3>\n<p>\u53e6\u4e00\u7a2e\u65b9\u6cd5\u662f\u4f7f\u7528REPLACE INTO\u8a9e\u53e5\u3002\u9019\u500b\u8a9e\u53e5\u6703\u5148\u6aa2\u67e5\u662f\u5426\u5b58\u5728\u91cd\u8907\u7684unique\u503c\uff0c\u5982\u679c\u5b58\u5728\uff0c\u5247\u522a\u9664\u820a\u7684\u8a18\u9304\u4e26\u63d2\u5165\u65b0\u7684\u8a18\u9304\u3002<\/p>\n<pre><code>REPLACE INTO your_table (your_unique_column, other_column) VALUES ('duplicate_value', 'new_value');<\/code><\/pre>\n<h3 id=\"4-%e4%bd%bf%e7%94%a8on-duplicate-key-update-ESxXSNDqDK\">4. \u4f7f\u7528ON DUPLICATE KEY UPDATE<\/h3>\n<p>\u5982\u679c\u5e0c\u671b\u5728\u63d2\u5165\u91cd\u8907\u503c\u6642\u66f4\u65b0\u73fe\u6709\u8a18\u9304\uff0c\u53ef\u4ee5\u4f7f\u7528ON DUPLICATE KEY UPDATE\u8a9e\u53e5\u3002\u9019\u6a23\u53ef\u4ee5\u5728\u63d2\u5165\u6642\u9032\u884c\u66f4\u65b0\uff0c\u800c\u4e0d\u6703\u5c0e\u81f4\u932f\u8aa4\u3002<\/p>\n<pre><code>INSERT INTO your_table (your_unique_column, other_column) VALUES ('duplicate_value', 'new_value')\nON DUPLICATE KEY UPDATE other_column = 'updated_value';<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-ESxXSNDqDK\">\u7d50\u8ad6<\/h2>\n<p>\u5728MySQL\u4e2d\uff0cunique\u7d04\u675f\u662f\u78ba\u4fdd\u6578\u64da\u5b8c\u6574\u6027\u7684\u91cd\u8981\u5de5\u5177\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u958b\u767c\u8005\u53ef\u80fd\u9700\u8981\u5728unique\u5217\u4e0a\u63d2\u5165\u91cd\u8907\u503c\u3002\u901a\u904e\u66ab\u6642\u79fb\u9664\u7d04\u675f\u3001\u4f7f\u7528INSERT IGNORE\u3001REPLACE INTO\u6216ON DUPLICATE KEY UPDATE\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u6c7a\u9019\u4e00\u554f\u984c\u3002\u4e86\u89e3\u9019\u4e9b\u89e3\u6c7a\u65b9\u6848\u5c07\u6709\u52a9\u65bc\u958b\u767c\u8005\u5728\u9762\u5c0d\u6578\u64da\u5eab\u6311\u6230\u6642\u505a\u51fa\u66f4\u660e\u667a\u7684\u6c7a\u7b56\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f\u6578\u64da\u5eab\u7ba1\u7406\u9084\u662f\u7db2\u7ad9\u6258\u7ba1\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u8a0eMySQL\u4e2dunique\u5217\u4e0a\u63d2\u5165\u91cd\u8907\u503c\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u63d0\u4f9b\u6709\u6548\u7684\u65b9\u6cd5\u8207\u6280\u5de7\uff0c\u52a9\u60a8\u8f15\u9b06\u7ba1\u7406\u6578\u64da\u5b8c\u6574\u6027\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-171367","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=171367"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171367\/revisions"}],"predecessor-version":[{"id":171368,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171367\/revisions\/171368"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=171367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=171367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=171367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}