{"id":160723,"date":"2024-11-04T00:28:31","date_gmt":"2024-11-03T16:28:31","guid":{"rendered":"https:\/\/server.hk\/cnblog\/160723\/"},"modified":"2024-11-04T00:28:31","modified_gmt":"2024-11-03T16:28:31","slug":"%e8%b3%87%e6%96%99%e5%ba%ab%e6%95%99%e7%a8%8b%ef%bc%9amysql-5-7%e7%9a%84%e6%96%b0%e7%89%b9%e6%80%a7%ef%bc%88%e6%96%b0%e5%8a%9f%e8%83%bd%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/160723\/","title":{"rendered":"\u8cc7\u6599\u5eab\u6559\u7a0b\uff1aMySQL 5.7\u7684\u65b0\u7279\u6027\uff08\u65b0\u529f\u80fd\uff09"},"content":{"rendered":"<h1 id=\"%e8%b3%87%e6%96%99%e5%ba%ab%e6%95%99%e7%a8%8b%ef%bc%9amysql-5-7%e7%9a%84%e6%96%b0%e7%89%b9%e6%80%a7%ef%bc%88%e6%96%b0%e5%8a%9f%e8%83%bd%ef%bc%89-TXGHcBgyyM\">\u8cc7\u6599\u5eab\u6559\u7a0b\uff1aMySQL 5.7\u7684\u65b0\u7279\u6027\uff08\u65b0\u529f\u80fd\uff09<\/h1>\n<p>MySQL \u662f\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u958b\u6e90\u95dc\u806f\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u96a8\u8457\u7248\u672c\u7684\u66f4\u65b0\uff0cMySQL \u4e0d\u65b7\u5f15\u5165\u65b0\u7684\u7279\u6027\u548c\u529f\u80fd\uff0c\u4ee5\u6eff\u8db3\u4e0d\u65b7\u8b8a\u5316\u7684\u9700\u6c42\u3002MySQL 5.7 \u662f\u4e00\u500b\u91cd\u8981\u7684\u7248\u672c\uff0c\u5e36\u4f86\u4e86\u8a31\u591a\u65b0\u7279\u6027\uff0c\u9019\u4e9b\u7279\u6027\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6027\u80fd\uff0c\u9084\u589e\u5f37\u4e86\u5b89\u5168\u6027\u548c\u53ef\u7528\u6027\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e MySQL 5.7 \u7684\u4e00\u4e9b\u4e3b\u8981\u65b0\u7279\u6027\u3002<\/p>\n<h2 id=\"1-json-%e6%94%af%e6%8f%b4-TXGHcBgyyM\">1. JSON \u652f\u63f4<\/h2>\n<p>MySQL 5.7 \u5f15\u5165\u4e86\u5c0d JSON \u6578\u64da\u985e\u578b\u7684\u539f\u751f\u652f\u63f4\uff0c\u9019\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8655\u7406\u975e\u7d50\u69cb\u5316\u6578\u64da\u3002\u4f7f\u7528 JSON \u6578\u64da\u985e\u578b\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5728\u8cc7\u6599\u5eab\u4e2d\u5b58\u5132\u548c\u67e5\u8a62 JSON \u683c\u5f0f\u7684\u6578\u64da\uff0c\u9019\u5c0d\u65bc\u9700\u8981\u9748\u6d3b\u6578\u64da\u7d50\u69cb\u7684\u61c9\u7528\u7a0b\u5e8f\u7279\u5225\u6709\u7528\u3002<\/p>\n<pre><code>CREATE TABLE users (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    name VARCHAR(100),\n    data JSON\n);\n\nINSERT INTO users (name, data) VALUES ('Alice', '{\"age\": 30, \"city\": \"Hong Kong\"}');\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0cMySQL 5.7 \u9084\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217 JSON \u51fd\u6578\uff0c\u4f8b\u5982 <code>JSON_EXTRACT()<\/code> \u548c <code>JSON_ARRAY()<\/code>\uff0c\u4f7f\u5f97\u5c0d JSON \u6578\u64da\u7684\u67e5\u8a62\u548c\u64cd\u4f5c\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u3002<\/p>\n<h2 id=\"2-%e8%99%9b%e6%93%ac%e5%88%97%ef%bc%88generated-columns%ef%bc%89-TXGHcBgyyM\">2. \u865b\u64ec\u5217\uff08Generated Columns\uff09<\/h2>\n<p>\u865b\u64ec\u5217\u662f\u4e00\u7a2e\u65b0\u7279\u6027\uff0c\u5141\u8a31\u7528\u6236\u5728\u8868\u4e2d\u5b9a\u7fa9\u57fa\u65bc\u5176\u4ed6\u5217\u8a08\u7b97\u5f97\u51fa\u7684\u5217\u3002\u9019\u4e9b\u5217\u4e0d\u6703\u4f54\u7528\u7269\u7406\u5b58\u5132\u7a7a\u9593\uff0c\u56e0\u70ba\u5b83\u5011\u7684\u503c\u662f\u52d5\u614b\u8a08\u7b97\u7684\u3002\u9019\u5c0d\u65bc\u9700\u8981\u7d93\u5e38\u8a08\u7b97\u884d\u751f\u6578\u64da\u7684\u60c5\u6cc1\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>CREATE TABLE products (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    price DECIMAL(10, 2),\n    tax_rate DECIMAL(5, 2),\n    total_price DECIMAL(10, 2) AS (price * (1 + tax_rate)) VIRTUAL\n);\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c<code>total_price<\/code> \u5217\u662f\u865b\u64ec\u5217\uff0c\u6839\u64da <code>price<\/code> \u548c <code>tax_rate<\/code> \u81ea\u52d5\u8a08\u7b97\u3002<\/p>\n<h2 id=\"3-%e6%94%b9%e9%80%b2%e7%9a%84%e6%80%a7%e8%83%bd-TXGHcBgyyM\">3. \u6539\u9032\u7684\u6027\u80fd<\/h2>\n<p>MySQL 5.7 \u5728\u6027\u80fd\u65b9\u9762\u4e5f\u6709\u986f\u8457\u7684\u6539\u9032\u3002\u9019\u500b\u7248\u672c\u5f15\u5165\u4e86\u66f4\u9ad8\u6548\u7684\u67e5\u8a62\u512a\u5316\u5668\uff0c\u80fd\u5920\u66f4\u597d\u5730\u8655\u7406\u8907\u96dc\u67e5\u8a62\u3002\u6b64\u5916\uff0cInnoDB \u5b58\u5132\u5f15\u64ce\u7684\u6027\u80fd\u4e5f\u5f97\u5230\u4e86\u63d0\u5347\uff0c\u7279\u5225\u662f\u5728\u9ad8\u4f75\u767c\u74b0\u5883\u4e0b\u3002<\/p>\n<p>\u4f8b\u5982\uff0cMySQL 5.7 \u652f\u63f4\u66f4\u9ad8\u6548\u7684 <code>GROUP BY<\/code> \u548c <code>ORDER BY<\/code> \u64cd\u4f5c\uff0c\u9019\u4f7f\u5f97\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\uff0c\u67e5\u8a62\u901f\u5ea6\u66f4\u5feb\u3002<\/p>\n<h2 id=\"4-%e5%ae%89%e5%85%a8%e6%80%a7%e5%a2%9e%e5%bc%b7-TXGHcBgyyM\">4. \u5b89\u5168\u6027\u589e\u5f37<\/h2>\n<p>\u5b89\u5168\u6027\u662f MySQL 5.7 \u7684\u53e6\u4e00\u500b\u91cd\u9ede\u3002\u9019\u500b\u7248\u672c\u5f15\u5165\u4e86\u66f4\u5f37\u7684\u5bc6\u78bc\u7b56\u7565\uff0c\u5141\u8a31\u7528\u6236\u8a2d\u7f6e\u66f4\u8907\u96dc\u7684\u5bc6\u78bc\u8981\u6c42\u3002\u6b64\u5916\uff0cMySQL 5.7 \u9084\u589e\u5f37\u4e86 SSL \u652f\u63f4\uff0c\u78ba\u4fdd\u6578\u64da\u5728\u50b3\u8f38\u904e\u7a0b\u4e2d\u7684\u5b89\u5168\u6027\u3002<\/p>\n<h2 id=\"5-%e6%9b%b4%e5%a5%bd%e7%9a%84%e9%8c%af%e8%aa%a4%e6%97%a5%e8%aa%8c-TXGHcBgyyM\">5. \u66f4\u597d\u7684\u932f\u8aa4\u65e5\u8a8c<\/h2>\n<p>MySQL 5.7 \u6539\u9032\u4e86\u932f\u8aa4\u65e5\u8a8c\u7684\u529f\u80fd\uff0c\u63d0\u4f9b\u4e86\u66f4\u8a73\u7d30\u7684\u932f\u8aa4\u4fe1\u606f\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u5feb\u5730\u5b9a\u4f4d\u548c\u89e3\u6c7a\u554f\u984c\u3002\u9019\u5c0d\u65bc\u7dad\u8b77\u548c\u7ba1\u7406\u8cc7\u6599\u5eab\u7cfb\u7d71\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-TXGHcBgyyM\">\u7e3d\u7d50<\/h2>\n<p>MySQL 5.7 \u7684\u65b0\u7279\u6027\u4f7f\u5176\u6210\u70ba\u4e00\u500b\u66f4\u5f37\u5927\u3001\u66f4\u9748\u6d3b\u7684\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u3002\u5f9e JSON \u652f\u63f4\u5230\u865b\u64ec\u5217\uff0c\u518d\u5230\u6027\u80fd\u548c\u5b89\u5168\u6027\u7684\u589e\u5f37\uff0c\u9019\u4e9b\u529f\u80fd\u90fd\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u9078\u64c7\u548c\u4fbf\u5229\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u80fd\u548c\u5b89\u5168\u6027\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u6216 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u4f86\u904b\u884c MySQL 5.7 \u5c07\u662f\u660e\u667a\u7684\u9078\u64c7\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u4f01\u696d\u9084\u662f\u5927\u578b\u4f01\u696d\uff0cMySQL 5.7 \u90fd\u80fd\u5920\u6eff\u8db3\u5176\u8cc7\u6599\u5eab\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22MySQL 5.7\u7684\u65b0\u7279\u6027\uff0c\u4e86\u89e3\u5176\u529f\u80fd\u63d0\u5347\u3001\u6027\u80fd\u6539\u9032\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u52a9\u60a8\u512a\u5316\u8cc7\u6599\u5eab\u7ba1\u7406\u8207\u958b\u767c\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-160723","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160723","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=160723"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160723\/revisions"}],"predecessor-version":[{"id":160724,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160723\/revisions\/160724"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=160723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=160723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=160723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}