{"id":5880,"date":"2024-11-14T10:19:04","date_gmt":"2024-11-14T02:19:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5880\/"},"modified":"2024-11-14T10:19:04","modified_gmt":"2024-11-14T02:19:04","slug":"sql%e6%80%8e%e4%b9%88%e6%9f%a5%e8%af%a2%e8%a1%a8%e7%9a%84%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5880\/","title":{"rendered":"sql\u600e\u4e48\u67e5\u8be2\u8868\u7684\u7ed3\u6784"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7 describe\u3001information_schema \u8868\u6216 jdbc api\uff0c\u53ef\u4ee5\u67e5\u8be2\u8868\u7684\u7ed3\u6784\uff0c\u5305\u62ec\u5217\u540d\u3001\u6570\u636e\u7c7b\u578b\u3001\u7ea6\u675f\u6761\u4ef6\u7b49\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060321334493229.jpg\" class=\"aligncenter\" title=\"sql\u600e\u4e48\u67e5\u8be2\u8868\u7684\u7ed3\u6784\u63d2\u56fe\" alt=\"sql\u600e\u4e48\u67e5\u8be2\u8868\u7684\u7ed3\u6784\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 SQL \u67e5\u8be2\u8868\u7684\u7ed3\u6784<\/strong><\/p>\n<p>SQL \u8bed\u8a00\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u67e5\u8be2\u8868\u7684\u7ed3\u6784\uff0c\u5305\u62ec\uff1a<\/p>\n<p><strong>DESCRIBE \u547d\u4ee4<\/strong><\/p>\n<p>DESCRIBE \u547d\u4ee4\u63d0\u4f9b\u6709\u5173\u8868\u4e2d\u5217\u7684\u8be6\u7ec6\u63cf\u8ff0\uff0c\u5305\u62ec\u6570\u636e\u7c7b\u578b\u3001\u957f\u5ea6\u548c\u7ea6\u675f\u6761\u4ef6\u3002\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<pre>DESCRIBE table_name;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>INFORMATION_SCHEMA \u8868<\/strong><\/p>\n<p>INFORMATION_SCHEMA \u6570\u636e\u5e93\u5305\u542b\u6709\u5173\u6570\u636e\u5e93\u4e2d\u5bf9\u8c61\uff08\u5305\u62ec\u8868\uff09\u5143\u6570\u636e\u7684\u4fe1\u606f\u3002\u67e5\u8be2\u8868\u7ed3\u6784\u7684\u5e38\u7528\u8868\u662f COLUMNS \u8868\uff0c\u5b83\u5305\u542b\u6709\u5173\u8868\u4e2d\u5217\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<pre>SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table_name';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>JDBC API<\/strong><\/p>\n<p>Java \u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684 JDBC API \u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u6cd5\u6765\u83b7\u53d6\u8868\u7ed3\u6784\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8c03\u7528 DatabaseMetaData \u63a5\u53e3\u7684 getColumns() \u65b9\u6cd5\u6216 getTable() \u65b9\u6cd5\u3002<\/p>\n<p><strong>\u5176\u4ed6\u65b9\u6cd5<\/strong><\/p>\n<ul>\n<li> <strong>SHOW COLUMNS \u547d\u4ee4\uff1a<\/strong> MySQL \u7279\u6709\u7684\u547d\u4ee4\uff0c\u663e\u793a\u8868\u4e2d\u5217\u7684\u5217\u8868\u3002<\/li>\n<li> <strong>PRAGMA TABLE_INFO() \u51fd\u6570\uff1a<\/strong> SQLite \u7279\u6709\u7684\u51fd\u6570\uff0c\u8fd4\u56de\u6709\u5173\u8868\u4e2d\u5217\u7684\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<p><strong>\u67e5\u8be2\u8868\u7684\u7ed3\u6784\u793a\u4f8b<\/strong><\/p>\n<p>\u4f7f\u7528 DESCRIBE \u547d\u4ee4\u67e5\u8be2\u540d\u4e3a &#8220;customers&#8221; \u7684\u8868\u7684\u7ed3\u6784\uff1a<\/p>\n<pre>DESCRIBE customers;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u547d\u4ee4\u5c06\u8fd4\u56de\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u5185\u5bb9\u7684\u7ed3\u679c\uff1a<\/p>\n<pre>Field | Type | Null | Key | Default | Extra\n------ | ---- | ---- | --- | ------- | -----\nid     | int    | NO   | PRI | NULL    | auto_increment\nname   | varchar | YES  |     | NULL    |\nemail  | varchar | YES  |     | NULL    |<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u8868\u660e &#8220;customers&#8221; \u8868\u5177\u6709\u4e09\u4e2a\u5217\uff1a&#8221;id&#8221;\uff08\u4e00\u4e2a\u81ea\u52a8\u9012\u589e\u7684\u6574\u6570\u4e3b\u952e\uff09\u3001&#8221;name&#8221;\uff08\u4e00\u4e2a\u53ef\u4e3a\u7a7a\u7684 VARCHAR \u5b57\u7b26\u4e32\uff09\u548c &#8220;email&#8221;\uff08\u4e00\u4e2a\u53ef\u4e3a\u7a7a\u7684 VARCHAR \u5b57\u7b26\u4e32\uff09\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u600e\u4e48\u67e5\u8be2\u8868\u7684\u7ed3\u6784\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>\u901a\u8fc7 describe\u3001information_schema \u8868\u6216 jdbc api\uff0c\u53ef\u4ee5\u67e5\u8be2\u8868\u7684\u7ed3\u6784\uff0c\u5305\u62ec\u5217\u540d\u3001\u6570\u636e\u7c7b\u578b\u3001\u7ea6\u675f\u6761\u4ef6\u7b49\u3002 \u5982\u4f55\u4f7f\u7528 SQL \u67e5\u8be2\u8868\u7684\u7ed3\u6784 SQL \u8bed\u8a00\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u67e5\u8be2\u8868\u7684\u7ed3\u6784\uff0c\u5305\u62ec\uff1a DESCRIBE \u547d\u4ee4 DESCRIBE \u547d\u4ee4\u63d0\u4f9b\u6709\u5173\u8868\u4e2d\u5217\u7684\u8be6\u7ec6\u63cf\u8ff0\uff0c\u5305\u62ec\u6570\u636e\u7c7b\u578b\u3001\u957f\u5ea6\u548c\u7ea6\u675f\u6761\u4ef6\u3002\u8bed\u6cd5\u4e3a\uff1a DESCRIBE table_name; \u767b\u5f55\u540e\u590d\u5236 INFORMATION_SCHEMA \u8868 INFORMATION_SCHEMA \u6570\u636e\u5e93\u5305\u542b\u6709\u5173\u6570\u636e\u5e93\u4e2d\u5bf9\u8c61\uff08\u5305\u62ec\u8868\uff09\u5143\u6570\u636e\u7684\u4fe1\u606f\u3002\u67e5\u8be2\u8868\u7ed3\u6784\u7684\u5e38\u7528\u8868\u662f COLUMNS \u8868\uff0c\u5b83\u5305\u542b\u6709\u5173\u8868\u4e2d\u5217\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u8bed\u6cd5\u4e3a\uff1a SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = &#8216;table_name&#8217;; \u767b\u5f55\u540e\u590d\u5236 JDBC API Java \u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684 JDBC API \u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u6cd5\u6765\u83b7\u53d6\u8868\u7ed3\u6784\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8c03\u7528 DatabaseMetaData \u63a5\u53e3\u7684 getColumns() \u65b9\u6cd5\u6216 getTable() \u65b9\u6cd5\u3002 \u5176\u4ed6\u65b9\u6cd5 SHOW COLUMNS \u547d\u4ee4\uff1a MySQL \u7279\u6709\u7684\u547d\u4ee4\uff0c\u663e\u793a\u8868\u4e2d\u5217\u7684\u5217\u8868\u3002 PRAGMA TABLE_INFO() \u51fd\u6570\uff1a SQLite [&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-5880","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5880","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=5880"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5880\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}