{"id":108863,"date":"2024-11-05T14:43:10","date_gmt":"2024-11-05T06:43:10","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=108863"},"modified":"2024-11-05T14:43:10","modified_gmt":"2024-11-05T06:43:10","slug":"mysql-save","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mysql-save.html","title":{"rendered":"\u91cd\u8981\u7684MySQL \u6587\u6863\u5b58\u50a8\u77e5\u8bc6\u70b9\u626b\u76f2"},"content":{"rendered":"
MySQL \u6587\u6863\u5b58\u50a8 \u53ef\u4ee5\u8df3\u8fc7\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u521b\u5efa\u3001\u6570\u636e\u89c4\u8303\u5316\u548c\u5176\u5b83\u4f7f\u7528\u4f20\u7edf\u6570\u636e\u5e93\u65f6\u9700\u8981\u505a\u7684\u5de5\u4f5c\uff0c\u76f4\u63a5\u5b58\u50a8\u6570\u636e\u3002<\/p>\n
MySQL \u53ef\u4ee5\u63d0\u4f9b NoSQL JSON \u6587\u6863\u5b58\u50a8Document Store \u4e86\uff0c\u8fd9\u6837\u5f00\u53d1\u8005\u4fdd\u5b58\u6570\u636e\u524d\u65e0\u9700 \u89c4\u8303\u5316normalize \u6570\u636e\u3001\u521b\u5efa\u6570\u636e\u5e93\uff0c\u4e5f\u65e0\u9700\u5728\u5f00\u53d1\u4e4b\u524d\u5c31\u5236\u5b9a\u597d\u6570\u636e\u6837\u5f0f\u3002\u4ece MySQL 5.7 \u7248\u672c\u548c MySQL 8.0 \u7248\u672c\u5f00\u59cb\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u8868\u7684\u4e00\u5217\u4e2d\u5b58\u50a8 JSON \u6587\u6863\u3002\u7531\u4e8e\u5f15\u5165 X DevAPI\uff0c\u4f60\u53ef\u4ee5\u4ece\u4f60\u7684\u4ee3\u7801\u4e2d\u79fb\u9664\u4ee4\u4eba\u4e0d\u723d\u7684\u7ed3\u6784\u5316\u67e5\u8be2\u5b57\u7b26\u4e32\uff0c\u6539\u4e3a\u4f7f\u7528\u652f\u6301\u73b0\u4ee3\u7f16\u7a0b\u8bbe\u8ba1\u7684 API \u8c03\u7528\u3002<\/p>\n
\u7cfb\u7edf\u5b66\u4e60\u8fc7\u7ed3\u6784\u5316\u67e5\u8be2\u8bed\u8a00\uff08SQL\uff09\u3001 \u5173\u7cfb\u7406\u8bbarelational theory\u3001\u96c6\u5408set \u548c\u5176\u5b83\u5173\u7cfb\u6570\u636e\u5e93\u5e95\u5c42\u7406\u8bba\u7684\u5f00\u53d1\u8005\u5e76\u4e0d\u591a\uff0c\u4f46\u4ed6\u4eec\u9700\u8981\u4e00\u4e2a\u5b89\u5168\u53ef\u9760\u7684\u6570\u636e\u5b58\u50a8\u3002\u5982\u679c\u6570\u636e\u5e93\u7ba1\u7406\u4eba\u5458\u4e0d\u8db3\uff0c\u4e8b\u60c5\u5f88\u5feb\u5c31\u4f1a\u53d8\u5f97\u4e00\u56e2\u7cdf\uff0c<\/p>\n
MySQL \u6587\u6863\u5b58\u50a8 \u5141\u8bb8\u5f00\u53d1\u8005\u8df3\u8fc7\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u521b\u5efa\u3001\u6570\u636e\u89c4\u8303\u5316\u548c\u5176\u5b83\u4f7f\u7528\u4f20\u7edf\u6570\u636e\u5e93\u65f6\u9700\u8981\u505a\u7684\u5de5\u4f5c\uff0c\u76f4\u63a5\u5b58\u50a8\u6570\u636e\u3002\u53ea\u9700\u521b\u5efa\u4e00\u4e2a JSON \u6587\u6863\u96c6\u5408document collection\uff0c\u63a5\u7740\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86\u3002<\/p>\n
\u6240\u6709\u8fd9\u4e00\u5207\u90fd\u57fa\u4e8e\u591a\u5e74\u524d MySQL 5.7 \u5f15\u5165\u7684 JSON \u6570\u636e\u7c7b\u578b\u3002\u5b83\u5141\u8bb8\u5728\u8868\u7684\u4e00\u884c\u4e2d\u63d0\u4f9b\u5927\u7ea6 1GB \u5927\u5c0f\u7684\u5217\u3002\u6570\u636e\u5fc5\u987b\u662f\u6709\u6548\u7684 JSON\uff0c\u5426\u5219\u670d\u52a1\u5668\u4f1a\u62a5\u9519\uff1b\u4f46\u5f00\u53d1\u8005\u53ef\u4ee5\u81ea\u7531\u4f7f\u7528\u8fd9\u4e9b\u7a7a\u95f4\u3002<\/p>\n
\u65e7\u7684 MySQL \u534f\u8bae\u5df2\u7ecf\u5386\u7ecf\u5dee\u4e0d\u591a\u56db\u5206\u4e4b\u4e00\u4e2a\u4e16\u7eaa\uff0c\u5df2\u7ecf\u663e\u73b0\u51fa\u75b2\u6001\uff0c\u56e0\u6b64\u65b0\u7684\u534f\u8bae\u88ab\u5f00\u53d1\u51fa\u6765\uff0c\u534f\u8bae\u540d\u4e3a X DevAPI\u3002\u534f\u8bae\u5f15\u5165\u9ad8\u7ea7\u4f1a\u8bdd\u6982\u5ff5\uff0c\u5141\u8bb8\u4ee3\u7801\u4ece\u5355\u53f0\u670d\u52a1\u5668\u6269\u5c55\u5230\u591a\u53f0\uff0c\u4f7f\u7528\u7b26\u5408 \u901a\u7528\u4e3b\u673a\u7f16\u7a0b\u8bed\u8a00\u6837\u5f0fcommon host-language programming patterns \u7684\u975e\u963b\u585e\u5f02\u6b65 I\/O\u3002\u9700\u8981\u5173\u6ce8\u7684\u662f\u5982\u4f55\u9075\u5faa\u73b0\u4ee3\u5b9e\u8df5\u548c\u7f16\u7801\u98ce\u683c\uff0c\u540c\u65f6\u4f7f\u7528 CRUD\uff08Create\u3001 Replace\u3001 Update\u3001 Delete\uff09\u6837\u5f0f\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4f60\u4e0d\u518d\u9700\u8981\u5728\u4f60\u7cbe\u7f8e\u3001\u7eaf\u6d01\u7684\u4ee3\u7801\u4e2d\u5d4c\u5165\u4e11\u964b\u7684 SQL \u8bed\u53e5\u5b57\u7b26\u4e32\u3002<\/p>\n
\u4e00\u4e2a\u65b0\u7684 shell \u652f\u6301\u8fd9\u79cd\u65b0\u534f\u8bae\uff0c\u5373\u6240\u8c13\u7684 MySQL Shell\u3002\u8be5 shell \u53ef\u7528\u4e8e\u8bbe\u7f6e \u9ad8\u53ef\u7528\u96c6\u7fa4high-availability cluster\u3001\u68c0\u67e5\u670d\u52a1\u5668 \u5347\u7ea7\u5c31\u7eea\u72b6\u6001upgrade readiness \u4ee5\u53ca\u4e0e MySQL \u670d\u52a1\u5668\u4ea4\u4e92\u3002\u652f\u6301\u7684\u4ea4\u4e92\u65b9\u5f0f\u6709\u4ee5\u4e0b\u4e09\u79cd\uff1aJavaScript\uff0cPython \u548c SQL\u3002<\/p>\n
\u4e0b\u9762\u7684\u4ee3\u7801\u793a\u4f8b\u57fa\u4e8e JavaScript \u65b9\u5f0f\u4f7f\u7528 MySQL Shell\uff0c\u53ef\u4ee5\u4ece JS> \u63d0\u793a\u7b26\u770b\u51fa\u3002<\/p>\n
\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u7528\u6237 dstokes \u3001\u5bc6\u7801 password \u767b\u5f55\u672c\u5730\u7cfb\u7edf\u4e0a\u7684 demo \u5e93\u3002db \u662f\u4e00\u4e2a\u6307\u9488\uff0c\u6307\u5411 demo \u5e93\u3002<\/p>\n
$ mysqlsh dstokes:password@localhost\/demo\r\nJS> db.createCollection(\"example\")\r\nJS> db.example.add(\r\n{\r\nName: \"Dave\",\r\nState: \u00a0\"Texas\",\r\nfoo : \"bar\"\r\n}\r\n)\r\nJS><\/pre>\n\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u767b\u5f55\u670d\u52a1\u5668\uff0c\u8fde\u63a5\u5230 demo \u5e93\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a example \u7684\u96c6\u5408\uff0c\u6700\u540e\u63d2\u5165\u4e00\u6761\u8bb0\u5f55\uff1b\u6574\u4e2a\u8fc7\u7a0b\u65e0\u9700\u521b\u5efa\u8868\uff0c\u4e5f\u65e0\u9700\u4f7f\u7528 SQL\u3002\u53ea\u8981\u4f60\u80fd\u60f3\u8c61\u7684\u5230\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u751a\u81f3\u6ee5\u7528\u8fd9\u4e9b\u6570\u636e\u3002\u8fd9\u4e0d\u662f\u4e00\u79cd\u4ee3\u7801\u5bf9\u8c61\u4e0e\u5173\u7cfb\u8bed\u53e5\u4e4b\u95f4\u7684\u6620\u5c04\u5668\uff0c\u56e0\u4e3a\u5e76\u6ca1\u6709\u5c06\u4ee3\u7801\u6620\u5c04\u4e3a SQL\uff1b\u65b0\u534f\u8bae\u76f4\u63a5\u4e0e\u670d\u52a1\u5668\u5c42\u6253\u4ea4\u9053\u3002<\/p>\n
Node.js \u652f\u6301<\/strong><\/div>\n\u65b0 shell \u770b\u8d77\u6765\u633a\u4e0d\u9519\uff0c\u4f60\u53ef\u4ee5\u7528\u5176\u5b8c\u6210\u5f88\u591a\u5de5\u4f5c\uff1b\u4f46\u4f60\u53ef\u80fd\u66f4\u5e0c\u671b\u4f7f\u7528\u4f60\u9009\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u4f7f\u7528 world_x \u793a\u4f8b\u6570\u636e\u5e93\uff0c\u641c\u7d22 _id \u5b57\u6bb5\u5339\u914d CAN. \u7684\u8bb0\u5f55\u3002\u6211\u4eec\u6307\u5b9a\u6570\u636e\u5e93\u4e2d\u7684\u7279\u5b9a\u96c6\u5408\uff0c\u4f7f\u7528\u7279\u5b9a\u53c2\u6570\u8c03\u7528 find\u547d\u4ee4\u3002\u540c\u6837\u5730\uff0c\u64cd\u4f5c\u4e5f\u4e0d\u6d89\u53ca SQL\u3002<\/p>\n
var mysqlx = require('@mysql\/xdevapi');\r\nmysqlx.getSession({ \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/Auth to server\r\nhost: 'localhost',\r\nport: '33060',\r\ndbUser: 'root',\r\ndbPassword: 'password'\r\n}).then(function (session) { \u00a0 \u00a0\/\/ use world_x.country.info\r\nvar schema = session.getSchema('world_x');\r\nvar collection = schema.getCollection('countryinfo');\r\ncollection \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ Get row for 'CAN'\r\n.find(\"$._id == 'CAN'\")\r\n.limit(1)\r\n.execute(doc => console.log(doc))\r\n.then(() => console.log(\"\\n\\nAll done\"));\r\nsession.close();\r\n})<\/pre>\n\u4e0b\u9762\u4f8b\u5b50\u4f7f\u7528 PHP\uff0c\u641c\u7d22 _id \u5b57\u6bb5\u5339\u914d USA \u7684\u8bb0\u5f55\uff1a<\/p>\n
\/\/ Specify collection to use\r\n$collection = $schema->getCollection(\"countryinfo\");\r\n\/\/ SELECT * FROM world_x WHERE _id = \"USA\"\r\n$result = $collection->find('_id = \"USA\"')->execute();\r\n\/\/ Fetch\/Display data\r\n$data = $result->fetchAll();\r\nvar_dump($data);\r\n?><\/pre>\n\u53ef\u4ee5\u770b\u51fa\uff0c\u5728\u4e0a\u9762\u4e24\u4e2a\u4f7f\u7528\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u4f8b\u5b50\u4e2d\uff0cfind \u64cd\u4f5c\u7b26\u7684\u7528\u6cd5\u57fa\u672c\u4e00\u81f4\u3002\u8fd9\u79cd\u4e00\u81f4\u6027\u5bf9\u8de8\u8bed\u8a00\u7f16\u7a0b\u7684\u5f00\u53d1\u8005\u6709\u5f88\u5927\u5e2e\u52a9\uff0c\u5bf9\u8bd5\u56fe\u964d\u4f4e\u65b0\u8bed\u8a00\u5b66\u4e60\u6210\u672c\u7684\u5f00\u53d1\u8005\u4e5f\u4e0d\u65e0\u88e8\u76ca\u3002<\/p>\n
\u652f\u6301\u7684\u8bed\u8a00\u8fd8\u5305\u62ec C\u3001Java\u3001Python \u548c JavaScript \u7b49\uff0c\u672a\u6765\u8fd8\u4f1a\u6709\u66f4\u591a\u652f\u6301\u7684\u8bed\u8a00\u3002<\/p>\n
\u4ece\u4e24\u79cd\u65b9\u5f0f\u53d7\u76ca<\/strong><\/div>\n\u6211\u4f1a\u544a\u8bc9\u4f60\u4f7f\u7528 NoSQL \u65b9\u5f0f\u5f55\u5165\u7684\u6570\u636e\u4e5f\u53ef\u4ee5\u7528 SQL \u65b9\u5f0f\u4f7f\u7528\uff1f\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4f1a\u544a\u8bc9\u4f60\u65b0\u5f15\u5165\u7684 NoSQL \u65b9\u5f0f\u53ef\u4ee5\u8bbf\u95ee\u65e7\u5f0f\u5173\u7cfb\u578b\u8868\u4e2d\u7684\u6570\u636e\uff1f\u73b0\u5728\u4f7f\u7528 MySQL \u670d\u52a1\u5668\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u4f5c\u4e3a SQL \u670d\u52a1\u5668\uff0c\u4f5c\u4e3a NoSQL \u670d\u52a1\u5668\u6216\u8005\u540c\u65f6\u4f5c\u4e3a\u4e24\u8005\u3002<\/p>\n
\n\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.linuxeden.com\/a\/31950<\/a><\/p>\n