{"id":38175,"date":"2025-05-20T01:04:04","date_gmt":"2025-05-19T17:04:04","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/38175.html"},"modified":"2025-05-20T01:04:04","modified_gmt":"2025-05-19T17:04:04","slug":"%e3%80%90postgresql%e6%95%b0%e6%8d%ae%e5%88%86%e6%9e%90%e5%ae%9e%e6%88%98%ef%bc%9a%e4%bb%8e%e6%95%b0%e6%8d%ae%e6%b8%85%e6%b4%97%e5%88%b0%e5%8f%af%e8%a7%86%e5%8c%96%e5%85%a8%e6%b5%81%e7%a8%8b%e3%80%911","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/38175.html","title":{"rendered":"\u3010PostgreSQL\u6570\u636e\u5206\u6790\u5b9e\u6218\uff1a\u4ece\u6570\u636e\u6e05\u6d17\u5230\u53ef\u89c6\u5316\u5168\u6d41\u7a0b\u30111.4 \u6570\u636e\u5e93\u4e0e\u8868\u7684\u57fa\u672c\u64cd\u4f5c\uff08DDL\/DML\u8bed\u53e5\uff09"},"content":{"rendered":"<p>&#x1f449; \u70b9\u51fb\u5173\u6ce8\u4e0d\u8ff7\u8def &#x1f449; \u70b9\u51fb\u5173\u6ce8\u4e0d\u8ff7\u8def &#x1f449; \u70b9\u51fb\u5173\u6ce8\u4e0d\u8ff7\u8def<\/p>\n<hr \/>\n<h4>\u6587\u7ae0\u5927\u7eb2<\/h4>\n<ul>\n<li>1.4 \u6570\u636e\u5e93\u4e0e\u8868\u7684\u57fa\u672c\u64cd\u4f5c&#xff08;DDL\/DML\u8bed\u53e5&#xff09;<\/li>\n<li>\n<ul>\n<li>1.4.1 \u6570\u636e\u5e93\u751f\u547d\u5468\u671f\u7ba1\u7406&#xff08;DDL\u6838\u5fc3&#xff09;<\/li>\n<li>\n<ul>\n<li>1.4.1.1 \u521b\u5efa\u6570\u636e\u5e93&#xff08;CREATE DATABASE&#xff09;<\/li>\n<li>\n<ul>\n<li>\u6700\u4f73\u5b9e\u8df5<\/li>\n<\/ul>\n<\/li>\n<li>1.4.1.2 \u67e5\u770b\u6570\u636e\u5e93\u4fe1\u606f<\/li>\n<li>1.4.1.3 \u4fee\u6539\u6570\u636e\u5e93&#xff08;ALTER DATABASE&#xff09;<\/li>\n<li>1.4.1.4 \u5220\u9664\u6570\u636e\u5e93&#xff08;DROP DATABASE&#xff09;<\/li>\n<\/ul>\n<\/li>\n<li>1.4.2 \u8868\u7ed3\u6784\u5b9a\u4e49\u4e0e\u7ea6\u675f&#xff08;DDL\u6838\u5fc3&#xff09;<\/li>\n<li>\n<ul>\n<li>1.4.2.1 \u6570\u636e\u7c7b\u578b\u901f\u67e5\u8868<\/li>\n<li>1.4.2.2 \u521b\u5efa\u8868&#xff08;CREATE TABLE&#xff09;<\/li>\n<li>\n<ul>\n<li>\u7ea6\u675f\u7c7b\u578b\u5bf9\u6bd4<\/li>\n<\/ul>\n<\/li>\n<li>1.4.2.3 \u4fee\u6539\u8868\u7ed3\u6784&#xff08;ALTER TABLE&#xff09;<\/li>\n<li>1.4.2.4 \u5220\u9664\u8868&#xff08;DROP TABLE&#xff09;<\/li>\n<\/ul>\n<\/li>\n<li>1.4.3 \u6570\u636e\u64cd\u4f5c\u8bed\u8a00&#xff08;DML\u6838\u5fc3&#xff09;<\/li>\n<li>\n<ul>\n<li>1.4.3.1 \u63d2\u5165\u6570\u636e&#xff08;INSERT&#xff09;<\/li>\n<li>1.4.3.2 \u67e5\u8be2\u6570\u636e&#xff08;SELECT&#xff09;<\/li>\n<li>\n<ul>\n<li>\u57fa\u7840\u8bed\u6cd5\u7ed3\u6784<\/li>\n<li>\u5e38\u7528\u51fd\u6570<\/li>\n<\/ul>\n<\/li>\n<li>1.4.3.3 \u66f4\u65b0\u6570\u636e&#xff08;UPDATE&#xff09;<\/li>\n<li>1.4.3.4 \u5220\u9664\u6570\u636e&#xff08;DELETE&#xff09;<\/li>\n<\/ul>\n<\/li>\n<li>1.4.4 \u4e8b\u52a1\u7ba1\u7406\u4e0e\u9501\u5b9a\u673a\u5236<\/li>\n<li>\n<ul>\n<li>1.4.4.1 \u4e8b\u52a1\u63a7\u5236\u8bed\u53e5<\/li>\n<li>\n<ul>\n<li>ACID\u7279\u6027\u4fdd\u969c<\/li>\n<\/ul>\n<\/li>\n<li>1.4.4.2 \u9501\u673a\u5236<\/li>\n<\/ul>\n<\/li>\n<li>1.4.5 \u5b9e\u6218\u6848\u4f8b&#xff1a;\u7535\u5546\u8ba2\u5355\u8868\u8bbe\u8ba1<\/li>\n<li>\n<ul>\n<li>1.4.5.1 \u8868\u7ed3\u6784\u5b9a\u4e49<\/li>\n<li>1.4.5.2 \u9ad8\u9891\u64cd\u4f5c\u793a\u4f8b<\/li>\n<li>\n<ul>\n<li>\u8ba2\u5355\u67e5\u8be2&#xff08;\u5e26\u7d22\u5f15\u4f18\u5316&#xff09;<\/li>\n<li>\u6279\u91cf\u8ba2\u5355\u5bfc\u5165&#xff08;COPY\u547d\u4ee4&#xff09;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>1.4.6 \u6700\u4f73\u5b9e\u8df5\u4e0e\u89c4\u8303<\/li>\n<li>\n<ul>\n<li>1.4.6.1 &#096;\u547d\u540d\u89c4\u8303 !!!&#096;<\/li>\n<li>1.4.6.2 \u6027\u80fd\u4f18\u5316\u5efa\u8bae<\/li>\n<li>1.4.6.3 \u6570\u636e\u5b8c\u6574\u6027\u4fdd\u969c<\/li>\n<\/ul>\n<\/li>\n<li>1.4.7 \u5e38\u89c1\u9519\u8bef\u4e0e\u89e3\u51b3\u65b9\u6848<\/li>\n<li>\n<ul>\n<li>1.4.7.1 \u7ea6\u675f\u51b2\u7a81\u5904\u7406<\/li>\n<li>1.4.7.2 \u5927\u8868\u64cd\u4f5c\u6280\u5de7<\/li>\n<\/ul>\n<\/li>\n<li>1.4.8 \u603b\u7ed3&#xff1a;\u6784\u5efa\u5065\u58ee\u7684\u6570\u636e\u6a21\u578b<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>1.4 \u6570\u636e\u5e93\u4e0e\u8868\u7684\u57fa\u672c\u64cd\u4f5c&#xff08;DDL\/DML\u8bed\u53e5&#xff09;<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519170403-682b64831c8f5.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>1.4.1 \u6570\u636e\u5e93\u751f\u547d\u5468\u671f\u7ba1\u7406&#xff08;DDL\u6838\u5fc3&#xff09;<\/h3>\n<h4>1.4.1.1 \u521b\u5efa\u6570\u636e\u5e93&#xff08;CREATE DATABASE&#xff09;<\/h4>\n<p><span class=\"token comment\">&#8212; \u57fa\u7840\u8bed\u6cd5<\/span><br \/>\n<span class=\"token comment\">&#8212; \u57fa\u7840\u8bed\u6cd5<\/span><br \/>\n<span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">DATABASE<\/span> analytics_db<br \/>\n<span class=\"token keyword\">WITH<\/span><br \/>\n    OWNER <span class=\"token operator\">&#061;<\/span> postgres              <span class=\"token comment\">&#8212; \u6240\u6709\u8005<\/span><br \/>\n    ENCODING <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;UTF8&#039;<\/span>             <span class=\"token comment\">&#8212; \u5b57\u7b26\u7f16\u7801<\/span><br \/>\n<span class=\"token comment\">&#8212;    LC_COLLATE &#061; &#039;en_US.utf8&#039;     &#8212; \u6392\u5e8f\u89c4\u5219<\/span><br \/>\n<span class=\"token comment\">&#8212;    LC_CTYPE &#061; &#039;en_US.utf8&#039;       &#8212; \u5b57\u7b26\u5206\u7c7b<\/span><br \/>\n    <span class=\"token keyword\">TABLESPACE<\/span> <span class=\"token operator\">&#061;<\/span> pg_default       <span class=\"token comment\">&#8212; \u8868\u7a7a\u95f4<\/span><br \/>\n    CONNECTION <span class=\"token keyword\">LIMIT<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span>         <span class=\"token comment\">&#8212; \u6700\u5927\u8fde\u63a5\u6570&#xff08;-1\u65e0\u9650\u5236&#xff09;<\/span><br \/>\n    TEMPLATE <span class=\"token operator\">&#061;<\/span> template0<span class=\"token punctuation\">;<\/span>         <span class=\"token comment\">&#8212; \u6a21\u677f\u6570\u636e\u5e93<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519170403-682b648355410.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h5>\u6700\u4f73\u5b9e\u8df5<\/h5>\n<ul>\n<li>\u4f7f\u7528TEMPLATE template1\u521b\u5efa\u652f\u6301\u4e2d\u6587\u7684\u6570\u636e\u5e93<\/li>\n<li>\u901a\u8fc7pg_tablespace\u67e5\u770b\u53ef\u7528\u8868\u7a7a\u95f4<\/li>\n<li>\u751f\u4ea7\u73af\u5883\u5efa\u8bae\u5355\u72ec\u521b\u5efa\u4e1a\u52a1\u8868\u7a7a\u95f4&#xff1a;<span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">TABLESPACE<\/span> data_ts LOCATION <span class=\"token string\">&#039;\/data\/pg_tablespace&#039;<\/span><span class=\"token punctuation\">;<\/span>\n <\/li>\n<\/ul>\n<h4>1.4.1.2 \u67e5\u770b\u6570\u636e\u5e93\u4fe1\u606f<\/h4>\n<table>\n<tr>\u547d\u4ee4\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>\\\\l\u6216SELECT * FROM pg_database;<\/td>\n<td>\u5217\u51fa\u6240\u6709\u6570\u636e\u5e93<\/td>\n<\/tr>\n<tr>\n<td>SELECT datname, datsize FROM pg_database;<\/td>\n<td>\u67e5\u770b\u6570\u636e\u5e93\u540d\u79f0\u53ca\u5927\u5c0f<\/td>\n<\/tr>\n<tr>\n<td>\\\\conninfo<\/td>\n<td>\u5f53\u524d\u8fde\u63a5\u7684\u6570\u636e\u5e93\u4fe1\u606f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>1.4.1.3 \u4fee\u6539\u6570\u636e\u5e93&#xff08;ALTER DATABASE&#xff09;<\/h4>\n<p><span class=\"token comment\">&#8212; \u4fee\u6539\u6240\u6709\u8005<\/span><br \/>\n<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">DATABASE<\/span> analytics_db OWNER <span class=\"token keyword\">TO<\/span> data_team<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u9650\u5236\u8fde\u63a5\u6570<\/span><br \/>\n<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">DATABASE<\/span> analytics_db CONNECTION <span class=\"token keyword\">LIMIT<\/span> <span class=\"token number\">100<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u66f4\u6539\u53c2\u6570\u914d\u7f6e&#xff08;\u4f1a\u8bdd\u7ea7\u751f\u6548&#xff09;<\/span><br \/>\n<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">DATABASE<\/span> analytics_db <span class=\"token keyword\">SET<\/span> work_mem <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;32MB&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h4>1.4.1.4 \u5220\u9664\u6570\u636e\u5e93&#xff08;DROP DATABASE&#xff09;<\/h4>\n<p><span class=\"token comment\">&#8212; \u5f3a\u5236\u5220\u9664&#xff08;\u7ec8\u6b62\u6240\u6709\u8fde\u63a5&#xff09;<\/span><br \/>\n<span class=\"token keyword\">DROP<\/span> <span class=\"token keyword\">DATABASE<\/span> <span class=\"token keyword\">IF<\/span> <span class=\"token keyword\">EXISTS<\/span> analytics_db <span class=\"token keyword\">WITH<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">FORCE<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519170403-682b648365f86.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u26a0\ufe0f \u6ce8\u610f&#xff1a;\u5220\u9664\u524d\u8bf7\u901a\u8fc7SELECT * FROM pg_stat_activity WHERE datname&#061;&#039;analytics_db&#039;;\u786e\u8ba4\u65e0\u6d3b\u52a8\u8fde\u63a5<\/p>\n<h3>1.4.2 \u8868\u7ed3\u6784\u5b9a\u4e49\u4e0e\u7ea6\u675f&#xff08;DDL\u6838\u5fc3&#xff09;<\/h3>\n<h4>1.4.2.1 \u6570\u636e\u7c7b\u578b\u901f\u67e5\u8868<\/h4>\n<table>\n<tr>\u7c7b\u578b\u5206\u7c7b\u5e38\u7528\u6570\u636e\u7c7b\u578b\u5b58\u50a8\u8303\u56f4\/\u7279\u6027\u793a\u4f8b\u503c<\/tr>\n<tbody>\n<tr>\n<td>\u6570\u503c\u578b<\/td>\n<td>INT\/BIGINT<\/td>\n<td>4\/8\u5b57\u8282\u6574\u6570<\/td>\n<td>100, -500<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>DECIMAL(p,s)<\/td>\n<td>\u9ad8\u7cbe\u5ea6\u5341\u8fdb\u5236\u6570&#xff08;p\u603b\u4f4d\u6570&#xff0c;s\u5c0f\u6570\u4f4d&#xff09;<\/td>\n<td>DECIMAL(10,2) \u2192 1234.56<\/td>\n<\/tr>\n<tr>\n<td>\u5b57\u7b26\u578b<\/td>\n<td>VARCHAR(n)<\/td>\n<td>\u53ef\u53d8\u957f\u5b57\u7b26\u4e32&#xff08;n\u4e3a\u6700\u5927\u957f\u5ea6&#xff09;<\/td>\n<td>\u2018\u6570\u636e\u5206\u6790\u2019<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>TEXT<\/td>\n<td>\u65e0\u957f\u5ea6\u9650\u5236\u5b57\u7b26\u4e32<\/td>\n<td>\u957f\u6587\u672c\u5185\u5bb9<\/td>\n<\/tr>\n<tr>\n<td>\u65e5\u671f\u65f6\u95f4<\/td>\n<td>DATE<\/td>\n<td>\u5e74\u6708\u65e5&#xff08;YYYY-MM-DD&#xff09;<\/td>\n<td>\u20182023-12-31\u2019<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>TIMESTAMP WITH TIME ZONE<\/td>\n<td>\u5e26\u65f6\u533a\u7684\u65f6\u95f4\u6233<\/td>\n<td>\u20182024-01-01 10:00:00&#043;08\u2019<\/td>\n<\/tr>\n<tr>\n<td>\u5e03\u5c14\u578b<\/td>\n<td>BOOLEAN<\/td>\n<td>\u771f\/\u5047<\/td>\n<td>TRUE\/FALSE<\/td>\n<\/tr>\n<tr>\n<td>\u4e8c\u8fdb\u5236<\/td>\n<td>BYTEA<\/td>\n<td>\u4e8c\u8fdb\u5236\u6570\u636e&#xff08;\u56fe\u7247\/\u6587\u4ef6&#xff09;<\/td>\n<td>\\\\xDEADBEEF<\/td>\n<\/tr>\n<tr>\n<td>\u51e0\u4f55\u7c7b\u578b<\/td>\n<td>POINT<\/td>\n<td>\u4e8c\u7ef4\u5750\u6807&#xff08;x,y&#xff09;<\/td>\n<td>POINT(10, 20)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>1.4.2.2 \u521b\u5efa\u8868&#xff08;CREATE TABLE&#xff09;<\/h4>\n<p><span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">TABLE<\/span> departments <span class=\"token punctuation\">(<\/span><br \/>\n    dept_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    dept_name <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">50<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">&#8212;<\/span><br \/>\n \u5458\u5de5\u4fe1\u606f\u8868&#xff08;\u542b\u7ea6\u675f&#xff09;<br \/>\n<span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">TABLE<\/span> employees <span class=\"token punctuation\">(<\/span><br \/>\n    emp_id <span class=\"token keyword\">SERIAL<\/span> <span class=\"token keyword\">PRIMARY<\/span> <span class=\"token keyword\">KEY<\/span><span class=\"token punctuation\">,<\/span>          <span class=\"token comment\">&#8212; \u81ea\u589e\u4e3b\u952e<\/span><br \/>\n    emp_name <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">50<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span><span class=\"token punctuation\">,<\/span>       <span class=\"token comment\">&#8212; \u975e\u7a7a\u7ea6\u675f<\/span><br \/>\n    email <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">100<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">UNIQUE<\/span><span class=\"token punctuation\">,<\/span>           <span class=\"token comment\">&#8212; \u552f\u4e00\u7ea6\u675f<\/span><br \/>\n    hire_date <span class=\"token keyword\">DATE<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token keyword\">CURRENT_DATE<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token comment\">&#8212; \u9ed8\u8ba4\u503c\u7ea6\u675f<\/span><br \/>\n    salary <span class=\"token keyword\">NUMERIC<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">10<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">CHECK<\/span> <span class=\"token punctuation\">(<\/span>salary <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token comment\">&#8212; \u68c0\u67e5\u7ea6\u675f<\/span><br \/>\n    department_id <span class=\"token keyword\">INTEGER<\/span> <span class=\"token keyword\">REFERENCES<\/span> departments<span class=\"token punctuation\">(<\/span>dept_id<span class=\"token punctuation\">)<\/span> <span class=\"token comment\">&#8212; \u5916\u952e\u7ea6\u675f<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h5>\u7ea6\u675f\u7c7b\u578b\u5bf9\u6bd4<\/h5>\n<table>\n<tr>\u7ea6\u675f\u7c7b\u578b\u5173\u952e\u5b57\u4f5c\u7528\u6027\u80fd\u5f71\u54cd<\/tr>\n<tbody>\n<tr>\n<td>\u4e3b\u952e<\/td>\n<td>PRIMARY KEY<\/td>\n<td>\u552f\u4e00\u6807\u8bc6\u8bb0\u5f55&#xff0c;\u81ea\u52a8\u521b\u5efa\u7d22\u5f15<\/td>\n<td>\u8bfb\u4f18\u5316&#xff0c;\u5199\u8f7b\u5fae\u5f71\u54cd<\/td>\n<\/tr>\n<tr>\n<td>\u552f\u4e00<\/td>\n<td>UNIQUE<\/td>\n<td>\u786e\u4fdd\u5b57\u6bb5\u503c\u552f\u4e00<\/td>\n<td>\u7c7b\u4f3c\u4e3b\u952e&#xff0c;\u5141\u8bb8NULL\u503c<\/td>\n<\/tr>\n<tr>\n<td>\u975e\u7a7a<\/td>\n<td>NOT NULL<\/td>\n<td>\u7981\u6b62\u5b57\u6bb5\u4e3a\u7a7a<\/td>\n<td>\u65e0\u7d22\u5f15\u6027\u80fd\u5f71\u54cd<\/td>\n<\/tr>\n<tr>\n<td>\u68c0\u67e5<\/td>\n<td>CHECK<\/td>\n<td>\u81ea\u5b9a\u4e49\u903b\u8f91\u7ea6\u675f<\/td>\n<td>\u6bcf\u6b21\u5199\u5165\u65f6\u89e6\u53d1\u68c0\u67e5<\/td>\n<\/tr>\n<tr>\n<td>\u5916\u952e<\/td>\n<td>REFERENCES<\/td>\n<td>\u5efa\u7acb\u8868\u95f4\u5173\u8054<\/td>\n<td>\u7ea7\u8054\u64cd\u4f5c\u9700\u989d\u5916\u5f00\u9500<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>1.4.2.3 \u4fee\u6539\u8868\u7ed3\u6784&#xff08;ALTER TABLE&#xff09;<\/h4>\n<p><span class=\"token comment\">&#8212; \u6dfb\u52a0\u5b57\u6bb5&#xff08;\u5e26\u9ed8\u8ba4\u503c&#xff09;<\/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> phone <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">20<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token string\">&#039;\u672a\u63d0\u4f9b&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u4fee\u6539\u6570\u636e\u7c7b\u578b&#xff08;\u9700\u91cd\u5efa\u8868&#xff09;<\/span><br \/>\n<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> employees <span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">COLUMN<\/span> salary <span class=\"token keyword\">TYPE<\/span> <span class=\"token keyword\">NUMERIC<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">12<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u5220\u9664\u5b57\u6bb5&#xff08;\u751f\u4ea7\u73af\u5883\u614e\u7528&#xff09;<\/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> fax<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u6dfb\u52a0\u5916\u952e&#xff08;\u5ef6\u8fdf\u7ea6\u675f\u68c0\u67e5&#xff09;<\/span><br \/>\n<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> employees<br \/>\n<span class=\"token keyword\">ADD<\/span> <span class=\"token keyword\">CONSTRAINT<\/span> fk_dept<br \/>\n<span class=\"token keyword\">FOREIGN<\/span> <span class=\"token keyword\">KEY<\/span> <span class=\"token punctuation\">(<\/span>department_id<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">REFERENCES<\/span> departments<span class=\"token punctuation\">(<\/span>dept_id<span class=\"token punctuation\">)<\/span><br \/>\nDEFERRABLE INITIALLY DEFERRED<span class=\"token punctuation\">;<\/span><\/p>\n<h4>1.4.2.4 \u5220\u9664\u8868&#xff08;DROP TABLE&#xff09;<\/h4>\n<p><span class=\"token comment\">&#8212; \u7ea7\u8054\u5220\u9664\u4f9d\u8d56\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">DROP<\/span> <span class=\"token keyword\">TABLE<\/span> <span class=\"token keyword\">IF<\/span> <span class=\"token keyword\">EXISTS<\/span> employees <span class=\"token keyword\">CASCADE<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u4ec5\u5220\u9664\u8868\u7ed3\u6784&#xff0c;\u4fdd\u7559\u6570\u636e&#xff08;PostgreSQL 12&#043;&#xff09;<\/span><br \/>\n<span class=\"token keyword\">TRUNCATE<\/span> <span class=\"token keyword\">TABLE<\/span> employees<span class=\"token punctuation\">;<\/span><\/p>\n<p>&#x1f4a1;&#x1f4a1;&#x1f4a1; \u63d0\u793a&#xff1a;TRUNCATE\u6bd4DELETE FROM \u6548\u7387\u9ad810-100\u500d&#xff0c;\u9002\u5408\u6e05\u7a7a\u5927\u8868 &#x1f4a1;&#x1f4a1;&#x1f4a1; \u63d0\u793a&#xff1a;TRUNCATE\u6bd4DELETE FROM \u6548\u7387\u9ad810-100\u500d&#xff0c;\u9002\u5408\u6e05\u7a7a\u5927\u8868 &#x1f4a1;&#x1f4a1;&#x1f4a1; \u63d0\u793a&#xff1a;TRUNCATE\u6bd4DELETE FROM \u6548\u7387\u9ad810-100\u500d&#xff0c;\u9002\u5408\u6e05\u7a7a\u5927\u8868<\/p>\n<h3>1.4.3 \u6570\u636e\u64cd\u4f5c\u8bed\u8a00&#xff08;DML\u6838\u5fc3&#xff09;<\/h3>\n<h4>1.4.3.1 \u63d2\u5165\u6570\u636e&#xff08;INSERT&#xff09;<\/h4>\n<p><span class=\"token comment\">&#8212; \u5355\u6761\u63d2\u5165<\/span><br \/>\n<span class=\"token keyword\">INSERT<\/span> <span class=\"token keyword\">INTO<\/span> employees <span class=\"token punctuation\">(<\/span>emp_name<span class=\"token punctuation\">,<\/span> email<span class=\"token punctuation\">,<\/span> salary<span class=\"token punctuation\">,<\/span> department_id<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">VALUES<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u5f20\u4e09&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;zhangsan&#064;example.com&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">15000<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">101<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u6279\u91cf\u63d2\u5165&#xff08;\u6027\u80fd\u63d0\u534730%&#043;&#xff09;<\/span><br \/>\n<span class=\"token keyword\">INSERT<\/span> <span class=\"token keyword\">INTO<\/span> employees <span class=\"token punctuation\">(<\/span>emp_name<span class=\"token punctuation\">,<\/span> email<span class=\"token punctuation\">,<\/span> salary<span class=\"token punctuation\">,<\/span> department_id<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">VALUES<\/span><br \/>\n<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u674e\u56db&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;lisi&#064;example.com&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">16000<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">102<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u738b\u4e94&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;wangwu&#064;example.com&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">17000<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">101<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u63d2\u5165\u5e76\u8fd4\u56de\u81ea\u589eID<\/span><br \/>\n<span class=\"token keyword\">INSERT<\/span> <span class=\"token keyword\">INTO<\/span> employees <span class=\"token punctuation\">(<\/span>emp_name<span class=\"token punctuation\">,<\/span> email<span class=\"token punctuation\">,<\/span> salary<span class=\"token punctuation\">,<\/span> department_id<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">VALUES<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u8d75\u516d&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;zhaoliu&#064;example.com&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">18000<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">103<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">RETURNING<\/span> emp_id<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">&#8212; \u8fd4\u56de\u65b0\u751f\u6210\u7684emp_id<\/span><\/p>\n<h4>1.4.3.2 \u67e5\u8be2\u6570\u636e&#xff08;SELECT&#xff09;<\/h4>\n<h5>\u57fa\u7840\u8bed\u6cd5\u7ed3\u6784<\/h5>\n<p><span class=\"token keyword\">SELECT<\/span><br \/>\n    column1<span class=\"token punctuation\">,<\/span> column2<span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token keyword\">CASE<\/span> <span class=\"token keyword\">WHEN<\/span> salary <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">15000<\/span> <span class=\"token keyword\">THEN<\/span> <span class=\"token string\">&#039;\u9ad8\u85aa&#039;<\/span> <span class=\"token keyword\">ELSE<\/span> <span class=\"token string\">&#039;\u666e\u901a&#039;<\/span> <span class=\"token keyword\">END<\/span> <span class=\"token keyword\">AS<\/span> salary_level <span class=\"token comment\">&#8212; \u6761\u4ef6\u8868\u8fbe\u5f0f<\/span><br \/>\n<span class=\"token keyword\">FROM<\/span> employees<br \/>\n<span class=\"token keyword\">WHERE<\/span> hire_date <span class=\"token operator\">&gt;&#061;<\/span> <span class=\"token string\">&#039;2023-01-01&#039;<\/span>  <span class=\"token comment\">&#8212; \u8fc7\u6ee4\u6761\u4ef6<\/span><br \/>\n  <span class=\"token operator\">AND<\/span> department_id <span class=\"token operator\">IN<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token number\">101<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">102<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">GROUP<\/span> <span class=\"token keyword\">BY<\/span> department_id           <span class=\"token comment\">&#8212; \u5206\u7ec4\u805a\u5408<\/span><br \/>\n<span class=\"token keyword\">HAVING<\/span> <span class=\"token function\">AVG<\/span><span class=\"token punctuation\">(<\/span>salary<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">16000<\/span>       <span class=\"token comment\">&#8212; \u5206\u7ec4\u540e\u8fc7\u6ee4<\/span><br \/>\n<span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> salary <span class=\"token keyword\">DESC<\/span>              <span class=\"token comment\">&#8212; \u6392\u5e8f<\/span><br \/>\n<span class=\"token keyword\">LIMIT<\/span> <span class=\"token number\">10<\/span> <span class=\"token keyword\">OFFSET<\/span> <span class=\"token number\">20<\/span><span class=\"token punctuation\">;<\/span>               <span class=\"token comment\">&#8212; \u5206\u9875&#xff08;\u7b2c3\u9875&#xff0c;\u6bcf\u987510\u6761&#xff09;<\/span><\/p>\n<h5>\u5e38\u7528\u51fd\u6570<\/h5>\n<table>\n<tr>\u51fd\u6570\u5206\u7c7b\u51fd\u6570\u540d\u8bf4\u660e\u793a\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>\u805a\u5408\u51fd\u6570<\/td>\n<td>COUNT(*) \/ COUNT(col)<\/td>\n<td>\u8ba1\u6570<\/td>\n<td>COUNT(DISTINCT department_id)<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>SUM\/AVG\/MAX\/MIN<\/td>\n<td>\u6570\u503c\u805a\u5408<\/td>\n<td>AVG(salary)<\/td>\n<\/tr>\n<tr>\n<td>\u5b57\u7b26\u4e32\u51fd\u6570<\/td>\n<td>CONCAT(str1, str2)<\/td>\n<td>\u5b57\u7b26\u4e32\u62fc\u63a5<\/td>\n<td>CONCAT(emp_name, \u2019 (&#039;, email, \u2018)\u2019)<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>SUBSTRING(str FROM start FOR length)<\/td>\n<td>\u5b50\u4e32\u63d0\u53d6<\/td>\n<td>SUBSTRING(email FROM 1 FOR 5)<\/td>\n<\/tr>\n<tr>\n<td>\u65e5\u671f\u51fd\u6570<\/td>\n<td>AGE(timestamp)<\/td>\n<td>\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694<\/td>\n<td>AGE(hire_date)<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>DATE_TRUNC(\u2018month\u2019, date)<\/td>\n<td>\u65e5\u671f\u622a\u65ad\u5230\u6708<\/td>\n<td>DATE_TRUNC(\u2018quarter\u2019, now())<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\u5728 PostgreSQL \u4e2d&#xff0c;AGE(timestamp) \u662f\u4e00\u4e2a\u7528\u4e8e\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\u7684\u51fd\u6570&#xff0c;\u4e3b\u8981\u7528\u4e8e\u83b7\u53d6 \u67d0\u4e2a\u65f6\u95f4\u70b9\u4e0e\u5f53\u524d\u65f6\u95f4\u7684\u95f4\u9694&#xff0c;\u6216 \u4e24\u4e2a\u65f6\u95f4\u70b9\u4e4b\u95f4\u7684\u95f4\u9694\u3002\u4ee5\u4e0b\u662f\u5173\u4e8e\u8be5\u51fd\u6570\u7684\u8be6\u7ec6\u89e3\u6790&#xff1a;\n<ul>\n<li>\n<li>\u57fa\u7840\u8bed\u6cd5<\/li>\n<p> AGE<span class=\"token punctuation\">(<\/span><span class=\"token keyword\">timestamp<\/span><span class=\"token punctuation\">)<\/span>                  <span class=\"token comment\">&#8212; \u8ba1\u7b97 timestamp \u5230\u5f53\u524d\u65f6\u95f4\u7684\u95f4\u9694&#xff08;\u7b49\u6548\u4e8e AGE(now(), timestamp)&#xff09;<\/span><\/p>\n<p>AGE<span class=\"token punctuation\">(<\/span>end_timestamp<span class=\"token punctuation\">,<\/span> start_timestamp<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\">&#8212; \u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u70b9\u4e4b\u95f4\u7684\u95f4\u9694&#xff08;end &#8211; start&#xff09;<\/span>\n <\/li>\n<li>\n<li>\u53c2\u6570\u8bf4\u660e<br \/>\n<table>\n<tr>\u53c2\u6570\u7c7b\u578b\u63cf\u8ff0\u793a\u4f8b\u503c<\/tr>\n<tbody>\n<tr>\n<td>timestamp<\/td>\n<td>\u5355\u4e2a\u65f6\u95f4\u6233&#xff08;\u53ef\u5e26\u65f6\u533a timestamptz&#xff09;<\/td>\n<td>&#039;2020-01-01 08:00:00&#039;<\/td>\n<\/tr>\n<tr>\n<td>end_timestamp<\/td>\n<td>\u7ed3\u675f\u65f6\u95f4\u6233&#xff08;\u5fc5\u987b\u665a\u4e8e\u6216\u7b49\u4e8e\u5f00\u59cb\u65f6\u95f4&#xff09;<\/td>\n<td>order_completed_at<\/td>\n<\/tr>\n<tr>\n<td>start_timestamp<\/td>\n<td>\u5f00\u59cb\u65f6\u95f4\u6233<\/td>\n<td>order_created_at<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/li>\n<\/li>\n<li>\n<li>\u8fd4\u56de\u503c\u7c7b\u578b \u8fd4\u56de interval \u7c7b\u578b&#xff0c;\u683c\u5f0f\u4e3a \u5e74-\u6708-\u65e5 \u65f6:\u5206:\u79d2&#xff0c;\u4f8b\u5982&#xff1a; 3 years 2 mons 10 days 03:05:15&#xff08;\u8868\u793a 3 \u5e74 2 \u4e2a\u6708 10 \u5929 3 \u5c0f\u65f6 5 \u5206 15 \u79d2&#xff09;\u3002<\/li>\n<\/li>\n<li>\n<p>\u573a\u666f 1&#xff1a;\u8ba1\u7b97\u5355\u4e2a\u65f6\u95f4\u70b9\u4e0e\u5f53\u524d\u65f6\u95f4\u7684\u95f4\u9694&#xff08;\u6700\u5e38\u7528&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u573a\u666f 2&#xff1a;\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u70b9\u7684\u95f4\u9694&#xff08;\u5982\u8ba2\u5355\u5904\u7406\u8017\u65f6&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u573a\u666f 3&#xff1a;\u5e26\u65f6\u533a\u7684\u65f6\u95f4\u6233\u5904\u7406&#xff08;\u81ea\u52a8\u8f6c\u6362\u4e3a UTC \u8ba1\u7b97&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u6ce8\u610f\u4e8b\u9879\u4e0e\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/li>\n<li>\n<li>\u53c2\u6570\u987a\u5e8f\u7684\u91cd\u8981\u6027 AGE(end, start) \u5fc5\u987b\u6ee1\u8db3 end &gt;&#061; start&#xff0c;\u5426\u5219\u4f1a\u8fd4\u56de\u8d1f\u6570\u95f4\u9694&#xff08;\u5982 -1 day 00:00:00&#xff09;\u3002<\/li>\n<\/li>\n<li>\n<li>\u65f6\u533a\u5904\u7406 \u82e5\u65f6\u95f4\u6233\u5e26\u65f6\u533a&#xff08;timestamptz&#xff09;&#xff0c;AGE \u4f1a\u81ea\u52a8\u5c06\u5176\u8f6c\u6362\u4e3a UTC \u65f6\u95f4\u8fdb\u884c\u8ba1\u7b97&#xff0c;\u907f\u514d\u65f6\u533a\u504f\u79fb\u5bfc\u81f4\u7684\u8bef\u5dee\u3002<\/li>\n<\/li>\n<li>\n<li>\u6027\u80fd\u4f18\u5316 \u5bf9\u5927\u8868\u4f7f\u7528 AGE \u65f6&#xff0c;\u82e5\u6d89\u53ca\u7d22\u5f15\u5217&#xff08;\u5982 hire_date&#xff09;&#xff0c;\u786e\u4fdd\u8be5\u5217\u6709\u7d22\u5f15\u4ee5\u52a0\u901f\u67e5\u8be2&#xff1a;<\/li>\n<p> <span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">INDEX<\/span> idx_hire_date <span class=\"token keyword\">ON<\/span> employees<span class=\"token punctuation\">(<\/span>hire_date<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\n <\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>1.4.3.3 \u66f4\u65b0\u6570\u636e&#xff08;UPDATE&#xff09;<\/h4>\n<p><span class=\"token comment\">&#8212; \u5355\u8868\u66f4\u65b0&#xff08;\u8bbe\u7f6e\u5de5\u8d44\u589e\u957f5%&#xff09;<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> employees<br \/>\n<span class=\"token keyword\">SET<\/span> salary <span class=\"token operator\">&#061;<\/span> salary <span class=\"token operator\">*<\/span> <span class=\"token number\">1.05<\/span><br \/>\n<span class=\"token keyword\">WHERE<\/span> department_id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">101<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u5173\u8054\u66f4\u65b0&#xff08;\u57fa\u4e8e\u90e8\u95e8\u8868&#xff09;<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> employees e<br \/>\n<span class=\"token keyword\">SET<\/span> email<br \/>\n<span class=\"token operator\">&#061;<\/span> CONCAT<span class=\"token punctuation\">(<\/span>e<span class=\"token punctuation\">.<\/span>emp_name<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;&#064;&#039;<\/span><span class=\"token punctuation\">,<\/span> d<span class=\"token punctuation\">.<\/span>dept_domain<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">FROM<\/span> departments d<br \/>\n<span class=\"token keyword\">WHERE<\/span> e<span class=\"token punctuation\">.<\/span>department_id <span class=\"token operator\">&#061;<\/span> d<span class=\"token punctuation\">.<\/span>dept_id<span class=\"token punctuation\">;<\/span><\/p>\n<h4>1.4.3.4 \u5220\u9664\u6570\u636e&#xff08;DELETE&#xff09;<\/h4>\n<p><span class=\"token comment\">&#8212; \u5355\u8868\u5220\u9664&#xff08;\u79bb\u804c\u5458\u5de5&#xff09;<\/span><br \/>\n<span class=\"token comment\">&#8212; \u5355\u8868\u5220\u9664&#xff08;\u79bb\u804c\u5458\u5de5&#xff09;<\/span><br \/>\n<span class=\"token keyword\">DELETE<\/span> <span class=\"token keyword\">FROM<\/span> employees<br \/>\n<span class=\"token keyword\">WHERE<\/span> hire_date <span class=\"token operator\">&lt;<\/span> <span class=\"token string\">&#039;2020-01-01&#039;<\/span><br \/>\n  <span class=\"token operator\">AND<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token keyword\">EXISTS<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">SELECT<\/span> <span class=\"token number\">1<\/span> <span class=\"token keyword\">FROM<\/span> payroll <span class=\"token keyword\">WHERE<\/span> emp_id <span class=\"token operator\">&#061;<\/span> employees<span class=\"token punctuation\">.<\/span>emp_id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u8de8\u8868\u7ea7\u8054\u5220\u9664&#xff08;\u9700\u5916\u952e\u8bbe\u7f6e\u7ea7\u8054&#xff09;<\/span><br \/>\n<span class=\"token keyword\">DELETE<\/span> <span class=\"token keyword\">FROM<\/span> departments<br \/>\n<span class=\"token keyword\">WHERE<\/span> dept_id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">100<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">&#8212; \u81ea\u52a8\u5220\u9664\u5173\u8054\u7684employees\u8bb0\u5f55<\/span><\/p>\n<h3>1.4.4 \u4e8b\u52a1\u7ba1\u7406\u4e0e\u9501\u5b9a\u673a\u5236<\/h3>\n<h4>1.4.4.1 \u4e8b\u52a1\u63a7\u5236\u8bed\u53e5<\/h4>\n<p><span class=\"token comment\">&#8212; \u663e\u5f0f\u4e8b\u52a1\u793a\u4f8b&#xff08;\u8d26\u6237\u8f6c\u8d26&#xff09;<\/span><br \/>\n<span class=\"token keyword\">BEGIN<\/span> <span class=\"token keyword\">TRANSACTION<\/span><span class=\"token punctuation\">;<\/span>          <span class=\"token comment\">&#8212; \u5f00\u59cb\u4e8b\u52a1<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> accounts <span class=\"token keyword\">SET<\/span> balance <span class=\"token operator\">&#061;<\/span> balance <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1000<\/span> <span class=\"token keyword\">WHERE<\/span> user_id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> accounts <span class=\"token keyword\">SET<\/span> balance <span class=\"token operator\">&#061;<\/span> balance <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1000<\/span> <span class=\"token keyword\">WHERE<\/span> user_id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">COMMIT<\/span><span class=\"token punctuation\">;<\/span>                     <span class=\"token comment\">&#8212; \u63d0\u4ea4\u4e8b\u52a1&#xff08;\u6c38\u4e45\u751f\u6548&#xff09;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u5e26\u4fdd\u5b58\u70b9\u7684\u4e8b\u52a1<\/span><br \/>\n<span class=\"token keyword\">BEGIN<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">SAVEPOINT<\/span> sp1<span class=\"token punctuation\">;<\/span>              <span class=\"token comment\">&#8212; \u8bbe\u7f6e\u4fdd\u5b58\u70b9<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> orders <span class=\"token keyword\">SET<\/span> <span class=\"token keyword\">status<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;processing&#039;<\/span> <span class=\"token keyword\">WHERE<\/span> order_id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">101<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">ROLLBACK<\/span> <span class=\"token keyword\">TO<\/span> sp1<span class=\"token punctuation\">;<\/span>            <span class=\"token comment\">&#8212; \u56de\u6eda\u5230\u4fdd\u5b58\u70b9<\/span><br \/>\n<span class=\"token keyword\">COMMIT<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h5>ACID\u7279\u6027\u4fdd\u969c<\/h5>\n<ul>\n<li>\u539f\u5b50\u6027&#xff1a;\u4e8b\u52a1\u5185\u64cd\u4f5c\u8981\u4e48\u5168\u6210\u529f&#xff0c;\u8981\u4e48\u5168\u56de\u6eda<\/li>\n<li>\u4e00\u81f4\u6027&#xff1a;\u4e8b\u52a1\u524d\u540e\u6570\u636e\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6<\/li>\n<li>\u9694\u79bb\u6027&#xff1a;\u901a\u8fc7SET TRANSACTION ISOLATION LEVEL\u63a7\u5236&#xff08;\u9ed8\u8ba4READ COMMITTED&#xff09;<\/li>\n<li>\u6301\u4e45\u6027&#xff1a;\u63d0\u4ea4\u540e\u6570\u636e\u6c38\u4e45\u5199\u5165\u78c1\u76d8<\/li>\n<\/ul>\n<h4>1.4.4.2 \u9501\u673a\u5236<\/h4>\n<table>\n<tr>\u9501\u7c7b\u578b\u6a21\u5f0f\u4f5c\u7528\u793a\u4f8b\u573a\u666f<\/tr>\n<tbody>\n<tr>\n<td>\u5171\u4eab\u9501&#xff08;S&#xff09;<\/td>\n<td>SELECT \u2026 FOR SHARE<\/td>\n<td>\u963b\u6b62\u5199\u64cd\u4f5c&#xff0c;\u5141\u8bb8\u8bfb\u5e76\u53d1<\/td>\n<td>\u62a5\u8868\u67e5\u8be2\u65f6\u9632\u6b62\u6570\u636e\u4fee\u6539<\/td>\n<\/tr>\n<tr>\n<td>\u6392\u4ed6\u9501&#xff08;X&#xff09;<\/td>\n<td>SELECT \u2026 FOR UPDATE<\/td>\n<td>\u963b\u6b62\u6240\u6709\u5e76\u53d1\u64cd\u4f5c<\/td>\n<td>\u5e93\u5b58\u6263\u51cf\u65f6\u9632\u6b62\u8d85\u5356<\/td>\n<\/tr>\n<tr>\n<td>\u8868\u7ea7\u9501<\/td>\n<td>LOCK TABLE \u2026<\/td>\n<td>\u9501\u5b9a\u6574\u5f20\u8868&#xff08;\u6027\u80fd\u5f71\u54cd\u5927&#xff09;<\/td>\n<td>\u6279\u91cf\u6570\u636e\u521d\u59cb\u5316<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>1.4.5 \u5b9e\u6218\u6848\u4f8b&#xff1a;\u7535\u5546\u8ba2\u5355\u8868\u8bbe\u8ba1<\/h3>\n<h4>1.4.5.1 \u8868\u7ed3\u6784\u5b9a\u4e49<\/h4>\n<p><span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">TABLE<\/span> orders <span class=\"token punctuation\">(<\/span><br \/>\n    order_id <span class=\"token keyword\">BIGINT<\/span> <span class=\"token keyword\">PRIMARY<\/span> <span class=\"token keyword\">KEY<\/span> <span class=\"token keyword\">DEFAULT<\/span> generate_ulid<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token comment\">&#8212; \u4f7f\u7528ULID\u4f5c\u4e3a\u4e3b\u952e&#xff08;\u6bd4UUID\u6027\u80fd\u9ad850%&#xff09;<\/span><br \/>\n    user_id <span class=\"token keyword\">INTEGER<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">REFERENCES<\/span> users<span class=\"token punctuation\">(<\/span>user_id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    order_time <span class=\"token keyword\">TIMESTAMP<\/span> <span class=\"token keyword\">WITH<\/span> <span class=\"token keyword\">TIME<\/span> ZONE <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token keyword\">CURRENT_TIMESTAMP<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    total_amount <span class=\"token keyword\">NUMERIC<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">12<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">CHECK<\/span> <span class=\"token punctuation\">(<\/span>total_amount <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token keyword\">status<\/span> <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">20<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">CHECK<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">status<\/span> <span class=\"token operator\">IN<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u5f85\u652f\u4ed8&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;\u5df2\u652f\u4ed8&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;\u5df2\u53d6\u6d88&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;\u5df2\u5b8c\u6210&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    delivery_address JSONB<span class=\"token punctuation\">,<\/span> <span class=\"token comment\">&#8212; \u5b58\u50a8\u7ed3\u6784\u5316\u5730\u5740\u6570\u636e<\/span><br \/>\n    <span class=\"token keyword\">INDEX<\/span> idx_user_id_status <span class=\"token punctuation\">(<\/span>user_id<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">status<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token comment\">&#8212; \u7ec4\u5408\u7d22\u5f15\u4f18\u5316\u67e5\u8be2<\/span><br \/>\n    <span class=\"token keyword\">TABLESPACE<\/span> order_ts <span class=\"token comment\">&#8212; \u4f7f\u7528\u72ec\u7acb\u8868\u7a7a\u95f4<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h4>1.4.5.2 \u9ad8\u9891\u64cd\u4f5c\u793a\u4f8b<\/h4>\n<h5>\u8ba2\u5355\u67e5\u8be2&#xff08;\u5e26\u7d22\u5f15\u4f18\u5316&#xff09;<\/h5>\n<p><span class=\"token comment\">&#8212; \u4f18\u5316\u524d&#xff1a;\u5168\u8868\u626b\u63cf&#xff08;\u8017\u65f6120ms&#xff09;<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> orders <span class=\"token keyword\">WHERE<\/span> user_id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1234<\/span> <span class=\"token operator\">AND<\/span> <span class=\"token keyword\">status<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\u5df2\u5b8c\u6210&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u4f18\u5316\u540e&#xff1a;\u4f7f\u7528\u7ec4\u5408\u7d22\u5f15&#xff08;\u8017\u65f68ms&#xff09;<\/span><br \/>\n<span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">INDEX<\/span> idx_user_id_status <span class=\"token keyword\">ON<\/span> orders<span class=\"token punctuation\">(<\/span>user_id<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">status<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h5>\u6279\u91cf\u8ba2\u5355\u5bfc\u5165&#xff08;COPY\u547d\u4ee4&#xff09;<\/h5>\n<p><span class=\"token comment\">&#8212; \u6bd4INSERT\u901f\u5ea6\u5feb10\u500d\u4ee5\u4e0a<\/span><br \/>\nCOPY orders <span class=\"token punctuation\">(<\/span>user_id<span class=\"token punctuation\">,<\/span> order_time<span class=\"token punctuation\">,<\/span> total_amount<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">status<\/span><span class=\"token punctuation\">,<\/span> delivery_address<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">FROM<\/span> <span class=\"token string\">&#039;\/data\/orders_202401.csv&#039;<\/span><br \/>\n<span class=\"token keyword\">WITH<\/span> <span class=\"token punctuation\">(<\/span>FORMAT CSV<span class=\"token punctuation\">,<\/span> HEADER<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">DELIMITER<\/span> <span class=\"token string\">&#039;,&#039;<\/span><span class=\"token punctuation\">,<\/span> QUOTE <span class=\"token string\">&#039;&#034;&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h3>1.4.6 \u6700\u4f73\u5b9e\u8df5\u4e0e\u89c4\u8303<\/h3>\n<h4>1.4.6.1 \u547d\u540d\u89c4\u8303 !!!<\/h4>\n<table>\n<tr>\u5bf9\u8c61\u7c7b\u578b\u547d\u540d\u89c4\u5219\u793a\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>\u6570\u636e\u5e93<\/td>\n<td>\u4e1a\u52a1\u6a21\u5757_\u73af\u5883&#xff08;\u5c0f\u5199&#xff09;<\/td>\n<td>analytics_dev<\/td>\n<\/tr>\n<tr>\n<td>\u8868<\/td>\n<td>\u590d\u6570\u540d\u8bcd&#xff08;\u5c0f\u5199\u4e0b\u5212\u7ebf\u5206\u9694&#xff09;<\/td>\n<td>employee_records<\/td>\n<\/tr>\n<tr>\n<td>\u5b57\u6bb5<\/td>\n<td>\u5c0f\u5199\u4e0b\u5212\u7ebf\u547d\u540d<\/td>\n<td>hire_date<\/td>\n<\/tr>\n<tr>\n<td>\u7ea6\u675f<\/td>\n<td>\u8868\u540d_\u7ea6\u675f\u7c7b\u578b_\u5b57\u6bb5\u540d<\/td>\n<td>employees_pk_emp_id<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>1.4.6.2 \u6027\u80fd\u4f18\u5316\u5efa\u8bae<\/h4>\n<ul>\n<li>\n<li>**\u907f\u514d\u4f7f\u7528SELECT ***&#xff1a;\u663e\u5f0f\u5217\u51fa\u6240\u9700\u5b57\u6bb5&#xff0c;\u51cf\u5c11\u6570\u636e\u4f20\u8f93<\/li>\n<\/li>\n<li>\n<li>\u5408\u7406\u4f7f\u7528JOIN&#xff1a;\u4f18\u5148\u5728WHERE\u5b50\u53e5\u6dfb\u52a0\u8fc7\u6ee4\u6761\u4ef6&#xff0c;\u51cf\u5c11\u5173\u8054\u6570\u636e\u91cf<\/li>\n<\/li>\n<li>\n<li>\u6279\u91cf\u64cd\u4f5c&#xff1a;\u4f7f\u7528INSERT &#8230; VALUES (&#8230;),(&#8230;)\u66ff\u4ee3\u5355\u6761\u63d2\u5165<\/li>\n<\/li>\n<li>\n<li>\u4e8b\u52a1\u63a7\u5236&#xff1a;\u5c06\u9ad8\u9891\u5c0f\u4e8b\u52a1\u5408\u5e76&#xff0c;\u51cf\u5c11\u65e5\u5fd7\u5199\u5165\u6b21\u6570<\/li>\n<\/li>\n<\/ul>\n<h4>1.4.6.3 \u6570\u636e\u5b8c\u6574\u6027\u4fdd\u969c<\/h4>\n<ul>\n<li>\u5916\u952e\u7ea6\u675f\u5fc5\u987b\u5b9a\u4e49ON DELETE CASCADE\u6216SET NULL\u884c\u4e3a<\/li>\n<li>\u6838\u5fc3\u4e1a\u52a1\u8868\u542f\u7528TRIGGER\u8fdb\u884c\u6570\u636e\u6821\u9a8c<\/li>\n<li>\u5b9a\u671f\u8fd0\u884cANALYZE\u66f4\u65b0\u7edf\u8ba1\u4fe1\u606f&#xff08;\u5efa\u8bae\u6bcf\u65e5\u51cc\u6668\u6267\u884c&#xff09;<\/li>\n<\/ul>\n<h3>1.4.7 \u5e38\u89c1\u9519\u8bef\u4e0e\u89e3\u51b3\u65b9\u6848<\/h3>\n<h4>1.4.7.1 \u7ea6\u675f\u51b2\u7a81\u5904\u7406<\/h4>\n<table>\n<tr>\u9519\u8bef\u4fe1\u606f\u539f\u56e0\u5206\u6790\u89e3\u51b3\u65b9\u6848<\/tr>\n<tbody>\n<tr>\n<td>\u552f\u4e00\u7ea6\u675f\u51b2\u7a81<\/td>\n<td>\u63d2\u5165\u91cd\u590d\u6570\u636e<\/td>\n<td>\u4f7f\u7528INSERT &#8230; ON CONFLICT UPDATE<\/td>\n<\/tr>\n<tr>\n<td>\u5916\u952e\u7ea6\u675f\u5931\u8d25<\/td>\n<td>\u7236\u8868\u65e0\u5bf9\u5e94\u8bb0\u5f55<\/td>\n<td>\u5148\u63d2\u5165\u7236\u8868\u6570\u636e&#xff0c;\u6216\u8bbe\u7f6eDEFERRABLE\u7ea6\u675f<\/td>\n<\/tr>\n<tr>\n<td>\u68c0\u67e5\u7ea6\u675f\u8fdd\u53cd<\/td>\n<td>\u6570\u636e\u4e0d\u7b26\u5408\u81ea\u5b9a\u4e49\u89c4\u5219<\/td>\n<td>\u6dfb\u52a0\u6570\u636e\u6e05\u6d17\u6b65\u9aa4&#xff0c;\u6216\u4fee\u6539CHECK\u6761\u4ef6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>1.4.7.2 \u5927\u8868\u64cd\u4f5c\u6280\u5de7<\/h4>\n<p><span class=\"token comment\">&#8212; \u5728\u7ebf\u91cd\u547d\u540d\u5927\u8868&#xff08;PostgreSQL 11&#043;&#xff09;<\/span><br \/>\n<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> large_table <span class=\"token keyword\">RENAME<\/span> <span class=\"token keyword\">TO<\/span> new_large_table<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u5206\u6279\u6b21\u5220\u9664\u6570\u636e&#xff08;\u907f\u514d\u9501\u8868&#xff09;<\/span><br \/>\n<span class=\"token comment\">&#8212; \u5f00\u59cb\u4e00\u4e2a\u65e0\u9650\u5faa\u73af&#xff0c;\u7528\u4e8e\u5206\u6279\u6b21\u5220\u9664\u6570\u636e&#xff0c;\u907f\u514d\u4e00\u6b21\u6027\u5220\u9664\u5927\u91cf\u6570\u636e\u5bfc\u81f4\u9501\u8868\u548c\u6027\u80fd\u95ee\u9898<\/span><br \/>\n<span class=\"token keyword\">WHILE<\/span> <span class=\"token boolean\">TRUE<\/span> <span class=\"token keyword\">LOOP<\/span><br \/>\n    <span class=\"token comment\">&#8212; \u4ece old_data \u8868\u4e2d\u5220\u9664\u521b\u5efa\u65f6\u95f4\u65e9\u4e8e &#039;2023-01-01&#039; \u7684\u6570\u636e&#xff0c;\u6bcf\u6b21\u6700\u591a\u5220\u9664 10000 \u6761\u8bb0\u5f55<\/span><br \/>\n    <span class=\"token keyword\">DELETE<\/span> <span class=\"token keyword\">FROM<\/span> old_data <span class=\"token keyword\">WHERE<\/span> create_time <span class=\"token operator\">&lt;<\/span> <span class=\"token string\">&#039;2023-01-01&#039;<\/span> <span class=\"token keyword\">LIMIT<\/span> <span class=\"token number\">10000<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">&#8212; \u68c0\u67e5\u4e0a\u4e00\u6761 DELETE \u8bed\u53e5\u662f\u5426\u5220\u9664\u4e86\u4efb\u4f55\u8bb0\u5f55<\/span><br \/>\n    <span class=\"token comment\">&#8212; \u5982\u679c\u6ca1\u6709\u5220\u9664\u4efb\u4f55\u8bb0\u5f55&#xff08;\u5373 NOT FOUND \u6761\u4ef6\u4e3a\u771f&#xff09;&#xff0c;\u5219\u8bf4\u660e\u5df2\u7ecf\u6ca1\u6709\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e\u9700\u8981\u5220\u9664<\/span><br \/>\n    <span class=\"token keyword\">IF<\/span> <span class=\"token operator\">NOT<\/span> FOUND <span class=\"token keyword\">THEN<\/span><br \/>\n        <span class=\"token comment\">&#8212; \u5f53\u6ca1\u6709\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e\u53ef\u5220\u9664\u65f6&#xff0c;\u9000\u51fa\u5f53\u524d\u7684\u5faa\u73af<\/span><br \/>\n        <span class=\"token keyword\">EXIT<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">END<\/span> <span class=\"token keyword\">IF<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">&#8212; \u63d0\u4ea4\u5f53\u524d\u4e8b\u52a1&#xff0c;\u5c06\u5df2\u5220\u9664\u7684\u6570\u636e\u6c38\u4e45\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u4e2d<\/span><br \/>\n    <span class=\"token comment\">&#8212; \u5206\u6279\u6b21\u63d0\u4ea4\u4e8b\u52a1\u53ef\u4ee5\u51cf\u5c11\u9501\u7684\u6301\u6709\u65f6\u95f4&#xff0c;\u63d0\u9ad8\u5e76\u53d1\u6027\u80fd<\/span><br \/>\n    <span class=\"token keyword\">COMMIT<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">&#8212; \u7ed3\u675f\u5faa\u73af<\/span><br \/>\n<span class=\"token keyword\">END<\/span> <span class=\"token keyword\">LOOP<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h3>1.4.8 \u603b\u7ed3&#xff1a;\u6784\u5efa\u5065\u58ee\u7684\u6570\u636e\u6a21\u578b<\/h3>\n<ul>\n<li>\u901a\u8fc7\u638c\u63e1DDL\u4e0eDML\u7684\u6838\u5fc3\u8bed\u6cd5&#xff0c;\u6211\u4eec\u80fd\u591f&#xff1a;\n<ul>\n<li>\n<li>\u8bbe\u8ba1\u7b26\u5408\u4e1a\u52a1\u9700\u6c42\u7684\u6570\u636e\u8868\u7ed3\u6784&#xff08;\u5408\u7406\u4f7f\u7528\u7ea6\u675f\u4e0e\u7d22\u5f15&#xff09;<\/li>\n<\/li>\n<li>\n<li>\u9ad8\u6548\u64cd\u4f5c\u6570\u636e&#xff08;\u6279\u91cf\u5904\u7406\u3001\u4e8b\u52a1\u63a7\u5236\u3001\u6027\u80fd\u4f18\u5316&#xff09;<\/li>\n<\/li>\n<li>\n<li>\u4fdd\u969c\u6570\u636e\u5b8c\u6574\u6027\u4e0e\u4e00\u81f4\u6027&#xff08;ACID\u7279\u6027\u3001\u5916\u952e\u7ea6\u675f&#xff09;<\/li>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u8be5\u6587\u7ae0\u8bb2\u89e3\u4e86PostgreSQL\u7684DDL\/DML\u64cd\u4f5c\u3002 \u540e\u7eed\u7ae0\u8282\u5c06\u6df1\u5165\u8bb2\u89e3\u6570\u636e\u6e05\u6d17\u6280\u5de7&#xff08;\u4f7f\u7528CTE\u3001\u7a97\u53e3\u51fd\u6570&#xff09;\u3001\u590d\u6742\u67e5\u8be2\u4f18\u5316&#xff08;\u6267\u884c\u8ba1\u5212\u5206\u6790&#xff09;\u4ee5\u53ca\u4e0e\u6570\u636e\u5206\u6790\u5de5\u5177\u7684\u96c6\u6210\u65b9\u6cd5\u3002 \u5efa\u8bae\u5728\u5b9e\u8df5\u4e2d\u9075\u5faa&#034;\u5148\u8bbe\u8ba1ER\u56fe&#xff0c;\u518d\u7f16\u5199\u5efa\u8868\u8bed\u53e5&#034;\u7684\u6d41\u7a0b&#xff0c;\u9010\u6b65\u79ef\u7d2f\u6570\u636e\u5e93\u8bbe\u8ba1\u7ecf\u9a8c\u3002<\/p>\n<ul>\n<li>CTE \u5373\u516c\u5171\u8868\u8868\u8fbe\u5f0f&#xff08;Common Table Expressions&#xff09;&#xff0c;\u5b83\u662f\u5728 SQL \u4e2d\u5b9a\u4e49\u7684\u4e34\u65f6\u547d\u540d\u7ed3\u679c\u96c6&#xff0c;\u8fd9\u4e9b\u7ed3\u679c\u96c6\u53ea\u5728\u5f53\u524d\u67e5\u8be2\u7684\u6267\u884c\u671f\u95f4\u5b58\u5728\u3002\n<ul>\n<li>CTE \u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b&#xff1a;<span class=\"token keyword\">WITH<\/span> cte_name <span class=\"token punctuation\">(<\/span>column1<span class=\"token punctuation\">,<\/span> column2<span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> <span class=\"token punctuation\">(<\/span><br \/>\n    <span class=\"token comment\">&#8212; CTE \u7684\u67e5\u8be2\u8bed\u53e5<\/span><br \/>\n    <span class=\"token keyword\">SELECT<\/span> column1<span class=\"token punctuation\">,<\/span> column2<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\">FROM<\/span> table_name<br \/>\n    <span class=\"token keyword\">WHERE<\/span> condition<br \/>\n<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\">&#8212; \u4e3b\u67e5\u8be2&#xff0c;\u4f7f\u7528 CTE<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span><br \/>\n<span class=\"token keyword\">FROM<\/span> cte_name<span class=\"token punctuation\">;<\/span>\n <\/li>\n<li>\u5176\u4e2d&#xff0c;WITH \u5173\u952e\u5b57\u7528\u4e8e\u5f15\u5165 CTE&#xff0c;cte_name \u662f CTE \u7684\u540d\u79f0&#xff0c;\u62ec\u53f7\u5185\u53ef\u6307\u5b9a\u5217\u540d&#xff0c;AS \u540e\u9762\u7684\u62ec\u53f7\u4e2d\u662f CTE \u7684\u67e5\u8be2\u8bed\u53e5&#xff0c;\u6700\u540e\u662f\u4f7f\u7528\u8be5 CTE \u7684\u4e3b\u67e5\u8be2\u3002<\/li>\n<li>\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879\n<ul>\n<li>CTE \u7684\u4f5c\u7528\u57df&#xff1a; CTE \u53ea\u5728\u5f53\u524d\u67e5\u8be2\u4e2d\u6709\u6548&#xff0c;\u67e5\u8be2\u6267\u884c\u5b8c\u6bd5\u540e&#xff0c;CTE \u5360\u7528\u7684\u8d44\u6e90\u4f1a\u88ab\u91ca\u653e\u3002<\/li>\n<li>\u6027\u80fd\u95ee\u9898&#xff1a;\u867d\u7136 CTE \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd&#xff0c;\u4f46\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u590d\u6742\u9012\u5f52\u65f6&#xff0c;\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u3002\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\u548c\u4f18\u5316\u3002<\/li>\n<li>\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6&#xff1a;\u5728\u4f7f\u7528\u9012\u5f52 CTE \u65f6&#xff0c;\u5fc5\u987b\u786e\u4fdd\u9012\u5f52\u6709\u7ec8\u6b62\u6761\u4ef6&#xff0c;\u5426\u5219\u4f1a\u5bfc\u81f4\u65e0\u9650\u9012\u5f52&#xff0c;\u4f7f\u6570\u636e\u5e93\u9677\u5165\u6b7b\u5faa\u73af\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e20\u6b21\uff0c\u6536\u85cf20\u6b21\u3002\u3010PostgreSQL\u6570\u636e\u5206\u6790\u5b9e\u6218\uff1a\u4ece\u6570\u636e\u6e05\u6d17\u5230\u53ef\u89c6\u5316\u5168\u6d41\u7a0b\u30111.4 \u6570\u636e\u5e93\u4e0e\u8868\u7684\u57fa\u672c\u64cd\u4f5c\uff08DDL\/DML\u8bed\u53e5\uff09<\/p>\n","protected":false},"author":2,"featured_media":38172,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3321,3320,954,801,100],"topic":[],"class_list":["post-38175","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-age","tag-cte","tag-postgresql","tag-801","tag-100"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010PostgreSQL\u6570\u636e\u5206\u6790\u5b9e\u6218\uff1a\u4ece\u6570\u636e\u6e05\u6d17\u5230\u53ef\u89c6\u5316\u5168\u6d41\u7a0b\u30111.4 \u6570\u636e\u5e93\u4e0e\u8868\u7684\u57fa\u672c\u64cd\u4f5c\uff08DDL\/DML\u8bed\u53e5\uff09 - \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\/38175.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010PostgreSQL\u6570\u636e\u5206\u6790\u5b9e\u6218\uff1a\u4ece\u6570\u636e\u6e05\u6d17\u5230\u53ef\u89c6\u5316\u5168\u6d41\u7a0b\u30111.4 \u6570\u636e\u5e93\u4e0e\u8868\u7684\u57fa\u672c\u64cd\u4f5c\uff08DDL\/DML\u8bed\u53e5\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e20\u6b21\uff0c\u6536\u85cf20\u6b21\u3002\u3010PostgreSQL\u6570\u636e\u5206\u6790\u5b9e\u6218\uff1a\u4ece\u6570\u636e\u6e05\u6d17\u5230\u53ef\u89c6\u5316\u5168\u6d41\u7a0b\u30111.4 \u6570\u636e\u5e93\u4e0e\u8868\u7684\u57fa\u672c\u64cd\u4f5c\uff08DDL\/DML\u8bed\u53e5\uff09\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/38175.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-19T17:04:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519170403-682b64831c8f5.png\" \/>\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=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/38175.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/38175.html\",\"name\":\"\u3010PostgreSQL\u6570\u636e\u5206\u6790\u5b9e\u6218\uff1a\u4ece\u6570\u636e\u6e05\u6d17\u5230\u53ef\u89c6\u5316\u5168\u6d41\u7a0b\u30111.4 \u6570\u636e\u5e93\u4e0e\u8868\u7684\u57fa\u672c\u64cd\u4f5c\uff08DDL\/DML\u8bed\u53e5\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-19T17:04:04+00:00\",\"dateModified\":\"2025-05-19T17:04:04+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/38175.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/38175.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/38175.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010PostgreSQL\u6570\u636e\u5206\u6790\u5b9e\u6218\uff1a\u4ece\u6570\u636e\u6e05\u6d17\u5230\u53ef\u89c6\u5316\u5168\u6d41\u7a0b\u30111.4 \u6570\u636e\u5e93\u4e0e\u8868\u7684\u57fa\u672c\u64cd\u4f5c\uff08DDL\/DML\u8bed\u53e5\uff09\"}]},{\"@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":"\u3010PostgreSQL\u6570\u636e\u5206\u6790\u5b9e\u6218\uff1a\u4ece\u6570\u636e\u6e05\u6d17\u5230\u53ef\u89c6\u5316\u5168\u6d41\u7a0b\u30111.4 \u6570\u636e\u5e93\u4e0e\u8868\u7684\u57fa\u672c\u64cd\u4f5c\uff08DDL\/DML\u8bed\u53e5\uff09 - \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\/38175.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010PostgreSQL\u6570\u636e\u5206\u6790\u5b9e\u6218\uff1a\u4ece\u6570\u636e\u6e05\u6d17\u5230\u53ef\u89c6\u5316\u5168\u6d41\u7a0b\u30111.4 \u6570\u636e\u5e93\u4e0e\u8868\u7684\u57fa\u672c\u64cd\u4f5c\uff08DDL\/DML\u8bed\u53e5\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e20\u6b21\uff0c\u6536\u85cf20\u6b21\u3002\u3010PostgreSQL\u6570\u636e\u5206\u6790\u5b9e\u6218\uff1a\u4ece\u6570\u636e\u6e05\u6d17\u5230\u53ef\u89c6\u5316\u5168\u6d41\u7a0b\u30111.4 \u6570\u636e\u5e93\u4e0e\u8868\u7684\u57fa\u672c\u64cd\u4f5c\uff08DDL\/DML\u8bed\u53e5\uff09","og_url":"https:\/\/www.wsisp.com\/helps\/38175.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-19T17:04:04+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519170403-682b64831c8f5.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/38175.html","url":"https:\/\/www.wsisp.com\/helps\/38175.html","name":"\u3010PostgreSQL\u6570\u636e\u5206\u6790\u5b9e\u6218\uff1a\u4ece\u6570\u636e\u6e05\u6d17\u5230\u53ef\u89c6\u5316\u5168\u6d41\u7a0b\u30111.4 \u6570\u636e\u5e93\u4e0e\u8868\u7684\u57fa\u672c\u64cd\u4f5c\uff08DDL\/DML\u8bed\u53e5\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-19T17:04:04+00:00","dateModified":"2025-05-19T17:04:04+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/38175.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/38175.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/38175.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010PostgreSQL\u6570\u636e\u5206\u6790\u5b9e\u6218\uff1a\u4ece\u6570\u636e\u6e05\u6d17\u5230\u53ef\u89c6\u5316\u5168\u6d41\u7a0b\u30111.4 \u6570\u636e\u5e93\u4e0e\u8868\u7684\u57fa\u672c\u64cd\u4f5c\uff08DDL\/DML\u8bed\u53e5\uff09"}]},{"@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\/38175","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=38175"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/38175\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/38172"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=38175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=38175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=38175"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=38175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}