{"id":1760,"date":"2025-02-18T00:11:38","date_gmt":"2025-02-17T16:11:38","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1760"},"modified":"2025-02-18T00:11:38","modified_gmt":"2025-02-17T16:11:38","slug":"mysql-%e9%80%89%e6%8b%a9%e6%95%b0%e6%8d%ae%e5%ba%93%ef%bc%88%e9%99%84%e5%b8%a6%e5%ae%9e%e4%be%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/18\/mysql-%e9%80%89%e6%8b%a9%e6%95%b0%e6%8d%ae%e5%ba%93%ef%bc%88%e9%99%84%e5%b8%a6%e5%ae%9e%e4%be%8b%ef%bc%89\/","title":{"rendered":"MySQL \u9009\u62e9\u6570\u636e\u5e93\uff08\u9644\u5e26\u5b9e\u4f8b\uff09"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 MySQL \u4e2d\uff0c\u4e00\u65e6\u8fde\u63a5\u5230\u6570\u636e\u5e93\u670d\u52a1\u5668\uff0c\u4f60\u53ef\u4ee5\u6709\u591a\u4e2a\u6570\u636e\u5e93\u53ef\u4ee5\u64cd\u4f5c\u3002\u4e3a\u4e86\u6267\u884c SQL \u67e5\u8be2\uff0c\u4f60\u9700\u8981 <strong>\u9009\u62e9<\/strong> \u4e00\u4e2a\u6570\u636e\u5e93\u3002\u4ee5\u4e0b\u662f MySQL \u9009\u62e9\u6570\u636e\u5e93\u7684\u51e0\u79cd\u65b9\u6cd5\u5927\u5168\uff0c\u9644\u5e26\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4f7f\u7528 SQL \u547d\u4ee4 <code>USE<\/code> \u9009\u62e9\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>USE<\/code> \u547d\u4ee4\u662f\u9009\u62e9\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u6700\u5e38\u7528\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u57fa\u672c\u8bed\u6cd5<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>USE database_name;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>database_name<\/code>\uff1a\u4f60\u8981\u9009\u62e9\u7684\u6570\u636e\u5e93\u540d\u79f0\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5b9e\u4f8b\uff1a\u9009\u62e9\u540d\u4e3a <code>my_blog<\/code> \u7684\u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>USE my_blog;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u544a\u8bc9 MySQL \u670d\u52a1\u5668\uff0c\u4f60\u63a5\u4e0b\u6765\u8981\u64cd\u4f5c\u7684\u662f <code>my_blog<\/code> \u6570\u636e\u5e93\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u67e5\u770b\u5f53\u524d\u9009\u62e9\u7684\u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u5f53\u524d\u6240\u9009\u7684\u6570\u636e\u5e93\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DATABASE();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c06\u8fd4\u56de\u5f53\u524d\u9009\u4e2d\u7684\u6570\u636e\u5e93\u7684\u540d\u79f0\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u901a\u8fc7 <code>SHOW DATABASES<\/code> \u67e5\u770b\u6240\u6709\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u9009\u62e9\u6570\u636e\u5e93\u4e4b\u524d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>SHOW DATABASES<\/code> \u547d\u4ee4\u67e5\u770b\u6240\u6709\u53ef\u7528\u7684\u6570\u636e\u5e93\u3002\u901a\u8fc7\u8fd9\u4e2a\u547d\u4ee4\uff0c\u4f60\u53ef\u4ee5\u786e\u8ba4\u4f60\u8981\u64cd\u4f5c\u7684\u6570\u636e\u5e93\u662f\u5426\u5b58\u5728\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u57fa\u672c\u547d\u4ee4<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW DATABASES;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5b9e\u4f8b\uff1a\u5217\u51fa\u6240\u6709\u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW DATABASES;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6267\u884c\u8fd9\u4e2a\u547d\u4ee4\u540e\uff0cMySQL \u4f1a\u5217\u51fa\u6240\u6709\u53ef\u7528\u7684\u6570\u636e\u5e93\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u5728 PHP \u4e2d\u4f7f\u7528 MySQLi \u6269\u5c55\u9009\u62e9\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7 <strong>MySQLi<\/strong> \u6269\u5c55\uff0c\u4f60\u53ef\u4ee5\u5728 PHP \u811a\u672c\u4e2d\u9009\u62e9\u6570\u636e\u5e93\u3002\u901a\u5e38\uff0c\u9009\u62e9\u6570\u636e\u5e93\u662f\u5728\u8fde\u63a5\u5230 MySQL \u540e\u8fdb\u884c\u7684\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5b9e\u4f8b\uff1a\u4f7f\u7528 MySQLi \u6269\u5c55\u9009\u62e9\u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u521b\u5efa\u8fde\u63a5\n$connection = mysqli_connect(\"localhost\", \"username\", \"password\");\n\n\/\/ \u68c0\u67e5\u8fde\u63a5\nif (!$connection) {\n    die(\"Connection failed: \" . mysqli_connect_error());\n}\n\n\/\/ \u9009\u62e9\u6570\u636e\u5e93\nmysqli_select_db($connection, \"my_blog\");\n\n\/\/ \u8fdb\u884c\u5176\u4ed6\u64cd\u4f5c\n\/\/ \u67e5\u8be2\u7b49...\n\n\/\/ \u5173\u95ed\u8fde\u63a5\nmysqli_close($connection);\n?&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>mysqli_connect()<\/code> \u8fde\u63a5\u5230 MySQL\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>mysqli_select_db()<\/code> \u9009\u62e9\u6570\u636e\u5e93 <code>my_blog<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u5728 PHP \u4e2d\u4f7f\u7528 PDO \u6269\u5c55\u9009\u62e9\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7 <strong>PDO<\/strong> \u6269\u5c55\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728 PHP \u811a\u672c\u4e2d\u9009\u62e9\u6570\u636e\u5e93\uff0c\u901a\u5e38\u5728\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\u65f6\u5c31\u6307\u5b9a\u6570\u636e\u5e93\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5b9e\u4f8b\uff1a\u4f7f\u7528 PDO \u6269\u5c55\u9009\u62e9\u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\ntry {\n    \/\/ \u521b\u5efa PDO \u5b9e\u4f8b\u5e76\u8fde\u63a5 MySQL\uff0c\u6307\u5b9a\u6570\u636e\u5e93\n    $pdo = new PDO(\"mysql:host=localhost;dbname=my_blog\", \"username\", \"password\");\n\n    \/\/ \u8bbe\u7f6e PDO \u9519\u8bef\u6a21\u5f0f\u4e3a\u5f02\u5e38\n    $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n\n    \/\/ \u8fdb\u884c\u5176\u4ed6\u64cd\u4f5c\n    \/\/ \u67e5\u8be2\u7b49...\n\n    echo \"Connected to the database successfully.\";\n} catch (PDOException $e) {\n    echo \"Connection failed: \" . $e-&gt;getMessage();\n}\n\n\/\/ \u5173\u95ed\u8fde\u63a5\n$pdo = null;\n?&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>new PDO()<\/code> \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\u65f6\uff0c\u76f4\u63a5\u5728 DSN\uff08\u6570\u636e\u6e90\u540d\u79f0\uff09\u4e2d\u6307\u5b9a\u6570\u636e\u5e93 <code>my_blog<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u5728 MySQL \u8fde\u63a5\u65f6\u6307\u5b9a\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f60\u901a\u8fc7\u547d\u4ee4\u884c\u6216 PHP \u7b49\u5de5\u5177\u8fde\u63a5\u5230 MySQL \u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u8fde\u63a5\u65f6\u6307\u5b9a\u8981\u64cd\u4f5c\u7684\u6570\u636e\u5e93\u3002\u8fd9\u7701\u53bb\u4e86\u540e\u7eed\u4f7f\u7528 <code>USE<\/code> \u547d\u4ee4\u9009\u62e9\u6570\u636e\u5e93\u7684\u6b65\u9aa4\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u547d\u4ee4\u884c\u8fde\u63a5\u65f6\u6307\u5b9a\u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql -u username -p my_blog<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u547d\u4ee4\u884c\u8fde\u63a5 MySQL \u65f6\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u6307\u5b9a\u6570\u636e\u5e93 <code>my_blog<\/code>\uff0cMySQL \u4f1a\u81ea\u52a8\u9009\u62e9\u8be5\u6570\u636e\u5e93\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u9009\u62e9\u6570\u636e\u5e93\u65f6\u7684\u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u5e93\u5fc5\u987b\u5b58\u5728<\/strong>\uff1a\u5728\u4f7f\u7528 <code>USE<\/code> \u547d\u4ee4\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u8be5\u6570\u636e\u5e93\u5df2\u7ecf\u5b58\u5728\u3002\u53ef\u4ee5\u4f7f\u7528 <code>SHOW DATABASES<\/code> \u547d\u4ee4\u67e5\u770b\u6240\u6709\u6570\u636e\u5e93\u3002<\/li>\n\n\n\n<li><strong>\u6743\u9650\u8981\u6c42<\/strong>\uff1a\u9009\u62e9\u4e00\u4e2a\u6570\u636e\u5e93\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u62e5\u6709\u8be5\u6570\u636e\u5e93\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u6743\u9650\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1a\u5982\u679c\u4f60\u5c1d\u8bd5\u9009\u62e9\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u6570\u636e\u5e93\uff0cMySQL \u4f1a\u8fd4\u56de\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684 <strong>MySQL \u9009\u62e9\u6570\u636e\u5e93<\/strong> \u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 SQL <code>USE<\/code> \u547d\u4ee4<\/strong>\uff1a\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u9009\u62e9\u5f53\u524d\u4f1a\u8bdd\u4e2d\u8981\u64cd\u4f5c\u7684\u6570\u636e\u5e93\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528 <code>SHOW DATABASES<\/code> \u67e5\u770b\u6570\u636e\u5e93<\/strong>\uff1a\u5728\u9009\u62e9\u4e4b\u524d\u67e5\u770b\u6240\u6709\u6570\u636e\u5e93\u3002<\/li>\n\n\n\n<li><strong>\u901a\u8fc7 PHP \u7684 MySQLi \u6269\u5c55\u9009\u62e9\u6570\u636e\u5e93<\/strong>\uff1a\u5728 PHP \u811a\u672c\u4e2d\u4f7f\u7528 <code>mysqli_select_db()<\/code> \u6765\u9009\u62e9\u6570\u636e\u5e93\u3002<\/li>\n\n\n\n<li><strong>\u901a\u8fc7 PHP \u7684 PDO \u6269\u5c55\u9009\u62e9\u6570\u636e\u5e93<\/strong>\uff1a\u5728 PHP \u811a\u672c\u4e2d\uff0c\u901a\u8fc7\u521b\u5efa PDO \u5b9e\u4f8b\u65f6\u6307\u5b9a\u6570\u636e\u5e93\u3002<\/li>\n\n\n\n<li><strong>\u901a\u8fc7\u547d\u4ee4\u884c\u8fde\u63a5\u65f6\u6307\u5b9a\u6570\u636e\u5e93<\/strong>\uff1a\u5728\u8fde\u63a5 MySQL \u65f6\uff0c\u76f4\u63a5\u6307\u5b9a\u6570\u636e\u5e93\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u65e0\u8bba\u4f60\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u9009\u62e9\u6570\u636e\u5e93\u662f\u8fde\u63a5\u5230 MySQL \u670d\u52a1\u5668\u540e\u7684\u5173\u952e\u6b65\u9aa4\uff0c\u786e\u4fdd\u4f60\u9009\u62e9\u4e86\u6b63\u786e\u7684\u6570\u636e\u5e93\u4ee5\u8fdb\u884c\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 MySQL \u4e2d\uff0c\u4e00\u65e6\u8fde\u63a5\u5230\u6570\u636e\u5e93\u670d\u52a1\u5668\uff0c\u4f60\u53ef\u4ee5\u6709\u591a\u4e2a\u6570\u636e\u5e93\u53ef\u4ee5\u64cd\u4f5c\u3002\u4e3a\u4e86\u6267\u884c SQL \u67e5\u8be2\uff0c\u4f60\u9700\u8981 \u9009\u62e9  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1760","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1760"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1760\/revisions"}],"predecessor-version":[{"id":1761,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1760\/revisions\/1761"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}