{"id":2673,"date":"2025-03-12T23:41:08","date_gmt":"2025-03-12T15:41:08","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2673"},"modified":"2025-03-12T23:41:08","modified_gmt":"2025-03-12T15:41:08","slug":"postgresql-alter-table-%e5%91%bd%e4%bb%a4%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/12\/postgresql-alter-table-%e5%91%bd%e4%bb%a4%e8%af%a6%e8%a7%a3\/","title":{"rendered":"PostgreSQL ALTER TABLE \u547d\u4ee4\u8be6\u89e3"},"content":{"rendered":"\n<p>PostgreSQL <code>ALTER TABLE<\/code> \u547d\u4ee4\u7528\u4e8e <strong>\u4fee\u6539\u5df2\u6709\u8868\u7684\u7ed3\u6784<\/strong>\uff0c\u5305\u62ec\u6dfb\u52a0\u3001\u4fee\u6539\u3001\u5220\u9664\u5217\uff0c\u6dfb\u52a0\u7ea6\u675f\uff0c\u4fee\u6539\u5b58\u50a8\u8bbe\u7f6e\u7b49\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u57fa\u672c\u8bed\u6cd5<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE table_name action;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>table_name<\/code>\uff1a\u8981\u4fee\u6539\u7684\u8868\u540d\u3002<\/li>\n\n\n\n<li><code>action<\/code>\uff1a\u4fee\u6539\u8868\u7684\u5177\u4f53\u64cd\u4f5c\uff08\u5982\u6dfb\u52a0\u5217\u3001\u5220\u9664\u5217\u3001\u66f4\u6539\u6570\u636e\u7c7b\u578b\u7b49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u6dfb\u52a0\u5217<\/strong><\/h2>\n\n\n\n<p>\u4f7f\u7528 <code>ADD COLUMN<\/code> \u6dfb\u52a0\u65b0\u7684\u5217\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u6dfb\u52a0\u5355\u4e2a\u5217<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees ADD COLUMN age INT;<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees ADD COLUMN department VARCHAR(50);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u6dfb\u52a0\u591a\u4e2a\u5217<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees \nADD COLUMN phone VARCHAR(15), \nADD COLUMN address TEXT;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u4fee\u6539\u5217<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u4fee\u6539\u5217\u7684\u6570\u636e\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees ALTER COLUMN age TYPE BIGINT;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 \u4fee\u6539\u5217\u7684\u9ed8\u8ba4\u503c<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees ALTER COLUMN age SET DEFAULT 25;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.3 \u5220\u9664\u5217\u7684\u9ed8\u8ba4\u503c<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees ALTER COLUMN age DROP DEFAULT;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.4 \u4fee\u6539\u5217\u7684 NOT NULL \u7ea6\u675f<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6dfb\u52a0 <code>NOT NULL<\/code><\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  ALTER TABLE employees ALTER COLUMN age SET NOT NULL;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5220\u9664 <code>NOT NULL<\/code><\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  ALTER TABLE employees ALTER COLUMN age DROP NOT NULL;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.5 \u4fee\u6539\u5217\u7684\u540d\u79f0<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees RENAME COLUMN age TO emp_age;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u5220\u9664\u5217<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u5220\u9664\u5355\u4e2a\u5217<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees DROP COLUMN phone;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2 \u5220\u9664\u591a\u4e2a\u5217<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees \nDROP COLUMN phone, \nDROP COLUMN address;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u91cd\u547d\u540d\u8868<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees RENAME TO staff;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u7ea6\u675f\u64cd\u4f5c<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6.1 \u6dfb\u52a0\u4e3b\u952e<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees ADD CONSTRAINT emp_pk PRIMARY KEY (id);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6.2 \u5220\u9664\u4e3b\u952e<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees DROP CONSTRAINT emp_pk;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6.3 \u6dfb\u52a0\u552f\u4e00\u7ea6\u675f<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees ADD CONSTRAINT unique_email UNIQUE (email);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6.4 \u6dfb\u52a0\u5916\u952e<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees ADD CONSTRAINT emp_dept_fk FOREIGN KEY (dept_id) REFERENCES departments(id);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6.5 \u5220\u9664\u5916\u952e<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees DROP CONSTRAINT emp_dept_fk;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u4fee\u6539\u8868\u7684\u5b58\u50a8\u8bbe\u7f6e<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.1 \u8bbe\u7f6e\u8868\u7684 <code>FILLFACTOR<\/code><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees SET (FILLFACTOR = 70);<\/code><\/pre>\n\n\n\n<p><code>FILLFACTOR<\/code> \u63a7\u5236\u8868\u7684\u6570\u636e\u5757\u586b\u5145\u6bd4\u4f8b\uff0c\u9ed8\u8ba4 <code>100<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.2 \u89e3\u9664\u8868\u7684\u5b58\u50a8\u53c2\u6570<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees RESET (FILLFACTOR);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8. \u8bbe\u7f6e\u8868\u7684\u7ee7\u627f<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees INHERIT company_records;<\/code><\/pre>\n\n\n\n<p><strong>\u53d6\u6d88\u7ee7\u627f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees NO INHERIT company_records;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9. \u7981\u7528\u6216\u542f\u7528\u89e6\u53d1\u5668<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9.1 \u7981\u7528\u89e6\u53d1\u5668<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees DISABLE TRIGGER ALL;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9.2 \u542f\u7528\u89e6\u53d1\u5668<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees ENABLE TRIGGER ALL;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>10. \u4f8b\u5b50\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u5b8c\u6574\u4fee\u6539\u8868<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees \nADD COLUMN salary NUMERIC(10,2),\nALTER COLUMN age TYPE SMALLINT,\nALTER COLUMN email SET NOT NULL,\nDROP COLUMN phone,\nRENAME COLUMN address TO home_address,\nADD CONSTRAINT unique_email UNIQUE (email);<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6761\u547d\u4ee4\u505a\u4e86\u4ee5\u4e0b\u4fee\u6539\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6dfb\u52a0 <code>salary<\/code> \u5217\u3002<\/li>\n\n\n\n<li>\u4fee\u6539 <code>age<\/code> \u7684\u6570\u636e\u7c7b\u578b\u4e3a <code>SMALLINT<\/code>\u3002<\/li>\n\n\n\n<li>\u8bbe\u7f6e <code>email<\/code> \u5217 <code>NOT NULL<\/code> \u7ea6\u675f\u3002<\/li>\n\n\n\n<li>\u5220\u9664 <code>phone<\/code> \u5217\u3002<\/li>\n\n\n\n<li>\u5c06 <code>address<\/code> \u5217\u6539\u540d\u4e3a <code>home_address<\/code>\u3002<\/li>\n\n\n\n<li>\u6dfb\u52a0 <code>email<\/code> \u5217\u7684\u552f\u4e00\u7ea6\u675f\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>11. \u4f7f\u7528 <code>ALTER TABLE<\/code> \u65f6\u7684\u6ce8\u610f\u4e8b\u9879<\/strong><\/h2>\n\n\n\n<p>\u2705 <strong>\u5220\u9664\u5217\u65f6\u8981\u8c28\u614e<\/strong>\uff1a<code>DROP COLUMN<\/code> <strong>\u4e0d\u53ef\u9006<\/strong>\uff0c\u53ef\u80fd\u4f1a\u4e22\u5931\u91cd\u8981\u6570\u636e\u3002<br>\u2705 <strong>\u4fee\u6539\u6570\u636e\u7c7b\u578b\u65f6\u6ce8\u610f\u517c\u5bb9\u6027<\/strong>\uff1a\u65b0\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u80fd\u517c\u5bb9\u5df2\u6709\u6570\u636e\uff0c\u5426\u5219\u9700\u8981\u624b\u52a8\u8f6c\u6362\u3002<br>\u2705 <strong>\u6dfb\u52a0\u7ea6\u675f\u53ef\u80fd\u5f71\u54cd\u6027\u80fd<\/strong>\uff1a<code>NOT NULL<\/code>\u3001<code>UNIQUE<\/code>\u3001<code>FOREIGN KEY<\/code> \u53ef\u80fd\u4f1a\u964d\u4f4e\u63d2\u5165\u548c\u66f4\u65b0\u901f\u5ea6\u3002<br>\u2705 <strong>\u5c3d\u91cf\u907f\u514d\u5728\u5927\u8868\u4e0a\u76f4\u63a5\u4fee\u6539<\/strong>\uff1a<code>ALTER TABLE<\/code> \u53ef\u80fd\u4f1a\u9501\u8868\uff0c\u5f71\u54cd\u6027\u80fd\u3002\u5bf9\u4e8e\u5927\u8868\uff0c\u5efa\u8bae\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u521b\u5efa\u65b0\u8868\uff0c\u590d\u5236\u6570\u636e\uff0c\u7136\u540e\u91cd\u547d\u540d\u8868<\/strong><\/li>\n\n\n\n<li><strong>\u4f7f\u7528 <code>CONCURRENTLY<\/code> \u9009\u9879\uff08\u7d22\u5f15\u76f8\u5173\uff09<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><code>ALTER TABLE<\/code> \u662f\u7ba1\u7406 PostgreSQL \u8868\u7ed3\u6784\u7684\u91cd\u8981\u5de5\u5177\uff0c\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PostgreSQL ALTER TABLE \u547d\u4ee4\u7528\u4e8e \u4fee\u6539\u5df2\u6709\u8868\u7684\u7ed3\u6784\uff0c\u5305\u62ec\u6dfb\u52a0\u3001\u4fee\u6539\u3001\u5220\u9664\u5217\uff0c\u6dfb\u52a0\u7ea6\u675f\uff0c\u4fee [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2674,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-2673","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2673","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2673"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2673\/revisions"}],"predecessor-version":[{"id":2675,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2673\/revisions\/2675"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2674"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}