insert\u6570\u636e<\/strong><\/span><\/div>\n#\u7b2c\u4e00\u6b21\u63d2\u5165\uff1a\r\ninsert into user(email,name) values('aaa@qq.com','aaa');\r\nAffected rows: 1, Time: 0.003000s\r\n#\u518d\u6b21\u63d2\u5165\u540c\u6837\u7684\u6570\u636e\uff1a\r\ninsert into user(email,name) values('aaa@qq.com','aaa');\r\n1062 - Duplicate entry 'aaa@qq.com' for key 'uk-email', Time: 0.005000s<\/pre>\n\u6b64\u65f6\u5bf9\u4e8e\u552f\u4e00\u6027\u6765\u8bf4\u662f\u6ca1\u95ee\u9898\u7684\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u4e1a\u52a1\u7684email\u7684\u552f\u4e00\u6027\u3002\u5047\u8bbe\u968f\u7740\u4e1a\u52a1\u7684\u53d1\u5c55\uff0c\u6b64\u65f6\u9700\u8981\u589e\u52a0\u624b\u673a\u53f7\u6ce8\u518c\u529f\u80fd\uff0c\u90a3\u4e48\u8868\u4e2d\u5c31\u9700\u8981\u589e\u52a0\u624b\u673a\u53f7\u5b57\u6bb5\uff0c\u4e14\u9700\u8981\u4fdd\u8bc1\u624b\u673a\u53f7\u548c\u90ae\u7bb1\u7684\u5173\u8054\u552f\u4e00\u6027\u3002<\/p>\n
#\u5efa\u8868\u8bed\u53e5\uff0c\u6ce8\u610f\u6b64\u65f6phone\u5b57\u6bb5\u7684\u9ed8\u8ba4\u503c\u4e3anull\r\nCREATE TABLE `user_2` (\r\n `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '\u4e3b\u952e',\r\n `email` varchar(32) NOT NULL DEFAULT '' COMMENT '\u90ae\u7bb1',\r\n `phone` char(11) DEFAULT NULL COMMENT '\u624b\u673a\u53f7',\r\n `name` varchar(11) DEFAULT '' COMMENT '\u540d\u5b57',\r\n PRIMARY KEY (`id`),\r\n UNIQUE KEY `uk-email-phone` (`email`,`phone`)\r\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;<\/pre>\ninsert\u6570\u636e<\/p>\n
insert into user_2(email,name) values('aaa@qq.com','aaa');\r\nAffected rows: 1, Time: 0.003000s\r\ninsert into user_2(email,name) values('aaa@qq.com','aaa');\r\nAffected rows: 1, Time: 0.003000s\r\ninsert into user_2(email,name) values('aaa@qq.com','aaa');\r\nAffected rows: 1, Time: 0.003000s\r\ninsert into user_2(email,phone,name) values('bbb@qq.com','13333333333','bbb');\r\nAffected rows: 1, Time: 0.003000s\r\ninsert into user_2(email,phone,name) values('bbb@qq.com','13333333333','bbb');\r\n1062 - Duplicate entry 'bbb@qq.com-13333333333' for key 'uk-email-phone', Time: 0.002000s<\/pre>\n\u6b64\u65f6\u4f1a\u53d1\u73b0\uff0c\u4e0d\u5e26phone\u503c\u5f97\u524d\u4e09\u6761\u6570\u636e\u90fd\u80fd\u63d2\u5165\u6210\u529f\uff0c\u5e26\u4e0a\u90ae\u7bb1\u548c\u624b\u673a\u53f7\u7684\u503c\u5374\u80fd\u6b63\u5e38\u5224\u65ad\u552f\u4e00\u6027<\/p>\n
mysql> select * from user_2;\r\n+----+------------+-------------+------+\r\n| id | email | phone | name |\r\n+----+------------+-------------+------+\r\n| 1 | aaa@qq.com | NULL | aaa |\r\n| 2 | aaa@qq.com | NULL | aaa |\r\n| 3 | aaa@qq.com | NULL | aaa |\r\n| 4 | bbb@qq.com | 13333333333 | bbb |\r\n+----+------------+-------------+------+\r\n4 rows in set (0.00 sec)<\/pre>\n\u8fd9\u65f6\u5c31\u9700\u8981\u7275\u626f\u5230MySQL\u7684\u552f\u4e00\u7d22\u5f15\u673a\u5236\u4e86\uff1a\u5728MySQL\u5b98\u65b9\u6587\u6863\u4e2dMySQL\u7d22\u5f15\u6587\u6863\uff0c\u63cf\u8ff0\u5230\uff1a<\/p>\n
A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. If you specify a prefix value for a column in a UNIQUE index, the column values must be unique within the prefix length. A UNIQUE index permits multiple NULL values for columns that can contain NULL.<\/pre>\n\u89e3\u91ca\u4e00\u4e0b\uff1a\u552f\u4e00\u7d22\u5f15\u521b\u5efa\u4e00\u4e2a\u7ea6\u675f\uff0c\u4f7f\u5f97\u7d22\u5f15\u4e2d\u7684\u6240\u6709\u503c\u90fd\u5fc5\u987b\u662f\u4e0d\u540c\u7684\u3002\u5982\u679c\u5c1d\u8bd5\u6dfb\u52a0\u4e00\u4e2a\u952e\u503c\u4e0e\u73b0\u6709\u884c\u5339\u914d\u7684\u65b0\u884c\uff0c\u5219\u4f1a\u53d1\u751f\u9519\u8bef\u3002\u5982\u679c\u5728\u552f\u4e00\u7d22\u5f15\u4e2d\u4e3a\u5217\u6307\u5b9a\u524d\u7f00\u503c\uff0c\u5219\u5217\u503c\u5728\u524d\u7f00\u957f\u5ea6\u5185\u5fc5\u987b\u662f\u552f\u4e00\u7684\u3002\u552f\u4e00\u7d22\u5f15\u5141\u8bb8\u5305\u542b\u7a7a\u503c\u7684\u5217\u6709\u591a\u4e2a\u7a7a\u503c\u3002<\/p>\n
\u5148\u770b\u4e0bexplain\u6267\u884c\u8ba1\u5212\uff1a<\/p>\n
\r\nmysql> explain select * from user_2 where email='aaa@qq.com' and phone is NULL;\r\n+----+-------------+--------+------------+------+----------------+----------------+---------+-------------+------+----------+-----------------------+\r\n| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |\r\n+----+-------------+--------+------------+------+----------------+----------------+---------+-------------+------+----------+-----------------------+\r\n| 1 | SIMPLE | user_2 | NULL | ref | uk-email-phone | uk-email-phone | 132 | const,const | 3 | 100.00 | Using index condition |\r\n+----+-------------+--------+------------+------+----------------+----------------+---------+-------------+------+----------+-----------------------+\r\n1 row in set, 1 warning (0.01 sec)\r\nmysql>\r\nmysql> explain select * from user_2 where email='bbb@qq.com' and phone='13333333333';\r\n+----+-------------+--------+------------+-------+----------------+----------------+---------+-------------+------+----------+-------+\r\n| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |\r\n+----+-------------+--------+------------+-------+----------------+----------------+---------+-------------+------+----------+-------+\r\n| 1 | SIMPLE | user_2 | NULL | const | uk-email-phone | uk-email-phone | 132 | const,const | 1 | 100.00 | NULL |\r\n+----+-------------+--------+------------+-------+----------------+----------------+---------+-------------+------+----------+-------+\r\n1 row in set, 1 warning (0.00 sec)<\/pre>\n\u6709\u6ca1\u6709\u53d1\u73b0\u4e00\u4e2a\u6709\u8da3\u7684\u73b0\u8c61\uff0c\u867d\u7136\u4e24\u4e2asql\u8bed\u53e5\u90fd\u4f7f\u7528\u5230\u4e86uk-email-phone\u552f\u4e00\u7d22\u5f15\uff0c\u4f46\u662f \u7b2c\u4e00\u6761sql\u7684type\u4e3aref \u7b2c\u4e8c\u6761sql\u7684type\u4e3aconst \u6211\u4eec\u77e5\u9053\uff0cexplain\u6267\u884c\u8ba1\u5212\u4e2d\uff0cconst\u4e00\u822c\u662f\u4e3b\u952e\u67e5\u8be2\u6216\u8005\u552f\u4e00\u7d22\u5f15\u67e5\u8be2\u662f\u624d\u4f1a\u51fa\u73b0\uff0c\u800cref\u4e00\u822c\u662f\u4f7f\u7528\u666e\u901a\u7d22\u5f15\u65f6\u51fa\u73b0\u3002\u6240\u4ee5\uff0c\u53ef\u4ee5\u5f97\u51fa\u7ed3\u8bba\uff0cMySQL\u5728\u5e95\u5c42\u5bf9\u552f\u4e00\u7d22\u5f15\u7684null\u503c\u505a\u4e86\u7279\u6b8a\u5904\u7406\u3002<\/p>\n
\u6211\u4eec\u901a\u8fc7\u67e5\u770b\u6e90\u7801\u6587\u4ef6\u76841863\u884c\uff0c\u6709\u8fd9\u4e48\u4e2a\u6ce8\u91ca\uff1a<\/p>\n
Scans a unique non-clustered index at a given index entry to determine whether a uniqueness violation has occurred for the key value of the entry. Set shared locks on possible duplicate records<\/pre>\n\u610f\u601d\u662f\u626b\u63cf\u7ed9\u5b9a\u7d22\u5f15\u9879\u5904\u7684\u552f\u4e00\u975e\u805a\u96c6\u7d22\u5f15\u4ee5\u786e\u5b9a\u6761\u76ee\u7684\u952e\u503c\u662f\u5426\u53d1\u751f\u552f\u4e00\u6027\u51b2\u7a81\u3002\u5bf9\u53ef\u80fd\u91cd\u590d\u7684\u8bb0\u5f55\u8bbe\u7f6e\u5171\u4eab\u9501\u3002<\/p>\n
\u4e5f\u5c31\u662f\u8bf4row_ins_scan_sec_index_for_duplicate()\u8be5\u65b9\u6cd5\u5c31\u662f\u5904\u7406\u552f\u4e00\u7d22\u5f15\u7684\uff0c\u7ee7\u7eed\u5f80\u4e0b\u770b\uff0c\u57281892\u884c\uff0c\u6709\u4e00\u4e32\u6ce8\u91ca\uff1a<\/p>\n
If the secondary index is unique, but one of the fields in the n_unique first fields is NULL, a unique key violation cannot occur, since we define NULL != NULL in this case<\/pre>\n\u5982\u679c\u4e8c\u7ea7\u7d22\u5f15\u662f\u552f\u4e00\u7684\uff0c\u4f46\u662f\u552f\u4e00\u7d22\u5f15\u7684\u5b57\u6bb5\u5b58\u5728NULL\uff0c\u5219\u4e0d\u4f1a\u53d1\u751f\u552f\u4e00\u6027\u51b2\u7a81\uff0c\u5728\u6b64\u4ee3\u7801\u4e2d\u5b9a\u4e49\u4e86NULL != NULL<\/p>\n
\u7ee7\u7eed\u5f80\u4e0b\u8d70\uff0c\u57281996\u884c\uff0c\u8d70\u5230\u4e86row_ins_dupl_error_with_rec()\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u57281825\u884c\u3002\u5728\u8be5\u51fd\u6570\u4e2d\u6709\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n
\/* In a unique secondary index we allow equal key values if they\r\n contain SQL NULLs \r\n \u5728\u552f\u4e00\u7684\u4e8c\u7ea7\u7d22\u5f15\u4e2d\uff0c\u5982\u679c\u5305\u542bsql NULL\u503c\r\n*\/\r\n if (!index->is_clustered() && !index->nulls_equal) {\r\n for (i = 0; i < n_unique; i++) {\r\n if (dfield_is_null(dtuple_get_nth_field(entry, i))) {\r\n return (FALSE);\r\n }\r\n }\r\n }<\/pre>\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u552f\u4e00\u7d22\u5f15\u4e2d\u5b57\u6bb5\u4e3aNULL\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd4\u56defalse\uff0c\u6ca1\u6709\u629b\u51faDB_DUPLICATE_KEY\u5f02\u5e38.<\/p>\n
\u552f\u4e00\u7d22\u5f15\u91cd\u590d\u63d2\u5165\u4e4b\u7ec8\u6781\u89e3\u51b3\u65b9\u6848\uff1a\u7ed9\u5b57\u6bb5\u8bbe\u7f6e\u7a7a\u5b57\u7b26\u4e32\u521d\u59cb\u503c\uff0cNOT NULL DEFAULT ''\u5373\u53ef\uff0c\u4e0d\u8981\u7528null\u503c\u4f5c\u4e3a\u521d\u59cb\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5728\u65e5\u5e38\u4e1a\u52a1\u5f00\u53d1\u4e2d\uff0c\u4f1a\u7ecf\u5e38\u9047\u5230\u9700\u8981\u4fdd\u8bc1\u552f\u4e00\u6027\u7684\u6570\u636e\u4e1a\u52a1\uff0c\u5982\u7528\u6237\u6ce8\u518c\u4e1a\u52a1\u3002\u4e00\u822c\u6ce8\u518c\u4e1a\u52a1\u4e2d\u5141\u8bb8\u7528\u6237\u4ee5\u624b\u673a\u53f7\u6216emai […]<\/p>\n","protected":false},"author":668,"featured_media":108865,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-253851","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/253851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=253851"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/253851\/revisions"}],"predecessor-version":[{"id":253855,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/253851\/revisions\/253855"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/108865"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=253851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=253851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=253851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}