{"id":242,"date":"2023-02-24T06:01:52","date_gmt":"2023-02-23T22:01:52","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=242"},"modified":"2023-04-30T15:20:19","modified_gmt":"2023-04-30T07:20:19","slug":"mysql-index","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/24\/mysql-index\/","title":{"rendered":"MySQL \u7d22\u5f15"},"content":{"rendered":"<p>MySQL\u7d22\u5f15\u7684\u5efa\u7acb\u5bf9\u4e8eMySQL\u7684\u9ad8\u6548\u8fd0\u884c\u662f\u5f88\u91cd\u8981\u7684\uff0c\u7d22\u5f15\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8MySQL\u7684\u68c0\u7d22\u901f\u5ea6\u3002<\/p>\n<p>\u4ee5\u6c49\u8bed\u5b57\u5178\u7684\u76ee\u5f55\u9875\uff08\u7d22\u5f15\uff09\u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u6309\u62fc\u97f3\u3001\u7b14\u753b\u3001\u504f\u65c1\u90e8\u9996\u7b49\u6392\u5e8f\u7684\u76ee\u5f55\uff08\u7d22\u5f15\uff09\u5feb\u901f\u67e5\u627e\u5230\u9700\u8981\u7684\u5b57\u3002<\/p>\n<p>\u7d22\u5f15\u5206\u5355\u5217\u7d22\u5f15\u548c\u7ec4\u5408\u7d22\u5f15\u3002\u5355\u5217\u7d22\u5f15\uff0c\u5373\u4e00\u4e2a\u7d22\u5f15\u53ea\u5305\u542b\u5355\u4e2a\u5217\uff0c\u4e00\u4e2a\u8868\u53ef\u4ee5\u6709\u591a\u4e2a\u5355\u5217\u7d22\u5f15\uff0c\u4f46\u8fd9\u4e0d\u662f\u7ec4\u5408\u7d22\u5f15\u3002\u7ec4\u5408\u7d22\u5f15\uff0c\u5373\u4e00\u4e2a\u7d22\u5f15\u5305\u542b\u591a\u4e2a\u5217\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u521b\u5efa\u7d22\u5f15\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u8be5\u7d22\u5f15\u662f\u5e94\u7528\u5728SQL\u67e5\u8be2\u8bed\u53e5\u7684\u6761\u4ef6(\u4e00\u822c\u4f5c\u4e3a WHERE\u5b50\u53e5\u7684\u6761\u4ef6)\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u7d22\u5f15\u4e5f\u662f\u4e00\u5f20\u8868\uff0c\u8be5\u8868\u4fdd\u5b58\u4e86\u4e3b\u952e\u4e0e\u7d22\u5f15\u5b57\u6bb5\uff0c\u5e76\u6307\u5411\u5b9e\u4f53\u8868\u7684\u8bb0\u5f55\u3002<\/p>\n<p>\u4e0a\u9762\u90fd\u5728\u8bf4\u4f7f\u7528\u7d22\u5f15\u7684\u597d\u5904\uff0c\u4f46\u8fc7\u591a\u7684\u4f7f\u7528\u7d22\u5f15\u5c06\u4f1a\u9020\u6210\u6ee5\u7528\u3002\u56e0\u6b64\u7d22\u5f15\u4e5f\u4f1a\u6709\u5b83\u7684\u7f3a\u70b9\uff1a\u867d\u7136\u7d22\u5f15\u5927\u5927\u63d0\u9ad8\u4e86\u67e5\u8be2\u901f\u5ea6\uff0c\u540c\u65f6\u5374\u4f1a\u964d\u4f4e\u66f4\u65b0\u8868\u7684\u901f\u5ea6\uff0c\u5982\u5bf9\u8868\u8fdb\u884c<code>INSERT<\/code>\u3001<code>UPDATE<\/code>\u548c<code>DELETE<\/code>\u3002\u56e0\u4e3a\u66f4\u65b0\u8868\u65f6\uff0cMySQL\u4e0d\u4ec5\u8981\u4fdd\u5b58\u6570\u636e\uff0c\u8fd8\u8981\u4fdd\u5b58\u4e00\u4e0b\u7d22\u5f15\u6587\u4ef6\u3002<\/p>\n<p>\u5efa\u7acb\u7d22\u5f15\u4f1a\u5360\u7528\u78c1\u76d8\u7a7a\u95f4\u7684\u7d22\u5f15\u6587\u4ef6\u3002<\/p>\n<h2>\u666e\u901a\u7d22\u5f15<\/h2>\n<h3>\u521b\u5efa\u7d22\u5f15<\/h3>\n<p>\u8fd9\u662f\u6700\u57fa\u672c\u7684\u7d22\u5f15\uff0c\u6ca1\u6709\u4efb\u4f55\u9650\u5236\u3002\u5b83\u6709\u4ee5\u4e0b\u51e0\u79cd\u521b\u5efa\u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"language-sql\">CREATE INDEX indexName ON mytable(username(length));<\/code><\/pre>\n<p>\u5982\u679c\u662f<code>CHAR<\/code>\uff0c<code>VARCHAR<\/code>\u7c7b\u578b\uff0clength\u53ef\u4ee5\u5c0f\u4e8e\u5b57\u6bb5\u5b9e\u9645\u957f\u5ea6\uff1b\u5982\u679c\u662f<code>BLOB<\/code>\u548c<code>TEXT<\/code>\u7c7b\u578b\uff0c\u5fc5\u987b\u6307\u5b9a<code>length<\/code><\/p>\n<h3>\u4fee\u6539\u8868\u7ed3\u6784(\u6dfb\u52a0\u7d22\u5f15)<\/h3>\n<pre><code class=\"language-sql\">ALTER table tableName ADD INDEX indexName(columnName);<\/code><\/pre>\n<h3>\u521b\u5efa\u8868\u65f6\u76f4\u63a5\u6307\u5b9a<\/h3>\n<pre><code class=\"language-sql\">CREATE TABLE mytable(\n  ID INT NOT NULL,\n  username VARCHAR(16) NOT NULL,\n  INDEX [indexName] (username(length))\n);<\/code><\/pre>\n<h3>\u5220\u9664\u7d22\u5f15\u7684\u8bed\u6cd5<\/h3>\n<pre><code class=\"language-sql\">DROP INDEX [indexName] ON mytable;<\/code><\/pre>\n<h2>\u552f\u4e00\u7d22\u5f15<\/h2>\n<p>\u5b83\u4e0e\u524d\u9762\u7684\u666e\u901a\u7d22\u5f15\u7c7b\u4f3c\uff0c\u4e0d\u540c\u7684\u5c31\u662f\uff1a\u7d22\u5f15\u5217\u7684\u503c\u5fc5\u987b\u552f\u4e00\uff0c\u4f46\u5141\u8bb8\u6709\u7a7a\u503c\u3002\u5982\u679c\u662f\u7ec4\u5408\u7d22\u5f15\uff0c\u5219\u5217\u503c\u7684\u7ec4\u5408\u5fc5\u987b\u552f\u4e00\u3002\u5b83\u6709\u4ee5\u4e0b\u51e0\u79cd\u521b\u5efa\u65b9\u5f0f\uff1a<\/p>\n<h3>\u521b\u5efa\u7d22\u5f15<\/h3>\n<pre><code class=\"language-sql\">CREATE UNIQUE INDEX indexName ON mytable(username(length));<\/code><\/pre>\n<h3>\u4fee\u6539\u8868\u7ed3\u6784<\/h3>\n<pre><code class=\"language-sql\">ALTER table mytable ADD UNIQUE [indexName] (username(length));<\/code><\/pre>\n<h3>\u521b\u5efa\u8868\u7684\u65f6\u5019\u76f4\u63a5\u6307\u5b9a<\/h3>\n<pre><code class=\"language-sql\">CREATE TABLE mytable(\n  ID INT NOT NULL,\n  username VARCHAR(16) NOT NULL,\n  UNIQUE [indexName] (username(length))\n); <\/code><\/pre>\n<h2>\u4f7f\u7528ALTER\u547d\u4ee4\u6dfb\u52a0\u548c\u5220\u9664\u7d22\u5f15<\/h2>\n<p>\u6709\u56db\u79cd\u65b9\u5f0f\u6765\u6dfb\u52a0\u6570\u636e\u8868\u7684\u7d22\u5f15\uff1a<\/p>\n<ul>\n<li><code>ALTER TABLE tbl_name ADD PRIMARY KEY (column_list)<\/code>: \u6dfb\u52a0\u4e00\u4e2a\u4e3b\u952e\uff0c\u8fd9\u610f\u5473\u7740\u7d22\u5f15\u503c\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u4e14\u4e0d\u80fd\u4e3aNULL<\/li>\n<li><code>ALTER TABLE tbl_name ADD UNIQUE index_name (column_list)<\/code>: \u521b\u5efa\u7d22\u5f15\u7684\u503c\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff08\u9664\u4e86NULL\u5916\uff0cNULL\u53ef\u80fd\u4f1a\u51fa\u73b0\u591a\u6b21\uff09<\/li>\n<li><code>ALTER TABLE tbl_name ADD INDEX index_name (column_list)<\/code>: \u6dfb\u52a0\u666e\u901a\u7d22\u5f15\uff0c\u7d22\u5f15\u503c\u53ef\u51fa\u73b0\u591a\u6b21<\/li>\n<li><code>ALTER TABLE tbl_name ADD FULLTEXT index_name (column_list)<\/code>: \u6307\u5b9a\u7d22\u5f15\u4e3a<code>FULLTEXT<\/code>\uff0c\u7528\u4e8e\u5168\u6587\u7d22\u5f15<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u4e3a\u5728\u8868\u4e2d\u6dfb\u52a0\u7d22\u5f15\u3002<\/p>\n<pre><code class=\"language-sql\">mysql&gt; ALTER TABLE testalter_tbl ADD INDEX (c);<\/code><\/pre>\n<p>\u8fd8\u53ef\u4ee5\u5728 ALTER \u547d\u4ee4\u4e2d\u4f7f\u7528 DROP \u5b50\u53e5\u6765\u5220\u9664\u7d22\u5f15\u3002\u5c1d\u8bd5\u4ee5\u4e0b\u5b9e\u4f8b\u5220\u9664\u7d22\u5f15\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; ALTER TABLE testalter_tbl DROP INDEX c;<\/code><\/pre>\n<h2>\u4f7f\u7528 ALTER \u547d\u4ee4\u6dfb\u52a0\u548c\u5220\u9664\u4e3b\u952e<\/h2>\n<p>\u4e3b\u952e\u53ea\u80fd\u4f5c\u7528\u4e8e\u4e00\u4e2a\u5217\u4e0a\uff0c\u6dfb\u52a0\u4e3b\u952e\u7d22\u5f15\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u8be5\u4e3b\u952e\u9ed8\u8ba4\u4e0d\u4e3a\u7a7a\uff08<code>NOT NULL<\/code>\uff09\u3002\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; ALTER TABLE testalter_tbl MODIFY i INT NOT NULL;\nmysql&gt; ALTER TABLE testalter_tbl ADD PRIMARY KEY (i);<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>ALTER<\/code>\u547d\u4ee4\u5220\u9664\u4e3b\u952e\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; ALTER TABLE testalter_tbl DROP PRIMARY KEY;<\/code><\/pre>\n<p>\u5220\u9664\u4e3b\u952e\u65f6\u53ea\u9700\u6307\u5b9aPRIMARY KEY\uff0c\u4f46\u5728\u5220\u9664\u7d22\u5f15\u65f6\uff0c\u5fc5\u987b\u6307\u5b9a\u7d22\u5f15\u540d<\/p>\n<h2>\u663e\u793a\u7d22\u5f15\u4fe1\u606f<\/h2>\n<p>\u53ef\u4ee5\u4f7f\u7528<code>SHOW INDEX<\/code>\u547d\u4ee4\u5217\u51fa\u8868\u4e2d\u7684\u76f8\u5173\u7684\u7d22\u5f15\u4fe1\u606f\u3002\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0<code>\\G<\/code>\u6765\u683c\u5f0f\u5316\u8f93\u51fa\u4fe1\u606f\u3002<\/p>\n<p>\u5c1d\u8bd5\u4ee5\u4e0b\u5b9e\u4f8b:<\/p>\n<pre><code class=\"language-sql\">mysql&gt; SHOW INDEX FROM table_name; \\G\n........<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>MySQL\u7d22\u5f15\u7684\u5efa\u7acb\u5bf9\u4e8eMySQL\u7684\u9ad8\u6548\u8fd0\u884c\u662f\u5f88\u91cd\u8981\u7684\uff0c\u7d22\u5f15\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8MySQL\u7684\u68c0\u7d22\u901f\u5ea6\u3002 \u4ee5\u6c49\u8bed\u5b57\u5178\u7684\u76ee [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-242","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=242"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/242\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}