{"id":76728,"date":"2024-10-16T09:20:16","date_gmt":"2024-10-16T01:20:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/76728\/"},"modified":"2024-10-16T09:20:16","modified_gmt":"2024-10-16T01:20:16","slug":"mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e9%97%9c%e6%96%bcenum%e9%a1%9e%e5%9e%8b%e7%9a%84%e8%a9%b3%e7%b4%b0%e8%a7%a3%e9%87%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/76728\/","title":{"rendered":"MySQL\u6578\u64da\u5eab\u4e2d\u95dc\u65bcENUM\u985e\u578b\u7684\u8a73\u7d30\u89e3\u91cb"},"content":{"rendered":"<h1 id=\"mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e9%97%9c%e6%96%bcenum%e9%a1%9e%e5%9e%8b%e7%9a%84%e8%a9%b3%e7%b4%b0%e8%a7%a3%e9%87%8b-cDHUUPrYob\">MySQL\u6578\u64da\u5eab\u4e2d\u95dc\u65bcENUM\u985e\u578b\u7684\u8a73\u7d30\u89e3\u91cb<\/h1>\n<p>\u5728MySQL\u6578\u64da\u5eab\u4e2d\uff0cENUM\u662f\u4e00\u7a2e\u7279\u6b8a\u7684\u6578\u64da\u985e\u578b\uff0c\u7528\u65bc\u5b58\u5132\u4e00\u7d44\u9810\u5b9a\u7fa9\u7684\u5b57\u7b26\u4e32\u503c\u3002\u9019\u7a2e\u6578\u64da\u985e\u578b\u7684\u4e3b\u8981\u512a\u52e2\u5728\u65bc\u5b83\u80fd\u5920\u9650\u5236\u5217\u4e2d\u7684\u503c\uff0c\u4f7f\u5176\u53ea\u80fd\u662f\u6307\u5b9a\u7684\u9078\u9805\u4e4b\u4e00\u3002\u9019\u5c0d\u65bc\u9700\u8981\u78ba\u4fdd\u6578\u64da\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u7684\u61c9\u7528\u5834\u666f\u7279\u5225\u6709\u7528\u3002<\/p>\n<h2 id=\"enum%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-cDHUUPrYob\">ENUM\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>ENUM\u985e\u578b\u7684\u5b9a\u7fa9\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>CREATE TABLE example (\n    status ENUM('active', 'inactive', 'pending')\n);\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0cstatus\u5217\u53ea\u80fd\u63a5\u53d7\u4e09\u500b\u503c\uff1a&#8217;active&#8217;\u3001&#8217;inactive&#8217;\u548c&#8217;pending&#8217;\u3002\u5982\u679c\u5617\u8a66\u63d2\u5165\u5176\u4ed6\u503c\uff0cMySQL\u5c07\u8fd4\u56de\u932f\u8aa4\u3002<\/p>\n<h2 id=\"enum%e7%9a%84%e7%89%b9%e6%80%a7-cDHUUPrYob\">ENUM\u7684\u7279\u6027<\/h2>\n<ul>\n<li><strong>\u5b58\u5132\u6548\u7387\uff1a<\/strong>ENUM\u985e\u578b\u5728\u5b58\u5132\u6642\u4f7f\u7528\u4e00\u500b\u6574\u6578\u4f86\u8868\u793a\u6bcf\u500b\u5b57\u7b26\u4e32\u503c\uff0c\u9019\u4f7f\u5f97\u5b83\u6bd4VARCHAR\u985e\u578b\u66f4\u7bc0\u7701\u7a7a\u9593\u3002<\/li>\n<li><strong>\u6392\u5e8f\u529f\u80fd\uff1a<\/strong>ENUM\u985e\u578b\u7684\u503c\u5177\u6709\u5167\u5efa\u7684\u6392\u5e8f\u529f\u80fd\uff0c\u6839\u64da\u5b9a\u7fa9\u7684\u9806\u5e8f\u9032\u884c\u6392\u5e8f\u3002<\/li>\n<li><strong>\u9ed8\u8a8d\u503c\uff1a<\/strong>\u53ef\u4ee5\u70baENUM\u5217\u8a2d\u7f6e\u9ed8\u8a8d\u503c\uff0c\u9019\u6a23\u5728\u63d2\u5165\u6578\u64da\u6642\u5982\u679c\u672a\u6307\u5b9a\u8a72\u5217\u7684\u503c\uff0c\u5c07\u81ea\u52d5\u4f7f\u7528\u9ed8\u8a8d\u503c\u3002<\/li>\n<\/ul>\n<h2 id=\"enum%e7%9a%84%e4%bd%bf%e7%94%a8%e5%a0%b4%e6%99%af-cDHUUPrYob\">ENUM\u7684\u4f7f\u7528\u5834\u666f<\/h2>\n<p>ENUM\u985e\u578b\u9069\u5408\u7528\u65bc\u90a3\u4e9b\u503c\u7684\u7bc4\u570d\u6709\u9650\u4e14\u4e0d\u6703\u7d93\u5e38\u8b8a\u52d5\u7684\u60c5\u6cc1\u3002\u4f8b\u5982\uff0c\u72c0\u614b\u6a19\u8a18\u3001\u6027\u5225\u3001\u985e\u5225\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5177\u9ad4\u7684\u4f7f\u7528\u5834\u666f\uff1a<\/p>\n<ul>\n<li>\u7528\u6236\u72c0\u614b\uff1a\u5982&#8217;active&#8217;\u3001&#8217;inactive&#8217;\u3001&#8217;banned&#8217;\u3002<\/li>\n<li>\u8a02\u55ae\u72c0\u614b\uff1a\u5982&#8217;pending&#8217;\u3001&#8217;shipped&#8217;\u3001&#8217;delivered&#8217;\u3002<\/li>\n<li>\u7522\u54c1\u985e\u5225\uff1a\u5982&#8217;electronics&#8217;\u3001&#8217;clothing&#8217;\u3001&#8217;furniture&#8217;\u3002<\/li>\n<\/ul>\n<h2 id=\"enum%e7%9a%84%e5%84%aa%e7%bc%ba%e9%bb%9e-cDHUUPrYob\">ENUM\u7684\u512a\u7f3a\u9ede<\/h2>\n<h3 id=\"%e5%84%aa%e9%bb%9e-cDHUUPrYob\">\u512a\u9ede<\/h3>\n<ul>\n<li>\u6578\u64da\u5b8c\u6574\u6027\uff1aENUM\u9650\u5236\u4e86\u53ef\u63a5\u53d7\u7684\u503c\uff0c\u6e1b\u5c11\u4e86\u6578\u64da\u932f\u8aa4\u7684\u53ef\u80fd\u6027\u3002<\/li>\n<li>\u5b58\u5132\u6548\u7387\uff1a\u76f8\u8f03\u65bcVARCHAR\uff0cENUM\u5728\u5b58\u5132\u4e0a\u66f4\u70ba\u9ad8\u6548\u3002<\/li>\n<li>\u6613\u65bc\u7dad\u8b77\uff1a\u7576\u9700\u8981\u6dfb\u52a0\u65b0\u503c\u6642\uff0c\u53ea\u9700\u4fee\u6539\u8868\u7d50\u69cb\u5373\u53ef\u3002<\/li>\n<\/ul>\n<h3 id=\"%e7%bc%ba%e9%bb%9e-cDHUUPrYob\">\u7f3a\u9ede<\/h3>\n<ul>\n<li>\u9748\u6d3b\u6027\u4e0d\u8db3\uff1a\u5982\u679c\u9700\u8981\u983b\u7e41\u66f4\u6539ENUM\u7684\u503c\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u7dad\u8b77\u56f0\u96e3\u3002<\/li>\n<li>\u4e0d\u652f\u6301\u591a\u8a9e\u8a00\uff1aENUM\u7684\u503c\u662f\u56fa\u5b9a\u7684\uff0c\u5c0d\u65bc\u9700\u8981\u591a\u8a9e\u8a00\u652f\u6301\u7684\u61c9\u7528\u4e0d\u592a\u9069\u5408\u3002<\/li>\n<li>\u6578\u64da\u5eab\u9077\u79fb\u56f0\u96e3\uff1a\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u5c07ENUM\u985e\u578b\u8f49\u63db\u70ba\u5176\u4ed6\u6578\u64da\u985e\u578b\u53ef\u80fd\u6703\u6bd4\u8f03\u9ebb\u7169\u3002<\/li>\n<\/ul>\n<h2 id=\"enum%e7%9a%84%e6%93%8d%e4%bd%9c%e7%a4%ba%e4%be%8b-cDHUUPrYob\">ENUM\u7684\u64cd\u4f5c\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684ENUM\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-cDHUUPrYob\">\u63d2\u5165\u6578\u64da<\/h3>\n<pre><code>INSERT INTO example (status) VALUES ('active');\n<\/code><\/pre>\n<h3 id=\"%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-cDHUUPrYob\">\u67e5\u8a62\u6578\u64da<\/h3>\n<pre><code>SELECT * FROM example WHERE status = 'inactive';\n<\/code><\/pre>\n<h3 id=\"%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-cDHUUPrYob\">\u66f4\u65b0\u6578\u64da<\/h3>\n<pre><code>UPDATE example SET status = 'pending' WHERE status = 'active';\n<\/code><\/pre>\n<h3 id=\"%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a-cDHUUPrYob\">\u522a\u9664\u6578\u64da<\/h3>\n<pre><code>DELETE FROM example WHERE status = 'banned';\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-cDHUUPrYob\">\u7e3d\u7d50<\/h2>\n<p>ENUM\u985e\u578b\u5728MySQL\u4e2d\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u6578\u64da\u985e\u578b\uff0c\u7279\u5225\u9069\u5408\u7528\u65bc\u9700\u8981\u9650\u5236\u503c\u7bc4\u570d\u7684\u5834\u666f\u3002\u96d6\u7136\u5b83\u6709\u4e00\u4e9b\u7f3a\u9ede\uff0c\u4f46\u5728\u9069\u7576\u7684\u60c5\u6cc1\u4e0b\uff0cENUM\u53ef\u4ee5\u63d0\u9ad8\u6578\u64da\u7684\u5b8c\u6574\u6027\u548c\u5b58\u5132\u6548\u7387\u3002\u5c0d\u65bc\u9700\u8981\u7a69\u5b9a\u548c\u4e00\u81f4\u6578\u64da\u7684\u61c9\u7528\uff0cENUM\u662f\u4e00\u500b\u503c\u5f97\u8003\u616e\u7684\u9078\u64c7\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u6258\u7ba1\u60a8\u7684MySQL\u6578\u64da\u5eab\uff0c\u78ba\u4fdd\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u904b\u884c\u6d41\u66a2\u4e14\u7a69\u5b9a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u63a2\u8a0eMySQL\u6578\u64da\u5eab\u4e2d\u7684ENUM\u985e\u578b\uff0c\u4e86\u89e3\u5176\u5b9a\u7fa9\u3001\u7528\u6cd5\u53ca\u512a\u7f3a\u9ede\uff0c\u52a9\u60a8\u66f4\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\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-76728","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76728","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=76728"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76728\/revisions"}],"predecessor-version":[{"id":76729,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76728\/revisions\/76729"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=76728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=76728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=76728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}