{"id":913,"date":"2012-10-12T13:18:07","date_gmt":"2012-10-12T05:18:07","guid":{"rendered":"http:\/\/www.poloo.org\/?p=913"},"modified":"2012-10-12T13:18:07","modified_gmt":"2012-10-12T05:18:07","slug":"%e5%85%b3%e4%ba%8emysql%e7%9a%84%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=913","title":{"rendered":"\u5173\u4e8eMySql\u7684\u5b58\u50a8\u8fc7\u7a0b"},"content":{"rendered":"<p>Mysql\u5b58\u50a8\u8fc7\u7a0b \u652f\u63015.0\u4ee5\u4e0a\u7684\u7248\u672c \u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u8981\u7528\u5b58\u50a8\u8fc7\u7a0b?<\/p>\n<p><strong>\u5b58\u50a8\u8fc7\u7a0b\u4f1a\u4f7f\u7cfb\u7edf\u8fd0\u884c\u66f4\u5feb\uff01<\/strong><\/p>\n<p>\u662fMysql\u670d\u52a1\u5668\u5728\u7f13\u5b58\u673a\u5236\u4e0a\u505a\u4e86\u6539\u8fdb\uff0c\u5c31\u50cfPrepared statements\uff08\u9884\u5904\u7406\u8bed\u53e5\uff09\u6240\u505a\u7684\u90a3\u6837\u3002\u7531\u4e8e\u6ca1\u6709\u7f16\u8bd1\u5668\uff0c\u56e0\u6b64SQL\u5b58\u50a8\u8fc7\u7a0b\u4e0d\u4f1a\u50cf\u5916\u90e8\u8bed\u8a00\uff08\u5982C\uff09\u7f16\u5199\u7684\u7a0b\u5e8f\u8fd0\u884c\u8d77\u6765\u90a3\u4e48\u5feb\u3002\u4f46\u662f\u63d0\u5347\u901f\u5ea6\u7684\u4e3b\u8981\u65b9\u6cd5\u5374\u5728\u4e8e\u80fd\u5426\u964d\u4f4e\u7f51\u7edc\u4fe1\u606f\u6d41\u91cf\u3002\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u7684\u662f\u9700\u8981\u68c0\u67e5\u3001\u5faa\u73af\u3001\u591a\u8bed\u53e5\u4f46\u6ca1\u6709\u7528\u6237\u4ea4\u4e92\u7684\u91cd\u590d\u6027\u4efb\u52a1\uff0c\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528\u4fdd\u5b58\u5728\u670d\u52a1\u5668\u4e0a\u7684\u5b58\u50a8\u8fc7\u7a0b\u6765\u5b8c\u6210\u3002\u8fd9\u6837\u5728\u6267\u884c\u4efb\u52a1\u7684\u6bcf\u4e00\u6b65\u65f6\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u4e4b\u95f4\u5c31\u6ca1\u90a3\u4e48\u591a\u7684\u4fe1\u606f\u6765\u5f80\u4e86\u3002<\/p>\n<p><strong>\u5b58\u50a8\u8fc7\u7a0b\u662f\u53ef\u590d\u7528\u7684\u7ec4\u4ef6\uff01<\/strong><\/p>\n<p>\u5f53\u4f60\u6539\u53d8\u4e86\u4e3b\u673a\u7684\u8bed\u8a00\uff0c\u8fd9\u5bf9\u5b58\u50a8\u8fc7\u7a0b\u4e0d\u4f1a\u4ea7\u751f\u5f71\u54cd\uff0c\u56e0\u4e3a\u5b83\u662f\u6570\u636e\u5e93\u903b\u8f91\u800c\u4e0d\u662f\u5e94\u7528\u7a0b\u5e8f\u3002\u5b58\u50a8\u8fc7\u7a0b\u662f\u53ef\u4ee5\u79fb\u690d\u7684\uff0c\u7528SQL\u7f16\u5199\u5b58\u50a8\u8fc7\u7a0b\u65f6\uff0c\u5c31\u77e5\u9053\u5b83\u53ef\u4ee5\u8fd0\u884c\u5728Mysql\u652f\u6301\u7684\u4efb\u4f55\u5e73\u53f0\u4e0a\uff0c\u4e0d\u9700\u8981\u4f60\u989d\u5916\u6dfb\u52a0\u8fd0\u884c\u73af\u5883\u5305\uff0c\u4e5f\u4e0d\u9700\u8981\u4e3a\u66fe\u60f3\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u6267\u884c\u8bbe\u7f6e\u8bb8\u53ef\u3002<\/p>\n<p><!--more--><\/p>\n<p><strong>\u5b58\u50a8\u8fc7\u7a0b\u5c06\u4fdd\u5b58\uff01<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u7f16\u5199\u597d\u4e86\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u4f8b\u5982\u663e\u793a\u94f6\u884c\u4e8b\u7269\u5904\u7406\u4e2d\u7684\u652f\u7968\u64a4\u9500\uff0c\u90a3\u60f3\u8981\u4e86\u89e3\u652f\u7968\u7684\u4eba\u5c31\u53ef\u4ee5\u627e\u5230\u4f60\u7684\u7a0b\u5e8f\u3002\u5b83\u4f1a\u4ee5\u6e90\u4ee3\u7801\u7684\u5f62\u5f0f\u4fdd\u5b58\u5728\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<p><strong>\u5b58\u50a8\u8fc7\u7a0b\u53ef\u4ee5\u79fb\u690d\uff01<\/strong><\/p>\n<p>Mysql\u5b8c\u5168\u652f\u6301SQL 2003\u6807\u51c6\u3002\u67d0\u4e9b\u6570\u636e\u5e93\uff08\u5982DB2\u3001Mimer\uff09\u540c\u610f\u652f\u6301\u3002\u4f46\u4e5f\u6709\u90e8\u5206\u4e0d\u652f\u6301\uff0c\u5982Oracle\u3001SQLServer\u4e0d\u652f\u6301\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u542f\u52a8MySQL\u5ba2\u6237\u7aef\uff0c\u6838\u5bf9\u7248\u672c<\/strong><\/p>\n<p>\u67092\u79cd\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>1\u3001SHOW VARIABLES LIKE &#8216;version&#8217;;<\/p>\n<p>2\u3001SELECT VERSION();<\/p>\n<p>\u5f53\u770b\u89c1\u6570\u5b57&#8217;5.0.x&#8217;\u540e\u5c31\u53ef\u4ee5\u786e\u8ba4\u5b58\u50a8\u8fc7\u7a0b\u80fd\u591f\u5728\u8fd9\u4e2a\u5ba2\u6237\u7aef\u4e0a\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u5e93<\/strong><\/p>\n<p>CREATE DATABASE db5;<\/p>\n<p>USE db5;<\/p>\n<p>CREATE TABLE t(s1 INT);<\/p>\n<p>INSERT INTO v VALUES(5);<\/p>\n<p>\u6211\u4eec\u521b\u5efa\u7b80\u5355\u4e00\u70b9\u7684\uff0c\u53ea\u662f\u4e3a\u4e86\u5b66\u4e60\u5b58\u50a8\u8fc7\u7a0b\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u9009\u62e9\u5206\u9694\u7b26<\/strong><\/p>\n<p>\u73b0\u5728\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u5206\u9694\u7b26\uff0c\u5b9e\u73b0\u8fd9\u4e2a\u6b65\u9aa4\u7684SQL\u8bed\u53e5\u5982\u4e0b\uff1a<\/p>\n<p>DELIMITER \/\/<\/p>\n<p>\u5206\u9694\u7b26\u662f\u901a\u77e5mysql\u5ba2\u6237\u7aef\u5df2\u7ecf\u5b8c\u6210\u8f93\u5165\u4e00\u4e2asql\u8bed\u53e5\u7684\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u7b26\u53f7\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u521b\u5efa\u7a0b\u5e8f\u5b9e\u4f8b<\/strong><\/p>\n<p>CREATE PROCEDURE p1() SELECT * FROM t; \/\/<\/p>\n<p>SQL\u8bed\u53e5\u5b58\u50a8\u8fc7\u7a0b\u7684\u7b2c\u4e00\u90e8\u5206\u662f\uff1a\u201cCREATE PROCEDURE\u201d<\/p>\n<p>\u7b2c\u4e8c\u90e8\u5206\u662f\u8fc7\u7a0b\u540d\uff0c\u4e0a\u9762\u65b0\u5b58\u50a8\u8fc7\u7a0b\u7684\u540d\u5b57\u662fp1\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u9898\u5916\u8bdd\uff1a\u5408\u6cd5\u6807\u8bc6\u7b26\u7684\u95ee\u9898<\/strong><\/p>\n<p>\u5b58\u50a8\u8fc7\u7a0b\u540d\u5bf9\u5927\u5c0f\u5199\u4e0d\u654f\u611f\uff0c\u56e0\u6b64&#8217;P1&#8217;\u548c&#8217;p1&#8217;\u662f\u540c\u4e00\u4e2a\u540d\u5b57\uff0c\u5728\u540c\u4e00\u4e2a\u6570\u636e\u5e93\u4e2d\u4e0d\u80fd\u7ed9\u4e24\u4e2a\u5b58\u50a8\u8fc7\u7a0b\u53d6\u76f8\u540c\u7684\u540d\u5b57\uff0c\u56e0\u4e3a\u8fd9\u6837\u5c06\u4f1a\u5bfc\u81f4\u91cd\u8f7d\u3002<\/p>\n<p>\u53ef\u4ee5\u91c7\u53d6\u201c\u6570\u636e\u5e93\u540d.\u5b58\u50a8\u8fc7\u7a0b\u540d\u201d\u8fd9\u6837\u7684\u6298\u4e2d\u65b9\u6cd5\uff0c\u5982\uff1a\u201cdb5.p1\u201d\u3002<\/p>\n<p>\u5b58\u50a8\u8fc7\u7a0b\u540d\u53ef\u4ee5\u5206\u5f00\uff0c\u5b83\u53ef\u4ee5\u5305\u62ec\u7a7a\u683c\u7b26\uff0c\u5176\u957f\u5ea6\u9650\u5236\u4e3a64\u4e2a\u5b57\u7b26\uff0c\u4f46\u6ce8\u610f\u4e0d\u8981\u4f7f\u7528MySql\u5185\u7f6e\u51fd\u6570\u7684\u540d\u5b57\uff0c\u5426\u5219\u4f1a\u53d1\u751f\u9519\u8bef\uff0c\u5982\u4e0b\uff1a<\/p>\n<p><strong>mysql&gt; CALL pi(); <\/strong> <strong>Error 1064 (42000): You have a syntax error. <\/strong><\/p>\n<p>\u7b2c\u4e09\u90e8\u5206\u662f\u201c\uff08\uff09\u201d\uff0c\u662f\u201c\u53c2\u6570\u5217\u8868\u201d\u3002<\/p>\n<p>\u901a\u5e38\u9700\u8981\u5728\u62ec\u53f7\u5185\u6dfb\u52a0\u53c2\u6570\u3002<\/p>\n<p>\u6700\u540e\u4e00\u90e8\u5206\u201cSELECT * FROM t;\u201d\u662f\u5b58\u50a8\u8fc7\u7a0b\u7684\u4e3b\u4f53\u3002\u662f\u4e00\u822c\u7684SQL\u8bed\u53e5\uff0c\u4e00\u822c\u4e0d\u4f1a\u5c06SELECT\u8bed\u53e5\u7528\u5728\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u4ec0\u4e48MySql\u8bed\u53e5\u5728\u5b58\u50a8\u8fc7\u7a0b\u4f53\u4e2d\u662f\u5408\u6cd5\u7684\uff1f<\/strong><\/p>\n<p>\u4ec0\u4e48\u6837\u7684SQL\u8bed\u53e5\u5728Mysql\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u624d\u662f\u5408\u6cd5\u7684\u5462\uff1f\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5305\u542bINSERT, UPDATE, DELETE, SELECT, DROP, CREATE, REPLACE\u7b49\u7b49\u7684\u8bed\u53e5\u3002\u4f60\u552f\u4e00\u9700\u8981\u8bb0\u4f4f\u7684\u662f\u5982\u679c\u4ee3\u7801\u4e2d\u5305\u542bMySQL\u6269\u5145\u529f\u80fd\uff0c\u90a3\u4e48\u4ee3\u7801\u5c06\u4e0d\u80fd\u79fb\u690d\u3002\u5728\u6807\u51c6SQL\u8bed\u53e5\u4e2d\uff1a\u4efb\u4f55\u6570\u636e\u5e93\u5b9a\u4e49\u8bed\u8a00\u90fd\u662f\u5408\u6cd5\u7684\uff0c\u5982\uff1a CREATE PROCEDURE p () DELETE FROM t; \/\/<\/p>\n<p>SET\u3001COMMIT\u4ee5\u53caROLLBACK\u4e5f\u662f\u5408\u6cd5\u7684\uff0c\u5982\uff1a CREATE PROCEDURE p () SET @x = 5; \/\/<\/p>\n<p>MySQL\u7684\u9644\u52a0\u529f\u80fd\uff1a\u4efb\u4f55\u6570\u636e\u64cd\u4f5c\u8bed\u8a00\u7684\u8bed\u53e5\u90fd\u5c06\u5408\u6cd5\u3002 CREATE PROCEDURE p () DROP TABLE t; \/\/<\/p>\n<p>MySQL\u6269\u5145\u529f\u80fd\uff1a\u76f4\u63a5\u7684SELECT\u4e5f\u662f\u5408\u6cd5\u7684\uff1a CREATE PROCEDURE p () SELECT &#8216;a&#8217;; \/\/<\/p>\n<p>\u987a\u4fbf\u63d0\u4e00\u4e0b\uff0c\u6211\u5c06\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u5305\u62ecDDL\u8bed\u53e5\u7684\u529f\u80fd\u79f0\u4e3aMySQL\u9644\u52a0\u529f\u80fd\u7684\u539f\u56e0\u662f\u5728SQL\u6807 \u51c6\u4e2d\u628a\u8fd9\u4e2a\u5b9a\u4e49\u4e3a\u975e\u6838\u5fc3\u7684\uff0c\u5373\u53ef\u9009\u7ec4\u4ef6\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u8c03\u7528\u5b58\u50a8\u8fc7\u7a0b<\/strong><\/p>\n<p>mysql&gt; CALL p1();\/\/<br \/>\n+&#8212;-+<br \/>\n| si |<br \/>\n+&#8212;-+<br \/>\n| 5 |<br \/>\n+&#8212;-+<\/p>\n<p>\u8fd9\u548c\u6267\u884c&#8221;SELECT * FROM t;&#8221;\u6548\u679c\u4e00\u6837\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mysql\u5b58\u50a8\u8fc7\u7a0b \u652f\u63015.0\u4ee5\u4e0a\u7684\u7248\u672c \u3002 \u4e3a\u4ec0\u4e48\u8981\u7528\u5b58\u50a8\u8fc7\u7a0b? &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[38,63,43,64],"class_list":["post-913","post","type-post","status-publish","format-standard","hentry","category-DB","tag-mysql","tag-63","tag-43","tag-64"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=913"}],"version-history":[{"count":1,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/913\/revisions"}],"predecessor-version":[{"id":914,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/913\/revisions\/914"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}