{"id":66449,"date":"2024-10-14T17:37:15","date_gmt":"2024-10-14T09:37:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/66449\/"},"modified":"2024-10-14T17:37:15","modified_gmt":"2024-10-14T09:37:15","slug":"mysql-alter%e8%aa%9e%e6%b3%95%e7%9a%84%e9%81%8b%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/66449\/","title":{"rendered":"MySQL ALTER\u8a9e\u6cd5\u7684\u904b\u7528"},"content":{"rendered":"<h1 id=\"mysql-alter%e8%aa%9e%e6%b3%95%e7%9a%84%e9%81%8b%e7%94%a8-mrHyZvJrnt\">MySQL ALTER\u8a9e\u6cd5\u7684\u904b\u7528<\/h1>\n<p>\u5728\u8cc7\u6599\u5eab\u7ba1\u7406\u4e2d\uff0cMySQL\u662f\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u95dc\u806f\u5f0f\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u3002\u7576\u6211\u5011\u9700\u8981\u5c0d\u8cc7\u6599\u8868\u9032\u884c\u7d50\u69cb\u4e0a\u7684\u4fee\u6539\u6642\uff0cALTER\u8a9e\u6cd5\u4fbf\u6210\u70ba\u4e86\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eMySQL\u7684ALTER\u8a9e\u6cd5\u53ca\u5176\u904b\u7528\uff0c\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u8cc7\u6599\u5eab\u7d50\u69cb\u3002<\/p>\n<h2 id=\"alter%e8%aa%9e%e6%b3%95%e6%a6%82%e8%bf%b0-mrHyZvJrnt\">ALTER\u8a9e\u6cd5\u6982\u8ff0<\/h2>\n<p>ALTER\u8a9e\u6cd5\u4e3b\u8981\u7528\u65bc\u4fee\u6539\u5df2\u5b58\u5728\u7684\u8cc7\u6599\u8868\u7d50\u69cb\u3002\u9019\u5305\u62ec\u6dfb\u52a0\u3001\u522a\u9664\u6216\u4fee\u6539\u8cc7\u6599\u8868\u4e2d\u7684\u6b04\u4f4d\uff0c\u6216\u662f\u6539\u8b8a\u8cc7\u6599\u8868\u7684\u5176\u4ed6\u5c6c\u6027\u3002\u57fa\u672c\u7684\u8a9e\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>ALTER TABLE table_name action;<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c<code>table_name<\/code>\u662f\u8981\u4fee\u6539\u7684\u8cc7\u6599\u8868\u540d\u7a31\uff0c\u800c<code>action<\/code>\u5247\u662f\u5177\u9ad4\u7684\u4fee\u6539\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"%e5%b8%b8%e8%a6%8b%e7%9a%84alter%e6%93%8d%e4%bd%9c-mrHyZvJrnt\">\u5e38\u898b\u7684ALTER\u64cd\u4f5c<\/h2>\n<h3 id=\"1-%e6%b7%bb%e5%8a%a0%e6%ac%84%e4%bd%8d-mrHyZvJrnt\">1. \u6dfb\u52a0\u6b04\u4f4d<\/h3>\n<p>\u82e5\u8981\u5728\u8cc7\u6599\u8868\u4e2d\u6dfb\u52a0\u65b0\u7684\u6b04\u4f4d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>ALTER TABLE table_name ADD column_name column_type;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u8981\u5728\u540d\u70ba<code>employees<\/code>\u7684\u8cc7\u6599\u8868\u4e2d\u6dfb\u52a0\u4e00\u500b\u540d\u70ba<code>birthdate<\/code>\u7684\u65e5\u671f\u6b04\u4f4d\uff0c\u53ef\u4ee5\u9019\u6a23\u5beb\uff1a<\/p>\n<pre><code>ALTER TABLE employees ADD birthdate DATE;<\/code><\/pre>\n<h3 id=\"2-%e5%88%aa%e9%99%a4%e6%ac%84%e4%bd%8d-mrHyZvJrnt\">2. \u522a\u9664\u6b04\u4f4d<\/h3>\n<p>\u82e5\u8981\u522a\u9664\u8cc7\u6599\u8868\u4e2d\u7684\u67d0\u500b\u6b04\u4f4d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>ALTER TABLE table_name DROP COLUMN column_name;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u8981\u5f9e<code>employees<\/code>\u8cc7\u6599\u8868\u4e2d\u522a\u9664<code>birthdate<\/code>\u6b04\u4f4d\uff0c\u53ef\u4ee5\u9019\u6a23\u5beb\uff1a<\/p>\n<pre><code>ALTER TABLE employees DROP COLUMN birthdate;<\/code><\/pre>\n<h3 id=\"3-%e4%bf%ae%e6%94%b9%e6%ac%84%e4%bd%8d-mrHyZvJrnt\">3. \u4fee\u6539\u6b04\u4f4d<\/h3>\n<p>\u82e5\u8981\u4fee\u6539\u5df2\u5b58\u5728\u7684\u6b04\u4f4d\u5c6c\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>ALTER TABLE table_name MODIFY column_name new_column_type;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u8981\u5c07<code>employees<\/code>\u8cc7\u6599\u8868\u4e2d\u7684<code>salary<\/code>\u6b04\u4f4d\u985e\u578b\u6539\u70ba<code>DECIMAL(10,2)<\/code>\uff0c\u53ef\u4ee5\u9019\u6a23\u5beb\uff1a<\/p>\n<pre><code>ALTER TABLE employees MODIFY salary DECIMAL(10,2);<\/code><\/pre>\n<h3 id=\"4-%e9%87%8d%e5%91%bd%e5%90%8d%e6%ac%84%e4%bd%8d-mrHyZvJrnt\">4. \u91cd\u547d\u540d\u6b04\u4f4d<\/h3>\n<p>\u82e5\u8981\u91cd\u547d\u540d\u8cc7\u6599\u8868\u4e2d\u7684\u6b04\u4f4d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>ALTER TABLE table_name CHANGE old_column_name new_column_name column_type;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u8981\u5c07<code>employees<\/code>\u8cc7\u6599\u8868\u4e2d\u7684<code>salary<\/code>\u6b04\u4f4d\u91cd\u547d\u540d\u70ba<code>monthly_salary<\/code>\uff0c\u53ef\u4ee5\u9019\u6a23\u5beb\uff1a<\/p>\n<pre><code>ALTER TABLE employees CHANGE salary monthly_salary DECIMAL(10,2);<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-mrHyZvJrnt\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li>\u5728\u9032\u884cALTER\u64cd\u4f5c\u4e4b\u524d\uff0c\u5efa\u8b70\u5148\u5099\u4efd\u8cc7\u6599\u8868\uff0c\u4ee5\u9632\u6b62\u610f\u5916\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<li>\u67d0\u4e9bALTER\u64cd\u4f5c\u53ef\u80fd\u6703\u9396\u5b9a\u8cc7\u6599\u8868\uff0c\u5f71\u97ff\u5176\u4ed6\u7528\u6236\u7684\u8a2a\u554f\uff0c\u56e0\u6b64\u5728\u9ad8\u6d41\u91cf\u6642\u6bb5\u61c9\u8b39\u614e\u64cd\u4f5c\u3002<\/li>\n<li>\u4e0d\u540c\u7248\u672c\u7684MySQL\u5c0dALTER\u8a9e\u6cd5\u7684\u652f\u6301\u53ef\u80fd\u6709\u6240\u4e0d\u540c\uff0c\u5efa\u8b70\u67e5\u95b1\u5b98\u65b9\u6587\u6a94\u4ee5\u7372\u53d6\u6700\u65b0\u8cc7\u8a0a\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-mrHyZvJrnt\">\u7e3d\u7d50<\/h2>\n<p>MySQL\u7684ALTER\u8a9e\u6cd5\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u7528\u6236\u9748\u6d3b\u5730\u7ba1\u7406\u8cc7\u6599\u8868\u7d50\u69cb\u3002\u7121\u8ad6\u662f\u6dfb\u52a0\u3001\u522a\u9664\u9084\u662f\u4fee\u6539\u6b04\u4f4d\uff0c\u6b63\u78ba\u4f7f\u7528ALTER\u8a9e\u6cd5\u90fd\u80fd\u63d0\u9ad8\u8cc7\u6599\u5eab\u7684\u6548\u7387\u548c\u53ef\u7dad\u8b77\u6027\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u80fd\u548c\u7a69\u5b9a\u6027\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684<code><a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a><\/code>\u6216<code><a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a><\/code>\u89e3\u6c7a\u65b9\u6848\u4e5f\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u4e86\u89e3MySQL\u7684ALTER\u8a9e\u6cd5\uff0c\u5c07\u6709\u52a9\u65bc\u60a8\u5728\u8cc7\u6599\u5eab\u7ba1\u7406\u4e2d\u6e38\u5203\u6709\u9918\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MySQL ALTER\u8a9e\u6cd5\u7684\u904b\u7528\uff0c\u5b78\u7fd2\u5982\u4f55\u4fee\u6539\u8cc7\u6599\u8868\u7d50\u69cb\u3001\u6dfb\u52a0\u6b04\u4f4d\u53ca\u66f4\u65b0\u7d22\u5f15\uff0c\u63d0\u5347\u6578\u64da\u7ba1\u7406\u6548\u7387\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-66449","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/66449","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=66449"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/66449\/revisions"}],"predecessor-version":[{"id":66450,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/66449\/revisions\/66450"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=66449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=66449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=66449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}