{"id":77634,"date":"2024-10-16T11:00:34","date_gmt":"2024-10-16T03:00:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/77634\/"},"modified":"2024-10-16T11:00:34","modified_gmt":"2024-10-16T03:00:34","slug":"mysql%e6%8c%87%e4%bb%a4-alter-table%ef%bc%88%e4%bf%ae%e6%94%b9%e6%95%b8%e6%93%9a%e8%a1%a8%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/77634\/","title":{"rendered":"MySQL\u6307\u4ee4: ALTER TABLE\uff08\u4fee\u6539\u6578\u64da\u8868\uff09"},"content":{"rendered":"<h1 id=\"mysql%e6%8c%87%e4%bb%a4-alter-table%ef%bc%88%e4%bf%ae%e6%94%b9%e6%95%b8%e6%93%9a%e8%a1%a8%ef%bc%89-gjHTkpzlqL\">MySQL\u6307\u4ee4: ALTER TABLE\uff08\u4fee\u6539\u6578\u64da\u8868\uff09<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\uff0cMySQL\u662f\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u3002\u7576\u6211\u5011\u9700\u8981\u5c0d\u5df2\u5b58\u5728\u7684\u6578\u64da\u8868\u9032\u884c\u4fee\u6539\u6642\uff0c<code>ALTER TABLE<\/code>\u6307\u4ee4\u4fbf\u662f\u6211\u5011\u7684\u6700\u4f73\u9078\u64c7\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u6df1\u5165\u63a2\u8a0e<code>ALTER TABLE<\/code>\u7684\u7528\u6cd5\u53ca\u5176\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\u7684\u91cd\u8981\u6027\u3002<\/p>\n<h2 id=\"alter-table%e7%9a%84%e5%9f%ba%e6%9c%ac%e8%aa%9e%e6%b3%95-gjHTkpzlqL\">ALTER TABLE\u7684\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<p><code>ALTER TABLE<\/code>\u6307\u4ee4\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>ALTER TABLE table_name\n    action;<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c<code>table_name<\/code>\u662f\u4f60\u60f3\u8981\u4fee\u6539\u7684\u6578\u64da\u8868\u540d\u7a31\uff0c\u800c<code>action<\/code>\u5247\u662f\u4f60\u5e0c\u671b\u57f7\u884c\u7684\u5177\u9ad4\u64cd\u4f5c\u3002\u9019\u4e9b\u64cd\u4f5c\u53ef\u4ee5\u5305\u62ec\u6dfb\u52a0\u3001\u522a\u9664\u6216\u4fee\u6539\u5217\uff0c\u751a\u81f3\u6539\u8b8a\u8868\u7684\u540d\u7a31\u3002<\/p>\n<h2 id=\"%e5%b8%b8%e8%a6%8b%e7%9a%84alter-table%e6%93%8d%e4%bd%9c-gjHTkpzlqL\">\u5e38\u898b\u7684ALTER TABLE\u64cd\u4f5c<\/h2>\n<h3 id=\"1-%e6%b7%bb%e5%8a%a0%e6%96%b0%e5%88%97-gjHTkpzlqL\">1. \u6dfb\u52a0\u65b0\u5217<\/h3>\n<p>\u5982\u679c\u4f60\u9700\u8981\u5728\u6578\u64da\u8868\u4e2d\u6dfb\u52a0\u4e00\u500b\u65b0\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>ALTER TABLE table_name\n    ADD column_name column_type;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba<code>employees<\/code>\u7684\u8868\uff0c\u4e26\u5e0c\u671b\u6dfb\u52a0\u4e00\u500b\u540d\u70ba<code>birthdate<\/code>\u7684\u65e5\u671f\u5217\uff1a<\/p>\n<pre><code>ALTER TABLE employees\n    ADD birthdate DATE;<\/code><\/pre>\n<h3 id=\"2-%e5%88%aa%e9%99%a4%e5%88%97-gjHTkpzlqL\">2. \u522a\u9664\u5217<\/h3>\n<p>\u82e5\u8981\u522a\u9664\u4e00\u500b\u5df2\u5b58\u5728\u7684\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>ALTER TABLE table_name\n    DROP COLUMN column_name;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5f9e<code>employees<\/code>\u8868\u4e2d\u522a\u9664<code>birthdate<\/code>\u5217\uff1a<\/p>\n<pre><code>ALTER TABLE employees\n    DROP COLUMN birthdate;<\/code><\/pre>\n<h3 id=\"3-%e4%bf%ae%e6%94%b9%e5%88%97%e7%9a%84%e6%95%b8%e6%93%9a%e9%a1%9e%e5%9e%8b-gjHTkpzlqL\">3. \u4fee\u6539\u5217\u7684\u6578\u64da\u985e\u578b<\/h3>\n<p>\u5982\u679c\u9700\u8981\u66f4\u6539\u67d0\u4e00\u5217\u7684\u6578\u64da\u985e\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>ALTER TABLE table_name\n    MODIFY COLUMN column_name new_column_type;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5c07<code>employees<\/code>\u8868\u4e2d\u7684<code>salary<\/code>\u5217\u7684\u6578\u64da\u985e\u578b\u5f9e\u6574\u6578\u6539\u70ba\u6d6e\u9ede\u6578\uff1a<\/p>\n<pre><code>ALTER TABLE employees\n    MODIFY COLUMN salary FLOAT;<\/code><\/pre>\n<h3 id=\"4-%e9%87%8d%e5%91%bd%e5%90%8d%e5%88%97-gjHTkpzlqL\">4. \u91cd\u547d\u540d\u5217<\/h3>\n<p>\u82e5\u8981\u91cd\u547d\u540d\u67d0\u4e00\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>ALTER TABLE table_name\n    CHANGE old_column_name new_column_name column_type;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5c07<code>employees<\/code>\u8868\u4e2d\u7684<code>birthdate<\/code>\u5217\u91cd\u547d\u540d\u70ba<code>date_of_birth<\/code>\uff1a<\/p>\n<pre><code>ALTER TABLE employees\n    CHANGE birthdate date_of_birth DATE;<\/code><\/pre>\n<h3 id=\"5-%e9%87%8d%e5%91%bd%e5%90%8d%e6%95%b8%e6%93%9a%e8%a1%a8-gjHTkpzlqL\">5. \u91cd\u547d\u540d\u6578\u64da\u8868<\/h3>\n<p>\u5982\u679c\u9700\u8981\u91cd\u547d\u540d\u6574\u500b\u6578\u64da\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>ALTER TABLE old_table_name\n    RENAME TO new_table_name;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5c07<code>employees<\/code>\u8868\u91cd\u547d\u540d\u70ba<code>staff<\/code>\uff1a<\/p>\n<pre><code>ALTER TABLE employees\n    RENAME TO staff;<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-gjHTkpzlqL\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4f7f\u7528<code>ALTER TABLE<\/code>\u6307\u4ee4\u6642\uff0c\u6709\u5e7e\u9ede\u9700\u8981\u7279\u5225\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>\u5728\u9032\u884c\u7d50\u69cb\u4fee\u6539\u6642\uff0c\u5efa\u8b70\u5148\u5099\u4efd\u6578\u64da\uff0c\u4ee5\u9632\u6b62\u610f\u5916\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<li>\u67d0\u4e9b\u64cd\u4f5c\u53ef\u80fd\u6703\u5f71\u97ff\u6578\u64da\u8868\u7684\u6027\u80fd\uff0c\u7279\u5225\u662f\u5728\u5927\u578b\u6578\u64da\u8868\u4e0a\u57f7\u884c\u6642\u3002<\/li>\n<li>\u5728\u4fee\u6539\u5217\u7684\u6578\u64da\u985e\u578b\u6642\uff0c\u9700\u78ba\u4fdd\u65b0\u985e\u578b\u80fd\u5920\u517c\u5bb9\u73fe\u6709\u6578\u64da\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-gjHTkpzlqL\">\u7e3d\u7d50<\/h2>\n<p>MySQL\u7684<code>ALTER TABLE<\/code>\u6307\u4ee4\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u7528\u6236\u9748\u6d3b\u5730\u7ba1\u7406\u6578\u64da\u8868\u7d50\u69cb\u3002\u7121\u8ad6\u662f\u6dfb\u52a0\u65b0\u5217\u3001\u522a\u9664\u4e0d\u9700\u8981\u7684\u5217\uff0c\u9084\u662f\u4fee\u6539\u5217\u7684\u6578\u64da\u985e\u578b\uff0c\u9019\u4e9b\u64cd\u4f5c\u90fd\u80fd\u5920\u6709\u6548\u5730\u63d0\u5347\u6578\u64da\u5eab\u7684\u9748\u6d3b\u6027\u548c\u53ef\u7528\u6027\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u7ba1\u7406\u7684\u7528\u6236\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684<code>VPS<\/code>\u89e3\u6c7a\u65b9\u6848\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u4e86\u89e3\u66f4\u591a\u95dc\u65bc<code><a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a><\/code>\u548c<code><a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a><\/code>\u7684\u8cc7\u8a0a\uff0c\u5c07\u6709\u52a9\u65bc\u60a8\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e0a\u505a\u51fa\u66f4\u660e\u667a\u7684\u6c7a\u7b56\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MySQL\u6307\u4ee4ALTER TABLE\uff0c\u5b78\u7fd2\u5982\u4f55\u4fee\u6539\u6578\u64da\u8868\u7d50\u69cb\uff0c\u5305\u62ec\u6dfb\u52a0\u3001\u522a\u9664\u6216\u66f4\u6539\u5217\u53ca\u7d04\u675f\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-77634","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/77634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=77634"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/77634\/revisions"}],"predecessor-version":[{"id":77635,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/77634\/revisions\/77635"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=77634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=77634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=77634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}