{"id":5904,"date":"2024-11-14T12:23:52","date_gmt":"2024-11-14T04:23:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5904\/"},"modified":"2024-11-14T12:23:52","modified_gmt":"2024-11-14T04:23:52","slug":"sql%e4%b8%8a%e7%9a%84%e8%a1%a8%e5%a4%aa%e5%a4%9a%e6%80%8e%e4%b9%88%e7%9c%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5904\/","title":{"rendered":"sql\u4e0a\u7684\u8868\u592a\u591a\u600e\u4e48\u770b"},"content":{"rendered":"<blockquote><p>\n  \u7ba1\u7406\u5305\u542b\u5927\u91cf\u8868\u7684 sql \u6570\u636e\u5e93\u65f6\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u65b9\u6cd5\u8fdb\u884c\u6d4f\u89c8\uff1a\u4f7f\u7528 show tables \u547d\u4ee4\u83b7\u53d6\u6240\u6709\u8868\u7684\u540d\u79f0\u3002\u4f7f\u7528 information_schema \u6a21\u5f0f\u83b7\u53d6\u6709\u5173\u8868\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u4f7f\u7528\u901a\u914d\u7b26\u641c\u7d22\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u8868\u3002\u4f7f\u7528\u6570\u636e\u5e93\u7ba1\u7406\u5de5\u5177\u8fdb\u884c\u56fe\u5f62\u5316\u6d4f\u89c8\u3002\u4f7f\u7528\u547d\u4ee4\u884c\u811a\u672c\u81ea\u52a8\u5316\u6d4f\u89c8\u8fc7\u7a0b\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060322035862010.jpg\" class=\"aligncenter\" title=\"sql\u4e0a\u7684\u8868\u592a\u591a\u600e\u4e48\u770b\u63d2\u56fe\" alt=\"sql\u4e0a\u7684\u8868\u592a\u591a\u600e\u4e48\u770b\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728\u5927\u91cf\u8868\u7684\u60c5\u51b5\u4e0b\u6d4f\u89c8 SQL \u8868<\/strong><\/p>\n<p>\u7ba1\u7406\u62e5\u6709\u5927\u91cf\u8868\u7684 SQL \u6570\u636e\u5e93\u53ef\u80fd\u4f1a\u5f88\u68d8\u624b\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u67e5\u770b\u548c\u6d4f\u89c8\u8fd9\u4e9b\u8868\u7684\u6709\u6548\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u4f7f\u7528 SHOW TABLES \u547d\u4ee4<\/strong><\/p>\n<p>SHOW TABLES \u547d\u4ee4\u4f1a\u6253\u5370\u51fa\u6570\u636e\u5e93\u4e2d\u6240\u6709\u8868\u7684\u540d\u79f0\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b64\u547d\u4ee4\u83b7\u53d6\u6240\u6709\u8868\u7684\u6982\u8ff0\uff1a<\/p>\n<pre>SHOW TABLES;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528 INFORMATION_SCHEMA<\/strong><\/p>\n<p>INFORMATION_SCHEMA \u662f\u4e00\u4e2a\u7279\u6b8a\u6a21\u5f0f\uff0c\u5305\u542b\u6709\u5173\u6570\u636e\u5e93\u5bf9\u8c61\u7684\u4fe1\u606f\u3002\u5b83\u5305\u542b\u4ee5\u4e0b\u8868\uff0c\u5176\u4e2d\u5305\u542b\u6709\u5173\u8868\u7684\u8be6\u7ec6\u6570\u636e\uff1a<\/p>\n<ul>\n<li>TABLES: \u5305\u542b\u6709\u5173\u6240\u6709\u8868\u7684\u603b\u4f53\u4fe1\u606f\u3002<\/li>\n<li>COLUMNS: \u5305\u542b\u6709\u5173\u6bcf\u4e2a\u8868\u4e2d\u7684\u5217\u7684\u4fe1\u606f\u3002<\/li>\n<li>STATISTICS: \u542b\u6709\u5173\u4e8e\u8868\u7edf\u8ba1\u4fe1\u606f\uff0c\u4f8b\u5982\u884c\u6570\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u8981\u83b7\u53d6\u6709\u5173\u7279\u5b9a\u8868\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'my_table';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528\u7279\u6b8a\u5b57\u7b26<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 % \u7279\u6b8a\u5b57\u7b26\u4f5c\u4e3a\u901a\u914d\u7b26\u6765\u5339\u914d\u8868\u540d\u4e2d\u7684\u6a21\u5f0f\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u5c06\u663e\u793a\u6240\u6709\u540d\u79f0\u4e2d\u5305\u542b &#8220;user&#8221; \u7684\u8868\uff1a<\/p>\n<pre>SHOW TABLES LIKE 'user%';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528\u6570\u636e\u5e93\u7ba1\u7406\u5de5\u5177<\/strong><\/p>\n<p>\u5927\u591a\u6570\u6570\u636e\u5e93\u7ba1\u7406\u5de5\u5177\uff08DBMS\uff09\uff0c\u4f8b\u5982 MySQL Workbench \u6216 pgAdmin\uff0c\u90fd\u63d0\u4f9b\u4e86\u6d4f\u89c8\u8868\u5e76\u67e5\u770b\u5176\u4fe1\u606f\u7684\u56fe\u5f62\u754c\u9762\u3002\u8fd9\u4e9b\u5de5\u5177\u901a\u5e38\u5141\u8bb8\u60a8\u8f7b\u677e\u5730\u7b5b\u9009\u548c\u641c\u7d22\u8868\u3002<\/p>\n<p><strong>\u4f7f\u7528\u547d\u4ee4\u884c\u811a\u672c<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u7f16\u5199\u547d\u4ee4\u884c\u811a\u672c\u6765\u81ea\u52a8\u5316\u8868\u6d4f\u89c8\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b Bash \u811a\u672c\u5c06\u4e3a\u6570\u636e\u5e93\u4e2d\u7684\u6bcf\u4e2a\u8868\u6253\u5370\u8868\u540d\u79f0\u3001\u5217\u6570\u548c\u884c\u6570\uff1a<\/p>\n<pre>#!\/bin\/bash\n\n# \u83b7\u53d6\u6570\u636e\u5e93\u540d\u79f0\ndatabase_name=my_database\n\n# \u8fde\u63a5\u5230\u6570\u636e\u5e93\nmysql -u username -p password $database_name &lt;&lt; EOF\n\n# \u904d\u5386\u6240\u6709\u8868\nfor table_name in $(SHOW TABLES); do\n\n    # \u83b7\u53d6\u5217\u6570\n    column_count=$(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_NAME = '$table_name');\n\n    # \u83b7\u53d6\u884c\u6570\n    row_count=$(SELECT COUNT(*) FROM $table_name);\n\n    # \u6253\u5370\u4fe1\u606f\n    printf \"$table_name: $column_count columns, $row_count rows\n\"\n\ndone\nEOF<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u5229\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u6d4f\u89c8\u548c\u7ba1\u7406 SQL \u6570\u636e\u5e93\u4e2d\u7684\u5927\u91cf\u8868\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u4e0a\u7684\u8868\u592a\u591a\u600e\u4e48\u770b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ba1\u7406\u5305\u542b\u5927\u91cf\u8868\u7684 sql \u6570\u636e\u5e93\u65f6\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u65b9\u6cd5\u8fdb\u884c\u6d4f\u89c8\uff1a\u4f7f\u7528 show tables \u547d\u4ee4\u83b7\u53d6\u6240\u6709\u8868\u7684\u540d\u79f0\u3002\u4f7f\u7528 information_schema \u6a21\u5f0f\u83b7\u53d6\u6709\u5173\u8868\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u4f7f\u7528\u901a\u914d\u7b26\u641c\u7d22\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u8868\u3002\u4f7f\u7528\u6570\u636e\u5e93\u7ba1\u7406\u5de5\u5177\u8fdb\u884c\u56fe\u5f62\u5316\u6d4f\u89c8\u3002\u4f7f\u7528\u547d\u4ee4\u884c\u811a\u672c\u81ea\u52a8\u5316\u6d4f\u89c8\u8fc7\u7a0b\u3002 \u5982\u4f55\u5728\u5927\u91cf\u8868\u7684\u60c5\u51b5\u4e0b\u6d4f\u89c8 SQL \u8868 \u7ba1\u7406\u62e5\u6709\u5927\u91cf\u8868\u7684 SQL \u6570\u636e\u5e93\u53ef\u80fd\u4f1a\u5f88\u68d8\u624b\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u67e5\u770b\u548c\u6d4f\u89c8\u8fd9\u4e9b\u8868\u7684\u6709\u6548\u65b9\u6cd5\uff1a \u4f7f\u7528 SHOW TABLES \u547d\u4ee4 SHOW TABLES \u547d\u4ee4\u4f1a\u6253\u5370\u51fa\u6570\u636e\u5e93\u4e2d\u6240\u6709\u8868\u7684\u540d\u79f0\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b64\u547d\u4ee4\u83b7\u53d6\u6240\u6709\u8868\u7684\u6982\u8ff0\uff1a SHOW TABLES; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 INFORMATION_SCHEMA INFORMATION_SCHEMA \u662f\u4e00\u4e2a\u7279\u6b8a\u6a21\u5f0f\uff0c\u5305\u542b\u6709\u5173\u6570\u636e\u5e93\u5bf9\u8c61\u7684\u4fe1\u606f\u3002\u5b83\u5305\u542b\u4ee5\u4e0b\u8868\uff0c\u5176\u4e2d\u5305\u542b\u6709\u5173\u8868\u7684\u8be6\u7ec6\u6570\u636e\uff1a TABLES: \u5305\u542b\u6709\u5173\u6240\u6709\u8868\u7684\u603b\u4f53\u4fe1\u606f\u3002 COLUMNS: \u5305\u542b\u6709\u5173\u6bcf\u4e2a\u8868\u4e2d\u7684\u5217\u7684\u4fe1\u606f\u3002 STATISTICS: \u542b\u6709\u5173\u4e8e\u8868\u7edf\u8ba1\u4fe1\u606f\uff0c\u4f8b\u5982\u884c\u6570\u3002 \u4f8b\u5982\uff0c\u8981\u83b7\u53d6\u6709\u5173\u7279\u5b9a\u8868\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = &#8216;my_table&#8217;; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528\u7279\u6b8a\u5b57\u7b26 \u60a8\u53ef\u4ee5\u4f7f\u7528 % \u7279\u6b8a\u5b57\u7b26\u4f5c\u4e3a\u901a\u914d\u7b26\u6765\u5339\u914d\u8868\u540d\u4e2d\u7684\u6a21\u5f0f\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u5c06\u663e\u793a\u6240\u6709\u540d\u79f0\u4e2d\u5305\u542b &#8220;user&#8221; \u7684\u8868\uff1a SHOW TABLES LIKE &#8216;user%&#8217;; \u767b\u5f55\u540e\u590d\u5236 [&hellip;]<\/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-5904","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5904","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=5904"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5904\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}