{"id":76513,"date":"2026-02-22T17:42:12","date_gmt":"2026-02-22T09:42:12","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/76513.html"},"modified":"2026-02-22T17:42:12","modified_gmt":"2026-02-22T09:42:12","slug":"%e5%88%a0%e9%99%a4%e5%88%97%e6%98%af%e7%a0%b4%e5%9d%8f%e6%80%a7%e6%93%8d%e4%bd%9c%ef%bc%8c%e6%95%b0%e6%8d%ae%e5%b0%86%e6%b0%b8%e4%b9%85%e4%b8%a2%e5%a4%b1%ef%bc%8c%e6%89%a7%e8%a1%8c%e5%89%8d","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/76513.html","title":{"rendered":"\u5220\u9664\u5217\u662f**\u7834\u574f\u6027\u64cd\u4f5c**\uff0c\u6570\u636e\u5c06\u6c38\u4e45\u4e22\u5931\uff0c\u6267\u884c\u524d\u52a1\u5fc5\u5907\u4efd"},"content":{"rendered":"<p>\u9010\u6761\u89e3\u91ca\u5e76\u7ed9\u51fa\u6807\u51c6 SQL \u8bed\u53e5\u5199\u6cd5&#xff08;\u4ee5\u6807\u51c6 SQL&#xff08;\u5982 PostgreSQL\/MySQL 8.0&#043;\/SQL Server&#xff09;\u4e3a\u57fa\u51c6&#xff0c;\u6ce8\u610f\u4e0d\u540c\u6570\u636e\u5e93\u8bed\u6cd5\u7565\u6709\u5dee\u5f02&#xff09;&#xff1a;<\/p>\n<p>\u2460 \u7b1b\u5361\u5c14\u79ef&#xff08;R \u00d7 S&#xff09;\u8f6c SQL \u8bed\u53e5&#xff1a;<br \/>\n\u7b1b\u5361\u5c14\u79ef\u5373\u4e0d\u5e26 ON \u6216 WHERE \u6761\u4ef6\u7684 JOIN&#xff0c;\u7ed3\u679c\u662f R \u8868\u6bcf\u884c\u4e0e S \u8868\u6bcf\u884c\u7ec4\u5408\u3002<br \/>\n\u2705 \u6b63\u786e\u5199\u6cd5&#xff1a;<\/p>\n<p><span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> R <span class=\"token keyword\">CROSS<\/span> <span class=\"token keyword\">JOIN<\/span> S<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">&#8212; \u6216\u7b49\u4ef7\u5199\u6cd5&#xff08;\u663e\u5f0f\u9017\u53f7\u8fde\u63a5&#xff0c;\u4f46\u4e0d\u63a8\u8350&#xff0c;\u53ef\u8bfb\u6027\u5dee&#xff09;&#xff1a;<\/span><br \/>\n<span class=\"token comment\">&#8212; SELECT * FROM R, S;<\/span><\/p>\n<p>\u26a0\ufe0f \u6ce8\u610f&#xff1a;\u201cR\u00d7S&#xff1e;&#xff1e;R&#xff0c;S\u201d \u4e2d\u7684 \u201c&#xff1e;&#xff1e;\u201d \u5e76\u975e SQL \u8fd0\u7b97\u7b26&#xff0c;\u53ef\u80fd\u662f\u6559\u6750\u4e2d\u8868\u793a\u201c\u4ea7\u751f\u7ed3\u679c\u5173\u7cfb R \u548c S \u7684\u7b1b\u5361\u5c14\u79ef\u201d\u7684\u793a\u610f\u7b26\u53f7&#xff0c;\u4e0d\u53ef\u76f4\u63a5\u5199\u5165 SQL\u3002<\/p>\n<p>\u2461 \u81ea\u7136\u8fde\u63a5&#xff08;Natural Join&#xff09;\u8f6c SQL \u8bed\u53e5&#xff1a;<br \/>\n\u81ea\u7136\u8fde\u63a5 R \u2a1d S \u8981\u6c42\u4e24\u8868\u6709\u540c\u540d\u4e14\u540c\u4e49&#xff08;\u540c\u7c7b\u578b&#xff09;\u7684\u5217&#xff0c;\u81ea\u52a8\u6309\u6240\u6709\u540c\u540d\u5217\u7b49\u503c\u8fde\u63a5\u3002<br \/>\n\u274c \u4e0d\u80fd\u7b80\u5355\u7528 AND\/OR \u66ff\u6362 \u2227\/\u2228 \u2014\u2014 \u8fd9\u662f\u903b\u8f91\u7b26\u53f7\u8bef\u7528&#xff01;<br \/>\n\u2227&#xff08;\u5408\u53d6&#xff09;\u3001\u2228&#xff08;\u6790\u53d6&#xff09;\u5c5e\u4e8e\u5173\u7cfb\u4ee3\u6570\u4e2d\u7684\u6761\u4ef6\u8fde\u63a5\u8c13\u8bcd&#xff08;\u5982 R \u2a1d_{R.A&#061;S.A \u2227 R.B&#061;S.B} S&#xff09;&#xff0c;\u6b64\u65f6\u624d\u5bf9\u5e94 SQL \u4e2d\u7684 AND&#xff1b;\u800c\u81ea\u7136\u8fde\u63a5\u672c\u8eab\u65e0\u663e\u5f0f\u8c13\u8bcd&#xff0c;\u7531\u7cfb\u7edf\u81ea\u52a8\u63a8\u5bfc\u540c\u540d\u5b57\u6bb5\u7b49\u503c\u5339\u914d\u3002<br \/>\n\u2705 \u82e5\u9700\u624b\u52a8\u6a21\u62df\u81ea\u7136\u8fde\u63a5&#xff08;\u4f8b\u5982\u5b57\u6bb5\u540d\u4e0d\u5b8c\u5168\u4e00\u81f4\u6216\u9700\u517c\u5bb9\u4e0d\u652f\u6301 NATURAL JOIN \u7684\u6570\u636e\u5e93&#xff09;&#xff0c;\u5e94\u663e\u5f0f\u5199\u51fa\u7b49\u503c\u6761\u4ef6&#xff1a;<\/p>\n<p><span class=\"token keyword\">SELECT<\/span> <span class=\"token keyword\">DISTINCT<\/span> R<span class=\"token punctuation\">.<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">,<\/span> S<span class=\"token punctuation\">.<\/span>b <span class=\"token keyword\">AS<\/span> s_b  <span class=\"token comment\">&#8212; \u6392\u9664\u91cd\u590d\u5217&#xff08;\u81ea\u7136\u8fde\u63a5\u4f1a\u53bb\u91cd\u540c\u540d\u5217&#xff09;<\/span><br \/>\n<span class=\"token keyword\">FROM<\/span> R<br \/>\n<span class=\"token keyword\">INNER<\/span> <span class=\"token keyword\">JOIN<\/span> S <span class=\"token keyword\">ON<\/span> R<span class=\"token punctuation\">.<\/span>a <span class=\"token operator\">&#061;<\/span> S<span class=\"token punctuation\">.<\/span>a <span class=\"token operator\">AND<\/span> R<span class=\"token punctuation\">.<\/span>c <span class=\"token operator\">&#061;<\/span> S<span class=\"token punctuation\">.<\/span>c<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; \u5047\u8bbe\u516c\u5171\u5217\u4e3a a, c<\/span><\/p>\n<p>&#x1f4cc; \u5173\u952e&#xff1a;\u81ea\u7136\u8fde\u63a5\u9690\u542b USING (col1, col2, &#8230;) \u884c\u4e3a&#xff0c;\u4e14\u81ea\u52a8 SELECT \u53bb\u91cd\u540e\u7684\u6240\u6709\u5217&#xff08;\u540c\u540d\u5217\u53ea\u51fa\u73b0\u4e00\u6b21&#xff09;\u3002<\/p>\n<p>\u2462 \u5220\u9664\u6216\u65b0\u5efa\u5217&#xff08;DDL \u64cd\u4f5c&#xff09;&#xff1a;<\/p>\n<ul>\n<li>\u2705 \u65b0\u589e\u5217&#xff08;\u6807\u51c6 SQL&#xff09;&#xff1a;<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> R <span class=\"token keyword\">ADD<\/span> <span class=\"token keyword\">COLUMN<\/span> new_col <span class=\"token keyword\">INT<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span>\n<\/li>\n<li>\u2705 \u5220\u9664\u5217&#xff08;\u6ce8\u610f&#xff1a;MySQL 8.0&#043;\u3001PostgreSQL\u3001SQL Server \u652f\u6301&#xff1b;SQLite \u65e7\u7248\u4e0d\u652f\u6301\u76f4\u63a5 DROP COLUMN&#xff0c;\u9700\u91cd\u5efa\u8868&#xff09;&#xff1a;<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> R <span class=\"token keyword\">DROP<\/span> <span class=\"token keyword\">COLUMN<\/span> old_col<span class=\"token punctuation\">;<\/span>\n<\/li>\n<\/ul>\n<p>\u26a0\ufe0f \u8865\u5145\u8bf4\u660e&#xff1a;<\/p>\n<ul>\n<li>\u5220\u9664\u5217\u662f\u7834\u574f\u6027\u64cd\u4f5c&#xff0c;\u6570\u636e\u5c06\u6c38\u4e45\u4e22\u5931&#xff0c;\u6267\u884c\u524d\u52a1\u5fc5\u5907\u4efd&#xff1b;<\/li>\n<li>\u65b0\u589e\u5217\u82e5\u542b NOT NULL \u4e14\u8868\u975e\u7a7a&#xff0c;\u9700\u6307\u5b9a DEFAULT \u503c&#xff08;\u5426\u5219\u62a5\u9519&#xff09;&#xff1b;<\/li>\n<li>\u5404\u6570\u636e\u5e93\u5bf9 ALTER TABLE &#8230; DROP COLUMN \u517c\u5bb9\u6027\u4e0d\u540c&#xff08;\u5982 MySQL 5.7 \u4e0d\u652f\u6301&#xff0c;\u9700 \u22658.0&#xff09;\u3002<\/li>\n<\/ul>\n<p><span class=\"token comment\">&#8212; \u793a\u4f8b&#xff1a;\u5b8c\u6574\u6f14\u793a<\/span><br \/>\n<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> employees <span class=\"token keyword\">ADD<\/span> <span class=\"token keyword\">COLUMN<\/span> hire_year <span class=\"token keyword\">INT<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token keyword\">YEAR<\/span><span class=\"token punctuation\">(<\/span>CURDATE<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> employees <span class=\"token keyword\">DROP<\/span> <span class=\"token keyword\">COLUMN<\/span> temp_flag<span class=\"token punctuation\">;<\/span><\/p>\n<p>\u5728 SQLite&#xff08;\u7248\u672c &lt; 3.35.0&#xff0c;\u5373 2021 \u5e74\u524d\u7684\u65e7\u7248&#xff09; \u4e2d&#xff0c;ALTER TABLE &#8230; DROP COLUMN \u8bed\u53e5\u4e0d\u88ab\u652f\u6301&#xff08;\u76f4\u5230 SQLite 3.35.0 \u624d\u539f\u751f\u5f15\u5165 DROP COLUMN&#xff09;\u3002\u56e0\u6b64&#xff0c;\u9700\u901a\u8fc7\u201c\u91cd\u5efa\u8868\u201d\u65b9\u5f0f\u5b89\u5168\u5220\u9664\u5217\u3002\u8be5\u8fc7\u7a0b\u867d\u7a0d\u7e41\u7410&#xff0c;\u4f46\u53ef\u4fdd\u8bc1\u6570\u636e\u5b8c\u6574\u6027\u4e0e\u4e8b\u52a1\u5b89\u5168\u6027&#xff08;\u82e5\u5728\u4e8b\u52a1\u4e2d\u6267\u884c&#xff09;\u3002<\/p>\n<p>\u2705 \u6807\u51c6\u5b89\u5168\u64cd\u4f5c\u6b65\u9aa4&#xff08;\u63a8\u8350&#xff09;&#xff1a;<\/p>\n<p><span class=\"token comment\">&#8212; 1. \u5f00\u542f\u4e8b\u52a1&#xff08;\u786e\u4fdd\u539f\u5b50\u6027&#xff0c;\u5931\u8d25\u53ef\u56de\u6eda&#xff09;<\/span><br \/>\n<span class=\"token keyword\">BEGIN<\/span> <span class=\"token keyword\">TRANSACTION<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; 2. \u91cd\u547d\u540d\u539f\u8868&#xff08;\u907f\u514d\u51b2\u7a81&#xff09;<\/span><br \/>\n<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> R <span class=\"token keyword\">RENAME<\/span> <span class=\"token keyword\">TO<\/span> R_old<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; 3. \u521b\u5efa\u65b0\u8868 R&#xff0c;\u7ed3\u6784\u4e2d**\u7701\u7565\u8981\u5220\u9664\u7684\u5217**&#xff08;\u4ec5\u5305\u542b\u4fdd\u7559\u7684\u5217&#xff0c;\u542b\u7ea6\u675f\u3001\u4e3b\u952e\u3001NOT NULL \u7b49&#xff09;<\/span><br \/>\n<span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">TABLE<\/span> R <span class=\"token punctuation\">(<\/span><br \/>\n    id <span class=\"token keyword\">INTEGER<\/span> <span class=\"token keyword\">PRIMARY<\/span> <span class=\"token keyword\">KEY<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    name <span class=\"token keyword\">TEXT<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    age <span class=\"token keyword\">INTEGER<\/span><br \/>\n    <span class=\"token comment\">&#8212; \u2705 \u5047\u8bbe\u539f\u8868\u6709\u5217&#xff1a;id, name, age, deleted_flag&#xff1b;\u6b64\u5904\u5df2\u7701\u7565 deleted_flag<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; 4. \u5c06\u65e7\u8868\u6570\u636e**\u6309\u65b0\u5217\u987a\u5e8f\u63d2\u5165**&#xff08;\u663e\u5f0f\u6307\u5b9a\u5217\u540d&#xff0c;\u907f\u514d\u987a\u5e8f\u9519\u4f4d&#xff09;<\/span><br \/>\n<span class=\"token keyword\">INSERT<\/span> <span class=\"token keyword\">INTO<\/span> R <span class=\"token punctuation\">(<\/span>id<span class=\"token punctuation\">,<\/span> name<span class=\"token punctuation\">,<\/span> age<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">SELECT<\/span> id<span class=\"token punctuation\">,<\/span> name<span class=\"token punctuation\">,<\/span> age <span class=\"token keyword\">FROM<\/span> R_old<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; 5. &#xff08;\u53ef\u9009&#xff09;\u9a8c\u8bc1\u6570\u636e\u4e00\u81f4\u6027&#xff08;\u5982\u884c\u6570\u3001\u5173\u952e\u503c\u6821\u9a8c&#xff09;<\/span><br \/>\n<span class=\"token comment\">&#8212; SELECT COUNT(*) FROM R; SELECT COUNT(*) FROM R_old;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; 6. \u5220\u9664\u65e7\u8868<\/span><br \/>\n<span class=\"token keyword\">DROP<\/span> <span class=\"token keyword\">TABLE<\/span> R_old<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; 7. \u63d0\u4ea4\u4e8b\u52a1<\/span><br \/>\n<span class=\"token keyword\">COMMIT<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u26a0\ufe0f \u5173\u952e\u6ce8\u610f\u4e8b\u9879&#xff1a;<\/p>\n<ul>\n<li>\u5fc5\u987b\u663e\u5f0f\u5217\u51fa INSERT INTO &#8230; SELECT \u7684\u5217\u540d&#xff08;\u4e0d\u53ef\u7528 SELECT *&#xff09;&#xff0c;\u5426\u5219\u56e0\u5217\u6570\/\u987a\u5e8f\u4e0d\u5339\u914d\u5bfc\u81f4\u9519\u8bef\u6216\u6570\u636e\u9519\u4f4d&#xff1b;<\/li>\n<li>\u9700\u624b\u52a8\u91cd\u5efa\u6240\u6709\u7ea6\u675f&#xff08;PRIMARY KEY, UNIQUE, CHECK, FOREIGN KEY&#xff09;\u3001\u7d22\u5f15\u3001\u89e6\u53d1\u5668\u2014\u2014\u5b83\u4eec\u4e0d\u4f1a\u81ea\u52a8\u8fc1\u79fb&#xff0c;\u9700\u5728 CREATE TABLE \u540e\u5355\u72ec\u6dfb\u52a0&#xff1b;<\/li>\n<li>\u82e5\u539f\u8868\u6709 AUTOINCREMENT \u4e3b\u952e&#xff0c;\u65b0\u8868\u5b9a\u4e49\u4e2d\u9700\u4fdd\u7559 INTEGER PRIMARY KEY AUTOINCREMENT&#xff1b;<\/li>\n<li>\u5916\u952e\u7ea6\u675f\u9700\u63d0\u524d PRAGMA foreign_keys &#061; OFF;&#xff08;\u64cd\u4f5c\u671f\u95f4\u7981\u7528\u5916\u952e\u68c0\u67e5&#xff09;&#xff0c;\u64cd\u4f5c\u5b8c\u6210\u540e\u518d\u5f00\u542f&#xff08;SQLite \u9ed8\u8ba4\u5173\u95ed\u5916\u952e&#xff0c;\u9700\u663e\u5f0f\u542f\u7528&#xff09;&#xff1b;<\/li>\n<li>\u5efa\u8bae\u5148\u5728\u6d4b\u8bd5\u5e93\u9a8c\u8bc1&#xff0c;\u518d\u64cd\u4f5c\u751f\u4ea7\u6570\u636e\u3002<\/li>\n<\/ul>\n<p>&#x1f527; \u81ea\u52a8\u5316\u8f85\u52a9&#xff08;Python \u793a\u4f8b\u7247\u6bb5&#xff09;&#xff1a;<\/p>\n<p><span class=\"token keyword\">import<\/span> sqlite3<br \/>\nconn <span class=\"token operator\">&#061;<\/span> sqlite3<span class=\"token punctuation\">.<\/span>connect<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;db.sqlite&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\nconn<span class=\"token punctuation\">.<\/span>execute<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;PRAGMA foreign_keys &#061; OFF&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u2026\u2026 \u6267\u884c\u4e0a\u8ff0 DDL \u6b65\u9aa4<\/span><br \/>\nconn<span class=\"token punctuation\">.<\/span>execute<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;PRAGMA foreign_keys &#061; ON&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\nconn<span class=\"token punctuation\">.<\/span>commit<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>\u2705 SQLite 3.35.0&#043; \u7528\u6237\u53ef\u76f4\u63a5\u4f7f\u7528&#xff08;\u66f4\u7b80\u6d01\u5b89\u5168&#xff09;&#xff1a;<\/p>\n<p><span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> R <span class=\"token keyword\">DROP<\/span> <span class=\"token keyword\">COLUMN<\/span> deleted_flag<span class=\"token punctuation\">;<\/span><\/p>\n<p>&#xff08;\u4f46\u4ecd\u5efa\u8bae\u5907\u4efd\u540e\u64cd\u4f5c&#xff09;<br \/>\n\u5728 SQLite \u91cd\u5efa\u8868&#xff08;\u5982\u5220\u9664\u5217&#xff09;\u8fc7\u7a0b\u4e2d&#xff0c;\u7d22\u5f15&#xff08;INDEX&#xff09;\u3001\u89e6\u53d1\u5668&#xff08;TRIGGER&#xff09;\u3001\u89c6\u56fe&#xff08;VIEW&#xff09;\u548c\u5916\u952e\u7ea6\u675f&#xff08;FOREIGN KEY&#xff09;\u5747\u4e0d\u4f1a\u81ea\u52a8\u8fc1\u79fb\u2014\u2014\u5b83\u4eec\u4ecd\u7ed1\u5b9a\u5728\u65e7\u8868\u540d\u4e0a&#xff0c;\u4e14 CREATE TABLE \u4e0d\u4f1a\u590d\u5236\u8fd9\u4e9b\u5bf9\u8c61\u3002\u56e0\u6b64&#xff0c;\u5fc5\u987b\u624b\u52a8\u5bfc\u51fa\u5b9a\u4e49\u3001\u4fee\u6539\u8868\u540d\u5f15\u7528\u3001\u518d\u91cd\u65b0\u521b\u5efa\u3002\u867d\u7136 SQLite \u6ca1\u6709\u5185\u7f6e\u201c\u4e00\u952e\u8fc1\u79fb\u201d\u547d\u4ee4&#xff0c;\u4f46\u53ef\u901a\u8fc7\u7cfb\u7edf\u8868 sqlite_master \u81ea\u52a8\u5316\u63d0\u53d6\u5e76\u91cd\u5199 DDL&#xff0c;\u5b9e\u73b0\u5b89\u5168\u91cd\u5efa\u3002<\/p>\n<p>\u2705 \u5b8c\u6574\u81ea\u52a8\u5316\u6d41\u7a0b&#xff08;\u542b\u7d22\u5f15\u3001\u89e6\u53d1\u5668\u3001\u5916\u952e\u3001\u552f\u4e00\u7ea6\u675f\u7b49&#xff09;&#xff1a;<\/p>\n<p><span class=\"token comment\">&#8212; \u2705 \u6b65\u9aa4 0&#xff1a;\u542f\u7528\u5916\u952e\u652f\u6301&#xff08;\u82e5\u6d89\u53ca\u5916\u952e&#xff09;<\/span><br \/>\nPRAGMA foreign_keys <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">OFF<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u2705 \u6b65\u9aa4 1&#xff1a;\u5907\u4efd\u539f\u8868\u5e76\u91cd\u547d\u540d<\/span><br \/>\n<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> R <span class=\"token keyword\">RENAME<\/span> <span class=\"token keyword\">TO<\/span> R_old<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u2705 \u6b65\u9aa4 2&#xff1a;\u521b\u5efa\u65b0\u8868 R&#xff08;\u4ec5\u542b\u9700\u4fdd\u7559\u7684\u5217&#xff0c;\u7ed3\u6784\u7cbe\u786e&#xff09;<\/span><br \/>\n<span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">TABLE<\/span> R <span class=\"token punctuation\">(<\/span><br \/>\n    id <span class=\"token keyword\">INTEGER<\/span> <span class=\"token keyword\">PRIMARY<\/span> <span class=\"token keyword\">KEY<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    name <span class=\"token keyword\">TEXT<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    age <span class=\"token keyword\">INTEGER<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token keyword\">CHECK<\/span><span class=\"token punctuation\">(<\/span>age <span class=\"token operator\">&gt;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u2705 \u6b65\u9aa4 3&#xff1a;\u8fc1\u79fb\u6570\u636e&#xff08;\u663e\u5f0f\u5217\u6620\u5c04&#xff09;<\/span><br \/>\n<span class=\"token keyword\">INSERT<\/span> <span class=\"token keyword\">INTO<\/span> R <span class=\"token punctuation\">(<\/span>id<span class=\"token punctuation\">,<\/span> name<span class=\"token punctuation\">,<\/span> age<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">SELECT<\/span> id<span class=\"token punctuation\">,<\/span> name<span class=\"token punctuation\">,<\/span> age <span class=\"token keyword\">FROM<\/span> R_old<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u2705 \u6b65\u9aa4 4&#xff1a;\u81ea\u52a8\u91cd\u5efa\u6240\u6709\u76f8\u5173\u5bf9\u8c61&#xff08;\u6838\u5fc3&#xff01;&#xff09;<\/span><br \/>\n<span class=\"token comment\">&#8212; \u25b6\ufe0f \u91cd\u5efa\u7d22\u5f15&#xff08;\u66ff\u6362\u8868\u540d R_old \u2192 R&#xff0c;\u4fdd\u7559\u552f\u4e00\u6027\/\u6392\u5e8f\u7b49\u5c5e\u6027&#xff09;<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token string\">&#039;CREATE &#039;<\/span> <span class=\"token operator\">||<\/span><br \/>\n       <span class=\"token keyword\">CASE<\/span> <span class=\"token keyword\">WHEN<\/span> <span class=\"token keyword\">sql<\/span> <span class=\"token operator\">LIKE<\/span> <span class=\"token string\">&#039;CREATE UNIQUE%&#039;<\/span> <span class=\"token keyword\">THEN<\/span> <span class=\"token string\">&#039;UNIQUE &#039;<\/span> <span class=\"token keyword\">ELSE<\/span> <span class=\"token string\">&#039;&#039;<\/span> <span class=\"token keyword\">END<\/span> <span class=\"token operator\">||<\/span><br \/>\n       <span class=\"token string\">&#039;INDEX &#039;<\/span> <span class=\"token operator\">||<\/span><br \/>\n       SUBSTR<span class=\"token punctuation\">(<\/span><span class=\"token keyword\">sql<\/span><span class=\"token punctuation\">,<\/span> INSTR<span class=\"token punctuation\">(<\/span><span class=\"token keyword\">sql<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;ON &#039;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">3<\/span><span class=\"token punctuation\">,<\/span> INSTR<span class=\"token punctuation\">(<\/span><span class=\"token keyword\">sql<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;(&#039;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#8211;<\/span> INSTR<span class=\"token punctuation\">(<\/span><span class=\"token keyword\">sql<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;ON &#039;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">3<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">||<\/span><br \/>\n       <span class=\"token string\">&#039; ON R&#039;<\/span> <span class=\"token operator\">||<\/span><br \/>\n       SUBSTR<span class=\"token punctuation\">(<\/span><span class=\"token keyword\">sql<\/span><span class=\"token punctuation\">,<\/span> INSTR<span class=\"token punctuation\">(<\/span><span class=\"token keyword\">sql<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;(&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">||<\/span> <span class=\"token string\">&#039;;&#039;<\/span><br \/>\n<span class=\"token keyword\">FROM<\/span> sqlite_master<br \/>\n<span class=\"token keyword\">WHERE<\/span> <span class=\"token keyword\">type<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;index&#039;<\/span> <span class=\"token operator\">AND<\/span> tbl_name <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;R_old&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u25b6\ufe0f \u91cd\u5efa\u89e6\u53d1\u5668&#xff08;\u5c06\u89e6\u53d1\u5668\u4e2d\u5bf9 R_old \u7684\u5f15\u7528\u66ff\u6362\u4e3a R&#xff09;<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token keyword\">REPLACE<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">sql<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;CREATE TRIGGER&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;CREATE TRIGGER&#039;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">||<\/span><br \/>\n       <span class=\"token keyword\">REPLACE<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">REPLACE<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">sql<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;R_old&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;R&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;ON R_old&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;ON R&#039;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">||<\/span> <span class=\"token string\">&#039;;&#039;<\/span><br \/>\n<span class=\"token keyword\">FROM<\/span> sqlite_master<br \/>\n<span class=\"token keyword\">WHERE<\/span> <span class=\"token keyword\">type<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;trigger&#039;<\/span> <span class=\"token operator\">AND<\/span> tbl_name <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;R_old&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u25b6\ufe0f \u91cd\u5efa\u5916\u952e\u7ea6\u675f&#xff08;\u9700\u5728 CREATE TABLE \u540e\u7528 ALTER TABLE ADD CONSTRAINT&#xff0c;\u4f46 SQLite \u4e0d\u652f\u6301&#xff01;\u2192 \u6539\u7528 PRAGMA foreign_key_list \u6216\u624b\u52a8\u68c0\u67e5&#xff09;<\/span><br \/>\n<span class=\"token comment\">&#8212; \u26a0\ufe0f \u6ce8\u610f&#xff1a;SQLite \u4e0d\u652f\u6301 ALTER TABLE ADD FOREIGN KEY&#xff0c;\u56e0\u6b64\u5916\u952e\u5fc5\u987b\u5728 CREATE TABLE \u65f6\u5b9a\u4e49&#xff08;\u89c1\u6b65\u9aa42&#xff09;&#xff0c;\u6216\u901a\u8fc7 PRAGMA defer_foreign_keys \u5ef6\u8fdf\u68c0\u67e5\u3002<\/span><\/p>\n<p>&#x1f4cc; \u66f4\u53ef\u9760\u505a\u6cd5&#xff08;\u63a8\u8350\u811a\u672c\u5316&#xff09;&#xff1a;<br \/>\n\u4f7f\u7528 Python &#043; sqlite3 \u6a21\u5757\u81ea\u52a8\u5b8c\u6210&#xff08;\u751f\u4ea7\u73af\u5883\u9996\u9009&#xff09;&#xff1a;<\/p>\n<p><span class=\"token keyword\">import<\/span> sqlite3<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">rebuild_table_drop_column<\/span><span class=\"token punctuation\">(<\/span>db_path<span class=\"token punctuation\">,<\/span> table_name<span class=\"token punctuation\">,<\/span> drop_columns<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    conn <span class=\"token operator\">&#061;<\/span> sqlite3<span class=\"token punctuation\">.<\/span>connect<span class=\"token punctuation\">(<\/span>db_path<span class=\"token punctuation\">)<\/span><br \/>\n    conn<span class=\"token punctuation\">.<\/span>execute<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;PRAGMA foreign_keys &#061; OFF&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    conn<span class=\"token punctuation\">.<\/span>execute<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;BEGIN TRANSACTION&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token keyword\">try<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token comment\"># 1. \u83b7\u53d6\u539f\u8868\u7ed3\u6784&#xff08;\u5217\u540d\u3001\u7c7b\u578b\u3001\u7ea6\u675f&#xff09;<\/span><br \/>\n        cursor <span class=\"token operator\">&#061;<\/span> conn<span class=\"token punctuation\">.<\/span>execute<span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;PRAGMA table_info(<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>table_name<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">)&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n        cols <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">(<\/span>row<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> row<span class=\"token punctuation\">[<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> row<span class=\"token punctuation\">[<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> row<span class=\"token punctuation\">[<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">for<\/span> row <span class=\"token keyword\">in<\/span> cursor<span class=\"token punctuation\">.<\/span>fetchall<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">]<\/span>  <span class=\"token comment\"># name,type,notnull,dflt_value<\/span><br \/>\n        keep_cols <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">[<\/span>c <span class=\"token keyword\">for<\/span> c <span class=\"token keyword\">in<\/span> cols <span class=\"token keyword\">if<\/span> c<span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token keyword\">not<\/span> <span class=\"token keyword\">in<\/span> drop_columns<span class=\"token punctuation\">]<\/span><\/p>\n<p>        <span class=\"token comment\"># 2. \u6784\u5efa\u65b0 CREATE TABLE \u8bed\u53e5&#xff08;\u542b\u4e3b\u952e\u3001NOT NULL\u3001DEFAULT\u3001CHECK&#xff09;<\/span><br \/>\n        col_defs <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><br \/>\n        pk_col <span class=\"token operator\">&#061;<\/span> <span class=\"token boolean\">None<\/span><br \/>\n        <span class=\"token keyword\">for<\/span> name<span class=\"token punctuation\">,<\/span> typ<span class=\"token punctuation\">,<\/span> notnull<span class=\"token punctuation\">,<\/span> dflt <span class=\"token keyword\">in<\/span> keep_cols<span class=\"token punctuation\">:<\/span><br \/>\n            s <span class=\"token operator\">&#061;<\/span> <span class=\"token string-interpolation\"><span class=\"token string\">f&#034;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>name<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>typ<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><br \/>\n            <span class=\"token keyword\">if<\/span> notnull<span class=\"token punctuation\">:<\/span> s <span class=\"token operator\">&#043;&#061;<\/span> <span class=\"token string\">&#034; NOT NULL&#034;<\/span><br \/>\n            <span class=\"token keyword\">if<\/span> dflt <span class=\"token keyword\">is<\/span> <span class=\"token keyword\">not<\/span> <span class=\"token boolean\">None<\/span> <span class=\"token keyword\">and<\/span> dflt <span class=\"token operator\">!&#061;<\/span> <span class=\"token string\">&#039;&#039;<\/span><span class=\"token punctuation\">:<\/span> s <span class=\"token operator\">&#043;&#061;<\/span> <span class=\"token string-interpolation\"><span class=\"token string\">f&#034; DEFAULT <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>dflt<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><br \/>\n            col_defs<span class=\"token punctuation\">.<\/span>append<span class=\"token punctuation\">(<\/span>s<span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token keyword\">if<\/span> typ<span class=\"token punctuation\">.<\/span>upper<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#039;INTEGER&#039;<\/span> <span class=\"token keyword\">and<\/span> <span class=\"token string\">&#039;PRIMARY KEY&#039;<\/span> <span class=\"token keyword\">in<\/span> <span class=\"token punctuation\">(<\/span>dflt <span class=\"token keyword\">or<\/span> <span class=\"token string\">&#039;&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n                pk_col <span class=\"token operator\">&#061;<\/span> name<br \/>\n        <span class=\"token comment\"># \u7b80\u5355\u4e3b\u952e\u5904\u7406&#xff08;\u5b9e\u9645\u9700\u89e3\u6790 PRAGMA index_list \/ index_info&#xff09;<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> pk_col<span class=\"token punctuation\">:<\/span><br \/>\n            col_defs<span class=\"token punctuation\">.<\/span>append<span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;PRIMARY KEY(<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>pk_col<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">)&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>        create_sql <span class=\"token operator\">&#061;<\/span> <span class=\"token string-interpolation\"><span class=\"token string\">f&#034;CREATE TABLE <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>table_name<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> (<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span><span class=\"token string\">&#039;, &#039;<\/span><span class=\"token punctuation\">.<\/span>join<span class=\"token punctuation\">(<\/span>col_defs<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">);&#034;<\/span><\/span><br \/>\n        conn<span class=\"token punctuation\">.<\/span>execute<span class=\"token punctuation\">(<\/span>create_sql<span class=\"token punctuation\">)<\/span><\/p>\n<p>        <span class=\"token comment\"># 3. \u63d2\u5165\u6570\u636e&#xff08;\u663e\u5f0f\u5217\u540d&#xff09;<\/span><br \/>\n        keep_names <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">[<\/span>c<span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token keyword\">for<\/span> c <span class=\"token keyword\">in<\/span> keep_cols<span class=\"token punctuation\">]<\/span><br \/>\n        insert_sql <span class=\"token operator\">&#061;<\/span> <span class=\"token string-interpolation\"><span class=\"token string\">f&#034;INSERT INTO <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>table_name<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> (<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span><span class=\"token string\">&#039;, &#039;<\/span><span class=\"token punctuation\">.<\/span>join<span class=\"token punctuation\">(<\/span>keep_names<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">) SELECT <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span><span class=\"token string\">&#039;, &#039;<\/span><span class=\"token punctuation\">.<\/span>join<span class=\"token punctuation\">(<\/span>keep_names<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> FROM <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>table_name<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">_old;&#034;<\/span><\/span><br \/>\n        conn<span class=\"token punctuation\">.<\/span>execute<span class=\"token punctuation\">(<\/span>insert_sql<span class=\"token punctuation\">)<\/span><\/p>\n<p>        <span class=\"token comment\"># 4. \u81ea\u52a8\u91cd\u5efa\u7d22\u5f15 &amp; \u89e6\u53d1\u5668&#xff08;\u4ece sqlite_master \u63d0\u53d6\u5e76\u91cd\u5199&#xff09;<\/span><br \/>\n        <span class=\"token keyword\">for<\/span> row <span class=\"token keyword\">in<\/span> conn<span class=\"token punctuation\">.<\/span>execute<span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;SELECT type, name, sql FROM sqlite_master WHERE tbl_name&#061;&#039;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>table_name<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">_old&#039; AND type IN (&#039;index&#039;,&#039;trigger&#039;)&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            obj_type<span class=\"token punctuation\">,<\/span> obj_name<span class=\"token punctuation\">,<\/span> sql <span class=\"token operator\">&#061;<\/span> row<br \/>\n            <span class=\"token keyword\">if<\/span> <span class=\"token keyword\">not<\/span> sql<span class=\"token punctuation\">:<\/span> <span class=\"token keyword\">continue<\/span><br \/>\n            <span class=\"token comment\"># \u66ff\u6362\u65e7\u8868\u540d\u4e3a\u65b0\u8868\u540d&#xff08;\u8c28\u614e\u5904\u7406\u5f15\u53f7\u548c\u4e0a\u4e0b\u6587&#xff09;<\/span><br \/>\n            new_sql <span class=\"token operator\">&#061;<\/span> sql<span class=\"token punctuation\">.<\/span>replace<span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;&#096;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>table_name<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">_old&#096;&#034;<\/span><\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string-interpolation\"><span class=\"token string\">f&#034;&#096;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>table_name<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#096;&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>replace<span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#039;&#034;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>table_name<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">_old&#034;&#039;<\/span><\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string-interpolation\"><span class=\"token string\">f&#039;&#034;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>table_name<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;&#039;<\/span><\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>replace<span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>table_name<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">_old&#034;<\/span><\/span><span class=\"token punctuation\">,<\/span> table_name<span class=\"token punctuation\">)<\/span><br \/>\n            conn<span class=\"token punctuation\">.<\/span>execute<span class=\"token punctuation\">(<\/span>new_sql<span class=\"token punctuation\">)<\/span><\/p>\n<p>        conn<span class=\"token punctuation\">.<\/span>execute<span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;DROP TABLE <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>table_name<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">_old&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n        conn<span class=\"token punctuation\">.<\/span>execute<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;COMMIT&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token keyword\">except<\/span> Exception <span class=\"token keyword\">as<\/span> e<span class=\"token punctuation\">:<\/span><br \/>\n        conn<span class=\"token punctuation\">.<\/span>execute<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;ROLLBACK&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">raise<\/span> e<br \/>\n    <span class=\"token keyword\">finally<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        conn<span class=\"token punctuation\">.<\/span>close<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>&#x1f4a1; \u5173\u952e\u6280\u5de7&#xff1a;<\/p>\n<ul>\n<li>\u4f7f\u7528 PRAGMA table_info(tab) \u83b7\u53d6\u5217\u5143\u6570\u636e&#xff08;\u542b pk \u6807\u5fd7&#xff09;&#xff1b;<\/li>\n<li>\u4f7f\u7528 PRAGMA index_list(tab) &#043; PRAGMA index_info(idx) \u83b7\u53d6\u7d22\u5f15\u5217\u987a\u5e8f\u4e0e\u552f\u4e00\u6027&#xff1b;<\/li>\n<li>\u89e6\u53d1\u5668\u4e2d\u7684 NEW\/OLD \u5f15\u7528\u65e0\u9700\u4fee\u6539&#xff0c;\u53ea\u6539 ON \u8868\u540d&#xff1b;<\/li>\n<li>\u6c38\u8fdc\u4e0d\u8981\u76f4\u63a5 REPLACE(sql, &#039;R_old&#039;, &#039;R&#039;) \u2014\u2014 \u53ef\u80fd\u8bef\u66ff\u6362\u5217\u540d\u6216\u5b57\u7b26\u4e32\u5b57\u9762\u91cf&#xff0c;\u5e94\u6b63\u5219\u6216\u6309\u8bed\u6cd5\u4f4d\u7f6e\u66ff\u6362\u3002<\/li>\n<\/ul>\n<p>\u2705 \u6700\u7ec8\u9a8c\u8bc1&#xff1a;<\/p>\n<p><span class=\"token comment\">&#8212; \u68c0\u67e5\u65b0\u8868\u7ed3\u6784<\/span><br \/>\nPRAGMA table_info<span class=\"token punctuation\">(<\/span>R<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">&#8212; \u68c0\u67e5\u7d22\u5f15\u662f\u5426\u751f\u6548<\/span><br \/>\nPRAGMA index_list<span class=\"token punctuation\">(<\/span>R<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">&#8212; \u68c0\u67e5\u89e6\u53d1\u5668\u662f\u5426\u5b58\u5728<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> name <span class=\"token keyword\">FROM<\/span> sqlite_master <span class=\"token keyword\">WHERE<\/span> <span class=\"token keyword\">type<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;trigger&#039;<\/span> <span class=\"token operator\">AND<\/span> tbl_name<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;R&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260222094210-699acf727d750.jpg\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9010\u6761\u89e3\u91ca\u5e76\u7ed9\u51fa\u6807\u51c6 SQL \u8bed\u53e5\u5199\u6cd5&#xff08;\u4ee5\u6807\u51c6 SQL&#xff08;\u5982 PostgreSQL\/MySQL 8.0\/SQL Server&#xff09;\u4e3a\u57fa\u51c6&#xff0c;\u6ce8\u610f\u4e0d\u540c\u6570\u636e\u5e93\u8bed\u6cd5\u7565\u6709\u5dee\u5f02&#xff09;&#xff1a;<br \/>\n\u2460 \u7b1b\u5361\u5c14\u79ef&#xff08;R \u00d7 S&#xff09;\u8f6c SQL \u8bed\u53e5&#xff1a;<br \/>\n\u7b1b\u5361\u5c14\u79ef\u5373\u4e0d\u5e26 ON \u6216 WHERE \u6761\u4ef6\u7684 JOIN&#xff0c;\u7ed3\u679c\u662f R \u8868\u6bcf\u884c\u4e0e S \u8868\u6bcf\u884c\u7ec4\u5408\u3002<br \/>\n\u2705 \u6b63\u786e\u5199\u6cd5&#xff1a;<br \/>\nSEL<\/p>\n","protected":false},"author":2,"featured_media":76512,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[426,604,100],"topic":[],"class_list":["post-76513","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-jvm","tag-oracle","tag-100"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u5220\u9664\u5217\u662f**\u7834\u574f\u6027\u64cd\u4f5c**\uff0c\u6570\u636e\u5c06\u6c38\u4e45\u4e22\u5931\uff0c\u6267\u884c\u524d\u52a1\u5fc5\u5907\u4efd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/76513.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u5220\u9664\u5217\u662f**\u7834\u574f\u6027\u64cd\u4f5c**\uff0c\u6570\u636e\u5c06\u6c38\u4e45\u4e22\u5931\uff0c\u6267\u884c\u524d\u52a1\u5fc5\u5907\u4efd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u9010\u6761\u89e3\u91ca\u5e76\u7ed9\u51fa\u6807\u51c6 SQL \u8bed\u53e5\u5199\u6cd5&#xff08;\u4ee5\u6807\u51c6 SQL&#xff08;\u5982 PostgreSQL\/MySQL 8.0\/SQL Server&#xff09;\u4e3a\u57fa\u51c6&#xff0c;\u6ce8\u610f\u4e0d\u540c\u6570\u636e\u5e93\u8bed\u6cd5\u7565\u6709\u5dee\u5f02&#xff09;&#xff1a; \u2460 \u7b1b\u5361\u5c14\u79ef&#xff08;R \u00d7 S&#xff09;\u8f6c SQL \u8bed\u53e5&#xff1a; \u7b1b\u5361\u5c14\u79ef\u5373\u4e0d\u5e26 ON \u6216 WHERE \u6761\u4ef6\u7684 JOIN&#xff0c;\u7ed3\u679c\u662f R \u8868\u6bcf\u884c\u4e0e S \u8868\u6bcf\u884c\u7ec4\u5408\u3002 \u2705 \u6b63\u786e\u5199\u6cd5&#xff1a; SEL\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/76513.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-22T09:42:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260222094210-699acf727d750.jpg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/76513.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/76513.html\",\"name\":\"\u5220\u9664\u5217\u662f**\u7834\u574f\u6027\u64cd\u4f5c**\uff0c\u6570\u636e\u5c06\u6c38\u4e45\u4e22\u5931\uff0c\u6267\u884c\u524d\u52a1\u5fc5\u5907\u4efd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-22T09:42:12+00:00\",\"dateModified\":\"2026-02-22T09:42:12+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/76513.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/76513.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/76513.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u5220\u9664\u5217\u662f**\u7834\u574f\u6027\u64cd\u4f5c**\uff0c\u6570\u636e\u5c06\u6c38\u4e45\u4e22\u5931\uff0c\u6267\u884c\u524d\u52a1\u5fc5\u5907\u4efd\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u5220\u9664\u5217\u662f**\u7834\u574f\u6027\u64cd\u4f5c**\uff0c\u6570\u636e\u5c06\u6c38\u4e45\u4e22\u5931\uff0c\u6267\u884c\u524d\u52a1\u5fc5\u5907\u4efd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/76513.html","og_locale":"zh_CN","og_type":"article","og_title":"\u5220\u9664\u5217\u662f**\u7834\u574f\u6027\u64cd\u4f5c**\uff0c\u6570\u636e\u5c06\u6c38\u4e45\u4e22\u5931\uff0c\u6267\u884c\u524d\u52a1\u5fc5\u5907\u4efd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u9010\u6761\u89e3\u91ca\u5e76\u7ed9\u51fa\u6807\u51c6 SQL \u8bed\u53e5\u5199\u6cd5&#xff08;\u4ee5\u6807\u51c6 SQL&#xff08;\u5982 PostgreSQL\/MySQL 8.0\/SQL Server&#xff09;\u4e3a\u57fa\u51c6&#xff0c;\u6ce8\u610f\u4e0d\u540c\u6570\u636e\u5e93\u8bed\u6cd5\u7565\u6709\u5dee\u5f02&#xff09;&#xff1a; \u2460 \u7b1b\u5361\u5c14\u79ef&#xff08;R \u00d7 S&#xff09;\u8f6c SQL \u8bed\u53e5&#xff1a; \u7b1b\u5361\u5c14\u79ef\u5373\u4e0d\u5e26 ON \u6216 WHERE \u6761\u4ef6\u7684 JOIN&#xff0c;\u7ed3\u679c\u662f R \u8868\u6bcf\u884c\u4e0e S \u8868\u6bcf\u884c\u7ec4\u5408\u3002 \u2705 \u6b63\u786e\u5199\u6cd5&#xff1a; SEL","og_url":"https:\/\/www.wsisp.com\/helps\/76513.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-22T09:42:12+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260222094210-699acf727d750.jpg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/76513.html","url":"https:\/\/www.wsisp.com\/helps\/76513.html","name":"\u5220\u9664\u5217\u662f**\u7834\u574f\u6027\u64cd\u4f5c**\uff0c\u6570\u636e\u5c06\u6c38\u4e45\u4e22\u5931\uff0c\u6267\u884c\u524d\u52a1\u5fc5\u5907\u4efd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-22T09:42:12+00:00","dateModified":"2026-02-22T09:42:12+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/76513.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/76513.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/76513.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u5220\u9664\u5217\u662f**\u7834\u574f\u6027\u64cd\u4f5c**\uff0c\u6570\u636e\u5c06\u6c38\u4e45\u4e22\u5931\uff0c\u6267\u884c\u524d\u52a1\u5fc5\u5907\u4efd"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/76513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=76513"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/76513\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/76512"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=76513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=76513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=76513"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=76513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}