{"id":15825,"date":"2025-02-20T17:34:41","date_gmt":"2025-02-20T09:34:41","guid":{"rendered":"https:\/\/nj.transwarp.cn:8180\/?p=15825"},"modified":"2025-07-02T18:38:20","modified_gmt":"2025-07-02T10:38:20","slug":"kundb-unique%e7%b4%a2%e5%bc%95%e6%95%b0%e6%8d%ae%e9%87%8d%e5%a4%8d%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/kbwp.transwarp.cn\/?p=15825","title":{"rendered":"kundb unique\u7d22\u5f15\u6570\u636e\u91cd\u590d\u95ee\u9898"},"content":{"rendered":"<h3>\u6982\u8981\u63cf\u8ff0<\/h3>\n<hr \/>\n<p>\u5ba2\u6237\u53cd\u9988\uff0ckundb3.2.3\u7248\u672c\uff0c\u4e00\u5f20\u8868\u4e2d\u8bbe\u7f6e\u4e86\u552f\u4e00\u7d22\u5f15\uff08unique key\uff09\uff0c\u51fa\u73b0\u91cd\u590d\u6570\u636e\u3002<\/p>\n<p><strong>\u5148\u8bf4\u7ed3\u8bba\uff1a<\/strong><font color=red>\u666e\u901a\u7684\u552f\u4e00\u7d22\u5f15\u4e0e\u4e3b\u952e\u7d22\u5f15\u4ec5\u80fd\u4fdd\u8bc1 shard \u5185\u7684\u552f\u4e00\u6027<\/font><\/p>\n<p><strong>\u4e3a\u4fdd\u8bc1\u5b57\u6bb5\u7684\u5168\u5c40\u552f\u4e00\u6027\uff0c\u53ef\u91c7\u7528\u4e24\u79cd\u65b9\u5f0f\uff1a<\/strong><\/p>\n<p>a)\u4ee5\u8be5\u4e3b\u952e\/\u552f\u4e00\u7d22\u5f15\u5b57\u6bb5\u4e3a\u5206\u7247\u952e\uff1b<br \/>\nb)\u5bf9\u5b57\u6bb5\u521b\u5efa\u4e00\u4e2a \u5168\u5c40\u552f\u4e00\u7d22\u5f15 (GLOBAL UNIQUE INDEX)\uff0c\u5168\u5c40\u552f\u4e00\u7d22\u5f15\u80fd\u591f\u4fdd\u8bc1\u6240\u6709shard\u4e2d\u5b57\u6bb5\u7684\u552f\u4e00\u6027\u3002<\/p>\n<h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<hr \/>\n<h4>\u95ee\u9898\u8bf4\u660e<\/h4>\n<p>\u5efa\u8868\u8bed\u53e5\u4e2d\u914d\u7f6e\u4e86 <code>UNIQUE KEY<\/code> \uff0c\u4f46\u662f\u67e5\u8be2\u6570\u636e\u51fa\u73b02\u6761\u91cd\u590d\u6570\u636e\u3002<\/p>\n<div style=\"box-shadow: 1px 1px 10px rgba(0,0,0,0.1); padding: 1px;margin-bottom: 10px;\">\n<img decoding=\"async\" src=\"\/wp-content\/uploads\/2025\/02\/image-1740044020729.png\"><\/div>\n<p>\u76f4\u8fde3\u4e2ashard\u5e95\u5c42\u6570\u636e\uff0c1\u6761\u6570\u636e\u51fa\u73b0\u5728shard1\uff0c1\u6761\u51fa\u73b0\u5728shard2\u3002\u8fd9\u4e24\u6761\u6570\u636e\u4e0d\u5728\u540c\u4e00\u4e2ashard\u4e0a\u9762\u3002<\/p>\n<h4>\u89e3\u51b3\u65b9\u6848\uff1a<\/h4>\n<h5>\u65b9\u6848\u4e00\uff1a\u4ee5\u8be5\u4e3b\u952e\/\u552f\u4e00\u7d22\u5f15\u5b57\u6bb5\u4e3a\u5206\u7247\u952e\uff1b<\/h5>\n<pre><code class=\"language-sql\">--\u8be5\u5217\u5373\u662f unique key\uff0c\u4e5f\u662f\u5206\u7247\u952e\ndrop table if exists t4;\ncreate table t4(\nid1 int ,\nid2 int ,\nname varchar(32))\ndistributed by hash(id1);\n\nalter table t4 add unique(id1);\n\ninsert into t4 values (2,22,'aa');\ninsert into t4 values (2,33,'aab');<\/code><\/pre>\n<p>\u6267\u884c\u62a5\u9519\uff1a<code>1105 - unknown error: Duplicate entry &#039;2&#039; for key &#039;t4.id1&#039; (errno 1062) (sqlstate 23000) during query: \/*DRDS \/kv2:16015\/0\/1\/\/ *\/INSERT INTO t4(id1, id2, name) VALUES (2,33,&#039;aab&#039;)<\/code><\/p>\n<h5>\u65b9\u6848\u4e8c\uff1a\u5bf9\u5b57\u6bb5\u521b\u5efa\u4e00\u4e2a GLOBAL UNIQUE INDEX<\/h5>\n<pre><code class=\"language-sql\">drop table if exists t3;\ncreate table t3(\nid1 int ,\nid2 int ,\nname varchar(32))\ndistributed by hash(id2);\n\ncreate global unique  index idx_t3 on t3(id1);\n\ninsert into t3 values (2,22,'aa');\ninsert into t3 values (2,33,'aab');<\/code><\/pre>\n<p>\u6267\u884c\u62a5\u9519\uff1a<code>1105 - unknown error: Duplicate entry &#039;1&#039; for key &#039;idx_t3_t3_4kundb_admin.id1&#039; (errno 1062) (sqlstate 23000)<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981\u63cf\u8ff0 \u5ba2\u6237\u53cd\u9988\uff0ckundb3.2.3\u7248\u672c\uff0c\u4e00\u5f20\u8868\u4e2d\u8bbe\u7f6e\u4e86\u552f\u4e00\u7d22\u5f15\uff08unique key\uff09\uff0c\u51fa\u73b0\u91cd\u590d\u6570\u636e\u3002  ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/kbwp.transwarp.cn\/?p=15825\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-15825","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"acf":[],"_links":{"self":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/15825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15825"}],"version-history":[{"count":4,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/15825\/revisions"}],"predecessor-version":[{"id":16835,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/15825\/revisions\/16835"}],"wp:attachment":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}