{"id":5685,"date":"2024-11-14T11:39:55","date_gmt":"2024-11-14T03:39:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5685\/"},"modified":"2024-11-14T11:39:55","modified_gmt":"2024-11-14T03:39:55","slug":"sql%e6%80%8e%e4%b9%88%e9%99%84%e5%8a%a0%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5685\/","title":{"rendered":"sql\u600e\u4e48\u9644\u52a0\u8868"},"content":{"rendered":"<blockquote><p>\n  \u5728 sql \u4e2d\u4f7f\u7528\u9644\u52a0\u8868\u53ef\u4ee5\u5c06\u4e24\u4e2a\u517c\u5bb9\u7684\u8868\u5408\u5e76\u5230\u4e00\u4e2a\u4e34\u65f6\u96c6\u5408\u4e2d\uff0c\u5141\u8bb8\u60a8\u67e5\u8be2\u548c\u64cd\u4f5c\u7ec4\u5408\u7684\u8868\u3002\u6b65\u9aa4\u5982\u4e0b\uff1a\u4f7f\u7528 alter table \u8bed\u53e5\uff0c\u540e\u8ddf attach \u5b50\u53e5\uff0c\u5e76\u6307\u5b9a\u8981\u9644\u52a0\u7684\u8868\u7684\u540d\u79f0\u6216\u6570\u636e\u6e90\u3002\u4f7f\u7528 pragma \u8bed\u53e5\u9a8c\u8bc1\u9644\u52a0\u662f\u5426\u6210\u529f\u3002\u9644\u52a0\u7684\u8868\u4f5c\u4e3a\u4e34\u65f6\u5bf9\u8c61\u9644\u52a0\u5230\u5f53\u524d\u6570\u636e\u5e93\uff0c\u4efb\u4f55\u66f4\u6539\u90fd\u4f1a\u540c\u6b65\u5230\u539f\u59cb\u8868\uff0c\u4f46\u4e0d\u80fd\u88ab\u4fee\u6539\u6216\u5220\u9664\uff0c\u4e5f\u4e0d\u80fd\u7528\u4f5c\u5916\u952e\u6216\u7d22\u5f15\u7684\u4e00\u90e8\u5206\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053020395620966.jpg\" class=\"aligncenter\" title=\"sql\u600e\u4e48\u9644\u52a0\u8868\u63d2\u56fe\" alt=\"sql\u600e\u4e48\u9644\u52a0\u8868\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 SQL \u9644\u52a0\u8868<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\uff0c\u9644\u52a0\u8868\u662f\u4e00\u79cd\u5c06\u4e24\u4e2a\u517c\u5bb9\u7684\u8868\u5408\u5e76\u5230\u4e00\u4e2a\u4e34\u65f6\u96c6\u5408\u4e2d\u7684\u64cd\u4f5c\u3002\u5b83\u5141\u8bb8\u60a8\u67e5\u8be2\u548c\u64cd\u4f5c\u8fd9\u4e24\u4e2a\u8868\u7684\u7ec4\u5408\uff0c\u800c\u65e0\u9700\u6c38\u4e45\u6027\u5730\u5c06\u5b83\u4eec\u5408\u5e76\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p><strong>1. \u4f7f\u7528 ALTER TABLE \u8bed\u53e5<\/strong><\/p>\n<p>\u8981\u9644\u52a0\u8868\uff0c\u8bf7\u4f7f\u7528 ALTER TABLE \u8bed\u53e5\uff0c\u540e\u8ddf ATTACH \u5b50\u53e5\u3002<\/p>\n<pre>ALTER TABLE &lt;\u8868\u540d&gt; ATTACH &lt;\u9644\u52a0\u8868\u540d&gt;;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u6307\u5b9a\u6570\u636e\u6e90<\/strong><\/p>\n<p>\u5728\u9644\u52a0\u5b50\u53e5\u4e2d\uff0c\u60a8\u9700\u8981\u6307\u5b9a\u8981\u9644\u52a0\u7684\u8868\u7684\u540d\u79f0\u6216\u6570\u636e\u6e90\u3002\u5982\u679c\u8868\u4e0d\u5728\u5f53\u524d\u6570\u636e\u5e93\u4e2d\uff0c\u8bf7\u4f7f\u7528\u5b8c\u5168\u9650\u5b9a\u8def\u5f84\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>ALTER TABLE my_table ATTACH 'C:path\toother_table.sqlite';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u9a8c\u8bc1\u9644\u52a0<\/strong><\/p>\n<p>\u9644\u52a0\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 PRAGMA \u8bed\u53e5\u68c0\u67e5\u662f\u5426\u6210\u529f\uff1a<\/p>\n<pre>PRAGMA table_info(&lt;\u8868\u540d&gt;);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u67e5\u8be2\u5c06\u663e\u793a\u9644\u52a0\u8868\u4e2d\u7684\u5217\u4fe1\u606f\uff0c\u786e\u8ba4\u9644\u52a0\u64cd\u4f5c\u5df2\u6210\u529f\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u5c06\u9644\u52a0\u540d\u4e3a &#8220;sales&#8221; \u7684\u8868\u5230 &#8220;orders&#8221; \u8868\uff1a<\/p>\n<pre>ALTER TABLE orders ATTACH sales;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\u5bf9\u8fd9\u4e24\u4e2a\u8868\u7684\u7ec4\u5408\u8fdb\u884c\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT * FROM orders UNION ALL SELECT * FROM sales;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u9644\u52a0\u7684\u8868\u5c06\u4f5c\u4e3a\u4e34\u65f6\u5bf9\u8c61\u9644\u52a0\u5230\u5f53\u524d\u6570\u636e\u5e93\uff0c\u76f4\u5230\u5f53\u524d\u8fde\u63a5\u5173\u95ed\u3002<\/li>\n<li>\u9644\u52a0\u8868\u4e2d\u7684\u4efb\u4f55\u66f4\u6539\u90fd\u4f1a\u81ea\u52a8\u540c\u6b65\u5230\u539f\u59cb\u8868\u3002<\/li>\n<li>\u9644\u52a0\u8868\u4e0d\u80fd\u88ab\u4fee\u6539\u6216\u5220\u9664\u3002<\/li>\n<li>\u9644\u52a0\u8868\u4e0d\u80fd\u4f5c\u4e3a\u5916\u952e\u6216\u7d22\u5f15\u7684\u4e00\u90e8\u5206\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u600e\u4e48\u9644\u52a0\u8868\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>\u5728 sql \u4e2d\u4f7f\u7528\u9644\u52a0\u8868\u53ef\u4ee5\u5c06\u4e24\u4e2a\u517c\u5bb9\u7684\u8868\u5408\u5e76\u5230\u4e00\u4e2a\u4e34\u65f6\u96c6\u5408\u4e2d\uff0c\u5141\u8bb8\u60a8\u67e5\u8be2\u548c\u64cd\u4f5c\u7ec4\u5408\u7684\u8868\u3002\u6b65\u9aa4\u5982\u4e0b\uff1a\u4f7f\u7528 alter table \u8bed\u53e5\uff0c\u540e\u8ddf attach \u5b50\u53e5\uff0c\u5e76\u6307\u5b9a\u8981\u9644\u52a0\u7684\u8868\u7684\u540d\u79f0\u6216\u6570\u636e\u6e90\u3002\u4f7f\u7528 pragma \u8bed\u53e5\u9a8c\u8bc1\u9644\u52a0\u662f\u5426\u6210\u529f\u3002\u9644\u52a0\u7684\u8868\u4f5c\u4e3a\u4e34\u65f6\u5bf9\u8c61\u9644\u52a0\u5230\u5f53\u524d\u6570\u636e\u5e93\uff0c\u4efb\u4f55\u66f4\u6539\u90fd\u4f1a\u540c\u6b65\u5230\u539f\u59cb\u8868\uff0c\u4f46\u4e0d\u80fd\u88ab\u4fee\u6539\u6216\u5220\u9664\uff0c\u4e5f\u4e0d\u80fd\u7528\u4f5c\u5916\u952e\u6216\u7d22\u5f15\u7684\u4e00\u90e8\u5206\u3002 \u5982\u4f55\u4f7f\u7528 SQL \u9644\u52a0\u8868 \u5728 SQL \u4e2d\uff0c\u9644\u52a0\u8868\u662f\u4e00\u79cd\u5c06\u4e24\u4e2a\u517c\u5bb9\u7684\u8868\u5408\u5e76\u5230\u4e00\u4e2a\u4e34\u65f6\u96c6\u5408\u4e2d\u7684\u64cd\u4f5c\u3002\u5b83\u5141\u8bb8\u60a8\u67e5\u8be2\u548c\u64cd\u4f5c\u8fd9\u4e24\u4e2a\u8868\u7684\u7ec4\u5408\uff0c\u800c\u65e0\u9700\u6c38\u4e45\u6027\u5730\u5c06\u5b83\u4eec\u5408\u5e76\u3002 \u6b65\u9aa4\uff1a 1. \u4f7f\u7528 ALTER TABLE \u8bed\u53e5 \u8981\u9644\u52a0\u8868\uff0c\u8bf7\u4f7f\u7528 ALTER TABLE \u8bed\u53e5\uff0c\u540e\u8ddf ATTACH \u5b50\u53e5\u3002 ALTER TABLE &lt;\u8868\u540d&gt; ATTACH &lt;\u9644\u52a0\u8868\u540d&gt;; \u767b\u5f55\u540e\u590d\u5236 2. \u6307\u5b9a\u6570\u636e\u6e90 \u5728\u9644\u52a0\u5b50\u53e5\u4e2d\uff0c\u60a8\u9700\u8981\u6307\u5b9a\u8981\u9644\u52a0\u7684\u8868\u7684\u540d\u79f0\u6216\u6570\u636e\u6e90\u3002\u5982\u679c\u8868\u4e0d\u5728\u5f53\u524d\u6570\u636e\u5e93\u4e2d\uff0c\u8bf7\u4f7f\u7528\u5b8c\u5168\u9650\u5b9a\u8def\u5f84\uff0c\u4f8b\u5982\uff1a ALTER TABLE my_table ATTACH &#8216;C:path oother_table.sqlite&#8217;; \u767b\u5f55\u540e\u590d\u5236 3. \u9a8c\u8bc1\u9644\u52a0 \u9644\u52a0\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 PRAGMA \u8bed\u53e5\u68c0\u67e5\u662f\u5426\u6210\u529f\uff1a PRAGMA table_info(&lt;\u8868\u540d&gt;); \u767b\u5f55\u540e\u590d\u5236 \u6b64\u67e5\u8be2\u5c06\u663e\u793a\u9644\u52a0\u8868\u4e2d\u7684\u5217\u4fe1\u606f\uff0c\u786e\u8ba4\u9644\u52a0\u64cd\u4f5c\u5df2\u6210\u529f\u3002 \u793a\u4f8b\uff1a \u4ee5\u4e0b\u793a\u4f8b\u5c06\u9644\u52a0\u540d\u4e3a [&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-5685","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5685","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=5685"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5685\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}