{"id":43656,"date":"2024-12-01T08:57:49","date_gmt":"2024-12-01T00:57:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/43656\/"},"modified":"2024-12-01T08:57:49","modified_gmt":"2024-12-01T00:57:49","slug":"%e4%bd%95%e6%97%b6%e5%ba%94%e5%bd%93%e4%bd%bf%e7%94%a8mysql%e4%ba%8b%e5%8a%a1%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/43656\/","title":{"rendered":"\u4f55\u65f6\u5e94\u5f53\u4f7f\u7528MySQL\u4e8b\u52a1\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u4f55\u65f6\u5e94\u5f53\u4f7f\u7528MySQL\u4e8b\u52a1\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u4f55\u65f6\u5e94\u5f53\u4f7f\u7528MySQL\u4e8b\u52a1\uff1f\u300b\uff0c\u8986\u76d6\u4e86\u6570\u636e\u5e93\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p>MySQL\u4e8b\u52a1\u5b9e\u8df5\uff1a\u5728\u54ea\u4e9b\u60c5\u51b5\u4e0b\u5e94\u8be5\u4f7f\u7528\u4e8b\u52a1\uff1f<\/p>\n<p>\u5728\u6570\u636e\u5e93\u7ba1\u7406\u4e2d\uff0c\u4e8b\u52a1\u662f\u4e00\u7ec4SQL\u8bed\u53e5\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u5355\u4e00\u7684\u5de5\u4f5c\u5355\u5143\uff0c\u8981\u4e48\u4e00\u8d77\u6210\u529f\u6267\u884c\uff0c\u8981\u4e48\u4e00\u8d77\u5931\u8d25\u6267\u884c\u3002MySQL\u652f\u6301\u4e8b\u52a1\u64cd\u4f5c\uff0c\u5b83\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5e93\u7684\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u4e8b\u52a1\u80fd\u591f\u6709\u6548\u5730\u7ba1\u7406\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5e76\u907f\u514d\u6570\u636e\u5f02\u5e38\u6216\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\u53d1\u751f\u3002<\/p>\n<p>\u5728\u54ea\u4e9b\u60c5\u51b5\u4e0b\u5e94\u8be5\u4f7f\u7528\u4e8b\u52a1\u5462\uff1f\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u60c5\u51b5\uff1a<\/p>\n<ol>\n<li>\u6570\u636e\u5e93\u66f4\u65b0\u64cd\u4f5c\uff1a\u5f53\u6709\u591a\u6761SQL\u8bed\u53e5\u9700\u8981\u4e00\u8d77\u6267\u884c\uff0c\u5e76\u4e14\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\u903b\u8f91\u4e0a\u7684\u4f9d\u8d56\u5173\u7cfb\u65f6\uff0c\u5e94\u8be5\u5c06\u8fd9\u4e9bSQL\u8bed\u53e5\u653e\u5728\u4e00\u4e2a\u4e8b\u52a1\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5176\u4e2d\u67d0\u6761\u8bed\u53e5\u6267\u884c\u5931\u8d25\u800c\u5bfc\u81f4\u6570\u636e\u5e93\u5904\u4e8e\u4e0d\u4e00\u81f4\u72b6\u6001\u3002<\/li>\n<li>\u591a\u8868\u5173\u8054\u64cd\u4f5c\uff1a\u5728\u8fdb\u884c\u8de8\u8868\u67e5\u8be2\u6216\u66f4\u65b0\u65f6\uff0c\u5982\u679c\u9700\u8981\u4fdd\u8bc1\u591a\u5f20\u8868\u4e4b\u95f4\u7684\u6570\u636e\u4e00\u81f4\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e8b\u52a1\u6765\u786e\u4fdd\u8fd9\u4e9b\u64cd\u4f5c\u8981\u4e48\u5168\u90e8\u6210\u529f\uff0c\u8981\u4e48\u5168\u90e8\u5931\u8d25\u3002<\/li>\n<li>\u5e76\u53d1\u8bbf\u95ee\u63a7\u5236\uff1a\u5728\u9ad8\u5e76\u53d1\u73af\u5883\u4e0b\uff0c\u591a\u4e2a\u7528\u6237\u540c\u65f6\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u65f6\uff0c\u4e3a\u4e86\u907f\u514d\u6570\u636e\u7ade\u4e89\u548c\u810f\u8bfb\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e8b\u52a1\u6765\u63a7\u5236\u5e76\u53d1\u8bbf\u95ee\u3002<\/li>\n<\/ol>\n<p>\u4e0b\u9762\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u6f14\u793a\u5728MySQL\u4e2d\u5982\u4f55\u5e94\u7528\u4e8b\u52a1\uff1a<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u8ba2\u5355\u8868(order)\u548c\u5e93\u5b58\u8868(inventory)\uff0c\u8ba2\u5355\u8868\u5b58\u50a8\u8ba2\u5355\u4fe1\u606f\uff0c\u5e93\u5b58\u8868\u5b58\u50a8\u5546\u54c1\u7684\u5e93\u5b58\u6570\u91cf\u3002\u6211\u4eec\u9700\u8981\u5728\u7528\u6237\u4e0b\u5355\u65f6\u66f4\u65b0\u8ba2\u5355\u8868\u5e76\u51cf\u5c11\u5e93\u5b58\u6570\u91cf\uff0c\u8fd9\u4e24\u4e2a\u64cd\u4f5c\u5fc5\u987b\u8981\u4fdd\u6301\u4e00\u81f4\uff0c\u5426\u5219\u4f1a\u9020\u6210\u8ba2\u5355\u548c\u5e93\u5b58\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>-- \u5f00\u542f\u4e8b\u52a1\nSTART TRANSACTION;\n\n-- \u63d2\u5165\u8ba2\u5355\u4fe1\u606f\nINSERT INTO order(order_id, user_id, product_id, quantity, order_time) \nVALUES (1, 101, 201, 2, NOW());\n\n-- \u66f4\u65b0\u5e93\u5b58\u6570\u91cf\nUPDATE inventory SET quantity = quantity - 2 WHERE product_id = 201;\n\n-- \u63d0\u4ea4\u4e8b\u52a1\nCOMMIT;<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>START TRANSACTION<\/code>\u5f00\u542f\u4e00\u4e2a\u4e8b\u52a1\uff0c\u7136\u540e\u4f9d\u6b21\u6267\u884c\u63d2\u5165\u8ba2\u5355\u548c\u66f4\u65b0\u5e93\u5b58\u7684SQL\u8bed\u53e5\uff0c\u6700\u540e\u4f7f\u7528<code>COMMIT<\/code>\u63d0\u4ea4\u4e8b\u52a1\u3002\u5982\u679c\u5176\u4e2d\u4efb\u4f55\u4e00\u4e2a\u64cd\u4f5c\u5931\u8d25\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ROLLBACK<\/code>\u56de\u6eda\u4e8b\u52a1\uff0c\u786e\u4fdd\u8ba2\u5355\u548c\u5e93\u5b58\u64cd\u4f5c\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<p>\u603b\u7ed3\uff0cMySQL\u4e8b\u52a1\u5728\u6570\u636e\u5e93\u7ba1\u7406\u4e2d\u8d77\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\uff0c\u5728\u7279\u5b9a\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u4e8b\u52a1\u80fd\u591f\u4fdd\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u5728\u54ea\u4e9b\u60c5\u51b5\u4e0b\u5e94\u8be5\u4f7f\u7528\u4e8b\u52a1\uff0c\u4ee5\u53ca\u5982\u4f55\u5728MySQL\u4e2d\u5b9e\u8df5\u4e8b\u52a1\u64cd\u4f5c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u4f55\u65f6\u5e94\u5f53\u4f7f\u7528MySQL\u4e8b\u52a1\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8emysql,\u5e94\u7528,\u4e8b\u52a1\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u4f55\u65f6\u5e94\u5f53\u4f7f\u7528MySQL\u4e8b\u52a1\uff1f \u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u4f55\u65f6\u5e94\u5f53\u4f7f\u7528MySQL\u4e8b\u52a1\uff1f\u300b\uff0c\u8986\u76d6\u4e86\u6570\u636e\u5e93\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002 MySQL\u4e8b\u52a1\u5b9e\u8df5\uff1a\u5728\u54ea\u4e9b\u60c5\u51b5\u4e0b\u5e94\u8be5\u4f7f\u7528\u4e8b\u52a1\uff1f \u5728\u6570\u636e\u5e93\u7ba1\u7406\u4e2d\uff0c\u4e8b\u52a1\u662f\u4e00\u7ec4SQL\u8bed\u53e5\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u5355\u4e00\u7684\u5de5\u4f5c\u5355\u5143\uff0c\u8981\u4e48\u4e00\u8d77\u6210\u529f\u6267\u884c\uff0c\u8981\u4e48\u4e00\u8d77\u5931\u8d25\u6267\u884c\u3002MySQL\u652f\u6301\u4e8b\u52a1\u64cd\u4f5c\uff0c\u5b83\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5e93\u7684\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u4e8b\u52a1\u80fd\u591f\u6709\u6548\u5730\u7ba1\u7406\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5e76\u907f\u514d\u6570\u636e\u5f02\u5e38\u6216\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\u53d1\u751f\u3002 \u5728\u54ea\u4e9b\u60c5\u51b5\u4e0b\u5e94\u8be5\u4f7f\u7528\u4e8b\u52a1\u5462\uff1f\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u60c5\u51b5\uff1a \u6570\u636e\u5e93\u66f4\u65b0\u64cd\u4f5c\uff1a\u5f53\u6709\u591a\u6761SQL\u8bed\u53e5\u9700\u8981\u4e00\u8d77\u6267\u884c\uff0c\u5e76\u4e14\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\u903b\u8f91\u4e0a\u7684\u4f9d\u8d56\u5173\u7cfb\u65f6\uff0c\u5e94\u8be5\u5c06\u8fd9\u4e9bSQL\u8bed\u53e5\u653e\u5728\u4e00\u4e2a\u4e8b\u52a1\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5176\u4e2d\u67d0\u6761\u8bed\u53e5\u6267\u884c\u5931\u8d25\u800c\u5bfc\u81f4\u6570\u636e\u5e93\u5904\u4e8e\u4e0d\u4e00\u81f4\u72b6\u6001\u3002 \u591a\u8868\u5173\u8054\u64cd\u4f5c\uff1a\u5728\u8fdb\u884c\u8de8\u8868\u67e5\u8be2\u6216\u66f4\u65b0\u65f6\uff0c\u5982\u679c\u9700\u8981\u4fdd\u8bc1\u591a\u5f20\u8868\u4e4b\u95f4\u7684\u6570\u636e\u4e00\u81f4\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e8b\u52a1\u6765\u786e\u4fdd\u8fd9\u4e9b\u64cd\u4f5c\u8981\u4e48\u5168\u90e8\u6210\u529f\uff0c\u8981\u4e48\u5168\u90e8\u5931\u8d25\u3002 \u5e76\u53d1\u8bbf\u95ee\u63a7\u5236\uff1a\u5728\u9ad8\u5e76\u53d1\u73af\u5883\u4e0b\uff0c\u591a\u4e2a\u7528\u6237\u540c\u65f6\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u65f6\uff0c\u4e3a\u4e86\u907f\u514d\u6570\u636e\u7ade\u4e89\u548c\u810f\u8bfb\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e8b\u52a1\u6765\u63a7\u5236\u5e76\u53d1\u8bbf\u95ee\u3002 \u4e0b\u9762\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u6f14\u793a\u5728MySQL\u4e2d\u5982\u4f55\u5e94\u7528\u4e8b\u52a1\uff1a \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u8ba2\u5355\u8868(order)\u548c\u5e93\u5b58\u8868(inventory)\uff0c\u8ba2\u5355\u8868\u5b58\u50a8\u8ba2\u5355\u4fe1\u606f\uff0c\u5e93\u5b58\u8868\u5b58\u50a8\u5546\u54c1\u7684\u5e93\u5b58\u6570\u91cf\u3002\u6211\u4eec\u9700\u8981\u5728\u7528\u6237\u4e0b\u5355\u65f6\u66f4\u65b0\u8ba2\u5355\u8868\u5e76\u51cf\u5c11\u5e93\u5b58\u6570\u91cf\uff0c\u8fd9\u4e24\u4e2a\u64cd\u4f5c\u5fc5\u987b\u8981\u4fdd\u6301\u4e00\u81f4\uff0c\u5426\u5219\u4f1a\u9020\u6210\u8ba2\u5355\u548c\u5e93\u5b58\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\u3002 \u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a &#8212; \u5f00\u542f\u4e8b\u52a1 START TRANSACTION; &#8212; \u63d2\u5165\u8ba2\u5355\u4fe1\u606f INSERT INTO order(order_id, user_id, product_id, quantity, order_time) VALUES (1, 101, 201, 2, NOW()); &#8212; \u66f4\u65b0\u5e93\u5b58\u6570\u91cf UPDATE inventory SET quantity = quantity &#8211; 2 WHERE product_id = 201; &#8212; \u63d0\u4ea4\u4e8b\u52a1 COMMIT; \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528START TRANSACTION\u5f00\u542f\u4e00\u4e2a\u4e8b\u52a1\uff0c\u7136\u540e\u4f9d\u6b21\u6267\u884c\u63d2\u5165\u8ba2\u5355\u548c\u66f4\u65b0\u5e93\u5b58\u7684SQL\u8bed\u53e5\uff0c\u6700\u540e\u4f7f\u7528COMMIT\u63d0\u4ea4\u4e8b\u52a1\u3002\u5982\u679c\u5176\u4e2d\u4efb\u4f55\u4e00\u4e2a\u64cd\u4f5c\u5931\u8d25\uff0c\u53ef\u4ee5\u4f7f\u7528ROLLBACK\u56de\u6eda\u4e8b\u52a1\uff0c\u786e\u4fdd\u8ba2\u5355\u548c\u5e93\u5b58\u64cd\u4f5c\u4fdd\u6301\u4e00\u81f4\u3002 \u603b\u7ed3\uff0cMySQL\u4e8b\u52a1\u5728\u6570\u636e\u5e93\u7ba1\u7406\u4e2d\u8d77\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\uff0c\u5728\u7279\u5b9a\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u4e8b\u52a1\u80fd\u591f\u4fdd\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u5728\u54ea\u4e9b\u60c5\u51b5\u4e0b\u5e94\u8be5\u4f7f\u7528\u4e8b\u52a1\uff0c\u4ee5\u53ca\u5982\u4f55\u5728MySQL\u4e2d\u5b9e\u8df5\u4e8b\u52a1\u64cd\u4f5c\u3002 \u4ee5\u4e0a\u5c31\u662f\u300a\u4f55\u65f6\u5e94\u5f53\u4f7f\u7528MySQL\u4e8b\u52a1\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8emysql,\u5e94\u7528,\u4e8b\u52a1\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-43656","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43656","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=43656"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43656\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=43656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=43656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=43656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}