{"id":76786,"date":"2024-10-16T09:55:15","date_gmt":"2024-10-16T01:55:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/76786\/"},"modified":"2024-10-16T09:55:15","modified_gmt":"2024-10-16T01:55:15","slug":"mysql%e7%b4%a2%e5%bc%95%e9%a1%9e%e5%9e%8b%e4%bb%8b%e7%b4%b9","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/76786\/","title":{"rendered":"MySQL\u7d22\u5f15\u985e\u578b\u4ecb\u7d39"},"content":{"rendered":"<h1 id=\"mysql%e7%b4%a2%e5%bc%95%e9%a1%9e%e5%9e%8b%e4%bb%8b%e7%b4%b9-uWmbYEVvPf\">MySQL\u7d22\u5f15\u985e\u578b\u4ecb\u7d39<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0c\u7d22\u5f15\u662f\u4e00\u7a2e\u91cd\u8981\u7684\u6578\u64da\u7d50\u69cb\uff0c\u7528\u65bc\u63d0\u9ad8\u67e5\u8a62\u6548\u7387\u3002MySQL\u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684\u958b\u6e90\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u7d22\u5f15\u985e\u578b\uff0c\u4ee5\u6eff\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u672c\u6587\u5c07\u4ecb\u7d39MySQL\u4e2d\u7684\u4e3b\u8981\u7d22\u5f15\u985e\u578b\u53ca\u5176\u7279\u9ede\u3002<\/p>\n<h2 id=\"1-%e4%b8%bb%e9%8d%b5%e7%b4%a2%e5%bc%95%ef%bc%88primary-key-index%ef%bc%89-uWmbYEVvPf\">1. \u4e3b\u9375\u7d22\u5f15\uff08Primary Key Index\uff09<\/h2>\n<p>\u4e3b\u9375\u7d22\u5f15\u662f\u552f\u4e00\u6a19\u8b58\u8868\u4e2d\u6bcf\u4e00\u884c\u7684\u7d22\u5f15\u3002\u6bcf\u500b\u8868\u53ea\u80fd\u6709\u4e00\u500b\u4e3b\u9375\uff0c\u4e14\u4e3b\u9375\u7684\u503c\u4e0d\u80fd\u70baNULL\u3002\u4e3b\u9375\u7d22\u5f15\u901a\u5e38\u4f7f\u7528B-tree\u7d50\u69cb\u4f86\u5be6\u73fe\uff0c\u9019\u4f7f\u5f97\u67e5\u8a62\u901f\u5ea6\u975e\u5e38\u5feb\u3002<\/p>\n<pre><code>CREATE TABLE users (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    username VARCHAR(50) NOT NULL,\n    email VARCHAR(100) NOT NULL\n);<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0cid\u5b57\u6bb5\u88ab\u8a2d\u7f6e\u70ba\u4e3b\u9375\uff0c\u9019\u610f\u5473\u8457\u6bcf\u500b\u7528\u6236\u90fd\u6703\u6709\u4e00\u500b\u552f\u4e00\u7684id\u3002<\/p>\n<h2 id=\"2-%e5%94%af%e4%b8%80%e7%b4%a2%e5%bc%95%ef%bc%88unique-index%ef%bc%89-uWmbYEVvPf\">2. \u552f\u4e00\u7d22\u5f15\uff08Unique Index\uff09<\/h2>\n<p>\u552f\u4e00\u7d22\u5f15\u8207\u4e3b\u9375\u7d22\u5f15\u985e\u4f3c\uff0c\u4f46\u5141\u8a31NULL\u503c\u3002\u9019\u610f\u5473\u8457\u5728\u552f\u4e00\u7d22\u5f15\u7684\u5217\u4e2d\uff0c\u6240\u6709\u7684\u503c\u5fc5\u9808\u662f\u552f\u4e00\u7684\uff0c\u4f46\u53ef\u4ee5\u6709\u591a\u500bNULL\u503c\u3002\u9019\u5c0d\u65bc\u9700\u8981\u4fdd\u6301\u6578\u64da\u552f\u4e00\u6027\u7684\u60c5\u6cc1\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>CREATE TABLE products (\n    product_id INT AUTO_INCREMENT,\n    product_name VARCHAR(100) NOT NULL,\n    sku VARCHAR(50) UNIQUE,\n    PRIMARY KEY (product_id)\n);<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0csku\u5b57\u6bb5\u88ab\u8a2d\u7f6e\u70ba\u552f\u4e00\u7d22\u5f15\uff0c\u78ba\u4fdd\u6bcf\u500b\u7522\u54c1\u7684SKU\u90fd\u662f\u552f\u4e00\u7684\u3002<\/p>\n<h2 id=\"3-%e6%99%ae%e9%80%9a%e7%b4%a2%e5%bc%95%ef%bc%88index%ef%bc%89-uWmbYEVvPf\">3. \u666e\u901a\u7d22\u5f15\uff08Index\uff09<\/h2>\n<p>\u666e\u901a\u7d22\u5f15\u662f\u6700\u57fa\u672c\u7684\u7d22\u5f15\u985e\u578b\uff0c\u6c92\u6709\u4efb\u4f55\u9650\u5236\u3002\u5b83\u53ef\u4ee5\u52a0\u901f\u67e5\u8a62\uff0c\u4f46\u4e0d\u4fdd\u8b49\u552f\u4e00\u6027\u3002\u666e\u901a\u7d22\u5f15\u9069\u5408\u65bc\u67e5\u8a62\u983b\u7e41\u4f46\u4e0d\u9700\u8981\u552f\u4e00\u6027\u7684\u5b57\u6bb5\u3002<\/p>\n<pre><code>CREATE INDEX idx_username ON users (username);<\/code><\/pre>\n<p>\u9019\u689d\u8a9e\u53e5\u70bausers\u8868\u4e2d\u7684username\u5b57\u6bb5\u5275\u5efa\u4e86\u4e00\u500b\u666e\u901a\u7d22\u5f15\uff0c\u9019\u6a23\u5728\u67e5\u8a62\u7528\u6236\u540d\u6642\u6703\u66f4\u5feb\u3002<\/p>\n<h2 id=\"4-%e5%85%a8%e6%96%87%e7%b4%a2%e5%bc%95%ef%bc%88fulltext-index%ef%bc%89-uWmbYEVvPf\">4. \u5168\u6587\u7d22\u5f15\uff08Fulltext Index\uff09<\/h2>\n<p>\u5168\u6587\u7d22\u5f15\u4e3b\u8981\u7528\u65bc\u5c0d\u6587\u672c\u9032\u884c\u641c\u7d22\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u9032\u884c\u95dc\u9375\u5b57\u641c\u7d22\u7684\u60c5\u6cc1\u4e0b\u3002\u5b83\u652f\u6301\u81ea\u7136\u8a9e\u8a00\u641c\u7d22\u548c\u5e03\u723e\u641c\u7d22\uff0c\u9069\u5408\u7528\u65bc\u5927\u6587\u672c\u5b57\u6bb5\u3002<\/p>\n<pre><code>CREATE TABLE articles (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    title VARCHAR(255),\n    content TEXT,\n    FULLTEXT (title, content)\n);<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u5c0dtitle\u548ccontent\u5b57\u6bb5\u5275\u5efa\u4e86\u5168\u6587\u7d22\u5f15\uff0c\u9019\u6a23\u53ef\u4ee5\u5feb\u901f\u67e5\u627e\u5305\u542b\u7279\u5b9a\u95dc\u9375\u5b57\u7684\u6587\u7ae0\u3002<\/p>\n<h2 id=\"5-%e7%a9%ba%e9%96%93%e7%b4%a2%e5%bc%95%ef%bc%88spatial-index%ef%bc%89-uWmbYEVvPf\">5. \u7a7a\u9593\u7d22\u5f15\uff08Spatial Index\uff09<\/h2>\n<p>\u7a7a\u9593\u7d22\u5f15\u5c08\u9580\u7528\u65bc\u5730\u7406\u6578\u64da\u985e\u578b\uff0c\u80fd\u5920\u52a0\u901f\u7a7a\u9593\u67e5\u8a62\u3002\u9019\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5730\u7406\u4fe1\u606f\u7cfb\u7d71\uff08GIS\uff09\u6578\u64da\u7684\u61c9\u7528\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<pre><code>CREATE TABLE locations (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    name VARCHAR(100),\n    coordinates POINT,\n    SPATIAL INDEX (coordinates)\n);<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0ccoordinates\u5b57\u6bb5\u88ab\u8a2d\u7f6e\u70ba\u7a7a\u9593\u7d22\u5f15\uff0c\u4ee5\u4fbf\u5feb\u901f\u67e5\u8a62\u5730\u7406\u4f4d\u7f6e\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-uWmbYEVvPf\">\u7e3d\u7d50<\/h2>\n<p>MySQL\u63d0\u4f9b\u4e86\u591a\u7a2e\u7d22\u5f15\u985e\u578b\uff0c\u4ee5\u6eff\u8db3\u4e0d\u540c\u7684\u67e5\u8a62\u9700\u6c42\u3002\u9078\u64c7\u5408\u9069\u7684\u7d22\u5f15\u985e\u578b\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6578\u64da\u5eab\u7684\u6027\u80fd\u3002\u5728\u4f7f\u7528MySQL\u9032\u884c\u6578\u64da\u7ba1\u7406\u6642\uff0c\u4e86\u89e3\u9019\u4e9b\u7d22\u5f15\u985e\u578b\u53ca\u5176\u7279\u9ede\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5982\u679c\u60a8\u9700\u8981\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u90e8\u7f72\u60a8\u7684MySQL\u6578\u64da\u5eab\uff0c\u9019\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u60a8\u7684\u61c9\u7528\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MySQL\u7d22\u5f15\u985e\u578b\uff0c\u5305\u62ecB\u6a39\u3001\u54c8\u5e0c\u3001\u5168\u6587\u7d22\u5f15\u7b49\uff0c\u63d0\u5347\u67e5\u8a62\u6548\u7387\uff0c\u512a\u5316\u6578\u64da\u5eab\u6027\u80fd\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-76786","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76786","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=76786"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76786\/revisions"}],"predecessor-version":[{"id":76787,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76786\/revisions\/76787"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=76786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=76786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=76786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}