{"id":77776,"date":"2024-10-16T11:10:19","date_gmt":"2024-10-16T03:10:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/77776\/"},"modified":"2024-10-16T11:10:19","modified_gmt":"2024-10-16T03:10:19","slug":"mysql%e6%8c%87%e4%bb%a4-bool%ef%bc%88%e5%b8%83%e7%88%be%e5%9e%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/77776\/","title":{"rendered":"MySQL\u6307\u4ee4: BOOL\uff08\u5e03\u723e\u578b\uff09"},"content":{"rendered":"<h1 id=\"mysql%e6%8c%87%e4%bb%a4-bool%ef%bc%88%e5%b8%83%e7%88%be%e5%9e%8b%ef%bc%89-JqrwRxstHN\">MySQL\u6307\u4ee4: BOOL\uff08\u5e03\u723e\u578b\uff09<\/h1>\n<p>\u5728\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0c\u6578\u64da\u985e\u578b\u7684\u9078\u64c7\u5c0d\u65bc\u6578\u64da\u7684\u5b58\u5132\u548c\u67e5\u8a62\u6548\u7387\u81f3\u95dc\u91cd\u8981\u3002MySQL\u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684\u958b\u6e90\u8cc7\u6599\u5eab\u7cfb\u7d71\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u6578\u64da\u985e\u578b\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u5e03\u723e\u578b\uff08BOOL\uff09\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eMySQL\u4e2d\u7684\u5e03\u723e\u578b\u6578\u64da\u985e\u578b\uff0c\u5305\u62ec\u5176\u5b9a\u7fa9\u3001\u4f7f\u7528\u65b9\u6cd5\u53ca\u5be6\u969b\u61c9\u7528\u7bc4\u4f8b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e5%b8%83%e7%88%be%e5%9e%8b%ef%bc%88bool%ef%bc%89-JqrwRxstHN\">\u4ec0\u9ebc\u662f\u5e03\u723e\u578b\uff08BOOL\uff09<\/h2>\n<p>\u5e03\u723e\u578b\uff08BOOL\uff09\u662f\u4e00\u7a2e\u7528\u65bc\u8868\u793a\u771f\uff08TRUE\uff09\u6216\u5047\uff08FALSE\uff09\u5169\u7a2e\u72c0\u614b\u7684\u6578\u64da\u985e\u578b\u3002\u5728MySQL\u4e2d\uff0c\u5e03\u723e\u578b\u5be6\u969b\u4e0a\u662fTINYINT(1)\u7684\u5225\u540d\uff0c\u9019\u610f\u5473\u8457\u5b83\u53ef\u4ee5\u5b58\u51320\u62161\uff0c\u5176\u4e2d0\u4ee3\u8868\u5047\uff08FALSE\uff09\uff0c1\u4ee3\u8868\u771f\uff08TRUE\uff09\u3002\u9019\u7a2e\u6578\u64da\u985e\u578b\u5728\u9700\u8981\u9032\u884c\u908f\u8f2f\u5224\u65b7\u7684\u60c5\u6cc1\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u7528\u65bc\u6a19\u8a18\u67d0\u500b\u689d\u4ef6\u662f\u5426\u6210\u7acb\u3002<\/p>\n<h2 id=\"%e5%b8%83%e7%88%be%e5%9e%8b%e7%9a%84%e5%ae%9a%e7%be%a9%e8%88%87%e4%bd%bf%e7%94%a8-JqrwRxstHN\">\u5e03\u723e\u578b\u7684\u5b9a\u7fa9\u8207\u4f7f\u7528<\/h2>\n<p>\u5728MySQL\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5728\u5275\u5efa\u8868\u6642\u5b9a\u7fa9\u5e03\u723e\u578b\u5b57\u6bb5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u5275\u5efa\u8868\u6642\u4f7f\u7528\u5e03\u723e\u578b\uff1a<\/p>\n<pre><code>CREATE TABLE users (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    username VARCHAR(50) NOT NULL,\n    is_active BOOL NOT NULL DEFAULT TRUE\n);<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u540d\u70bausers\u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u500b\u5e03\u723e\u578b\u5b57\u6bb5is_active\uff0c\u7528\u65bc\u6a19\u8a18\u7528\u6236\u662f\u5426\u6d3b\u8e8d\u3002\u9810\u8a2d\u503c\u8a2d\u70baTRUE\uff0c\u8868\u793a\u65b0\u7528\u6236\u9ed8\u8a8d\u70ba\u6d3b\u8e8d\u72c0\u614b\u3002<\/p>\n<h2 id=\"%e6%8f%92%e5%85%a5%e8%88%87%e6%9f%a5%e8%a9%a2%e5%b8%83%e7%88%be%e5%9e%8b%e6%95%b8%e6%93%9a-JqrwRxstHN\">\u63d2\u5165\u8207\u67e5\u8a62\u5e03\u723e\u578b\u6578\u64da<\/h2>\n<p>\u63d2\u5165\u5e03\u723e\u578b\u6578\u64da\u6642\uff0c\u53ef\u4ee5\u4f7f\u75281\u62160\u4f86\u8868\u793a\u771f\u6216\u5047\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>INSERT INTO users (username, is_active) VALUES ('Alice', TRUE);\nINSERT INTO users (username, is_active) VALUES ('Bob', FALSE);<\/code><\/pre>\n<p>\u67e5\u8a62\u5e03\u723e\u578b\u6578\u64da\u6642\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c21\u55ae\u7684SELECT\u8a9e\u53e5\u3002\u4f8b\u5982\uff0c\u67e5\u8a62\u6240\u6709\u6d3b\u8e8d\u7528\u6236\uff1a<\/p>\n<pre><code>SELECT * FROM users WHERE is_active = TRUE;<\/code><\/pre>\n<h2 id=\"%e5%b8%83%e7%88%be%e5%9e%8b%e7%9a%84%e6%87%89%e7%94%a8%e5%a0%b4%e6%99%af-JqrwRxstHN\">\u5e03\u723e\u578b\u7684\u61c9\u7528\u5834\u666f<\/h2>\n<p>\u5e03\u723e\u578b\u5728\u8a31\u591a\u61c9\u7528\u5834\u666f\u4e2d\u90fd\u975e\u5e38\u6709\u7528\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u4f8b\u5b50\uff1a<\/p>\n<ul>\n<li><strong>\u7528\u6236\u72c0\u614b\u7ba1\u7406\uff1a<\/strong>\u7528\u65bc\u6a19\u8a18\u7528\u6236\u662f\u5426\u6d3b\u8e8d\u3001\u662f\u5426\u5df2\u9a57\u8b49\u7b49\u3002<\/li>\n<li><strong>\u529f\u80fd\u958b\u95dc\uff1a<\/strong>\u7528\u65bc\u63a7\u5236\u67d0\u4e9b\u529f\u80fd\u662f\u5426\u555f\u7528\uff0c\u4f8b\u5982\u662f\u5426\u986f\u793a\u5ee3\u544a\u3002<\/li>\n<li><strong>\u6b0a\u9650\u63a7\u5236\uff1a<\/strong>\u7528\u65bc\u6a19\u8a18\u7528\u6236\u662f\u5426\u64c1\u6709\u67d0\u4e9b\u6b0a\u9650\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-JqrwRxstHN\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4f7f\u7528\u5e03\u723e\u578b\u6642\uff0c\u6709\u5e7e\u9ede\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>\u5e03\u723e\u578b\u5728MySQL\u4e2d\u5be6\u969b\u4e0a\u662fTINYINT(1)\uff0c\u56e0\u6b64\u53ef\u4ee5\u5b58\u5132\u7684\u503c\u7bc4\u570d\u662f-128\u5230127\uff0c\u4f46\u901a\u5e38\u53ea\u4f7f\u75280\u548c1\u3002<\/li>\n<li>\u5728\u67e5\u8a62\u6642\uff0c\u4f7f\u7528TRUE\u548cFALSE\u6703\u81ea\u52d5\u8f49\u63db\u70ba1\u548c0\uff0c\u9019\u4f7f\u5f97\u67e5\u8a62\u66f4\u52a0\u76f4\u89c0\u3002<\/li>\n<li>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u4f7f\u7528ENUM\u6216SET\u985e\u578b\u53ef\u80fd\u6703\u66f4\u9069\u5408\uff0c\u7279\u5225\u662f\u7576\u9700\u8981\u8868\u793a\u591a\u500b\u72c0\u614b\u6642\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-JqrwRxstHN\">\u7e3d\u7d50<\/h2>\n<p>\u5e03\u723e\u578b\uff08BOOL\uff09\u5728MySQL\u4e2d\u662f\u4e00\u500b\u7c21\u55ae\u800c\u6709\u6548\u7684\u6578\u64da\u985e\u578b\uff0c\u9069\u5408\u7528\u65bc\u8868\u793a\u4e8c\u5143\u72c0\u614b\u3002\u7121\u8ad6\u662f\u5728\u7528\u6236\u7ba1\u7406\u3001\u529f\u80fd\u958b\u95dc\u9084\u662f\u6b0a\u9650\u63a7\u5236\u65b9\u9762\uff0c\u5e03\u723e\u578b\u90fd\u80fd\u63d0\u4f9b\u6e05\u6670\u7684\u908f\u8f2f\u5224\u65b7\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u5b58\u5132\u548c\u67e5\u8a62\u7684\u61c9\u7528\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u985e\u578b\u81f3\u95dc\u91cd\u8981\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u642d\u5efa\u60a8\u7684MySQL\u8cc7\u6599\u5eab\uff0c\u4eab\u53d7\u7a69\u5b9a\u7684\u6027\u80fd\u548c\u9748\u6d3b\u7684\u914d\u7f6e\u9078\u64c7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MySQL\u4e2d\u7684BOOL\uff08\u5e03\u5c14\u578b\uff09\u6307\u4ee4\uff0c\u638c\u63e1\u5176\u7528\u6cd5\u548c\u5e94\u7528\u573a\u666f\uff0c\u63d0\u5347\u6570\u636e\u5e93\u7ba1\u7406\u4e0e\u67e5\u8be2\u6548\u7387\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-77776","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/77776","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=77776"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/77776\/revisions"}],"predecessor-version":[{"id":77777,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/77776\/revisions\/77777"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=77776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=77776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=77776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}