{"id":124,"date":"2024-06-17T22:07:22","date_gmt":"2024-06-17T14:07:22","guid":{"rendered":"https:\/\/www.jiwenbo.top\/?p=124"},"modified":"2024-06-17T22:07:22","modified_gmt":"2024-06-17T14:07:22","slug":"mysql%e5%9f%ba%e7%a1%80%ef%bc%9addl%e7%af%87","status":"publish","type":"post","link":"https:\/\/jiwenbo.top\/index.php\/2024\/06\/17\/mysql%e5%9f%ba%e7%a1%80%ef%bc%9addl%e7%af%87\/","title":{"rendered":"MySql\u57fa\u7840\u7bc7\uff1aDDL\u7bc7"},"content":{"rendered":"\n<ul class=\"wp-block-list\">\n<li>\u6570\u636e\u5e93\u64cd\u4f5c<\/li>\n<\/ul>\n\n\n\n<p>\u4e00.\u67e5\u8be2\u6570\u636e\u5e93<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">SHOW DATABASE();<\/pre>\n\n\n\n<p>\u4e8c.\u67e5\u8be2\u6240\u6709\u6570\u636e\u5e93<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">SELECT DATABASE();<\/pre>\n\n\n\n<p>\u4e09.\u521b\u5efa\u6570\u636e\u5e93<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">CREATE DATABASE [IF NOT EXISTS] \u6570\u636e\u5e93\u540d\u5b57 [DAFAULT \u5b57\u7b26\u96c6] [collate \u6392\u5e8f\u89c4\u5219];<\/pre>\n\n\n\n<p>\u56db.\u5220\u9664\u6570\u636e\u5e93<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">DROP DATABASE[IF EXISTS]\u6570\u636e\u5e93\u540d;<\/pre>\n\n\n\n<p>\u4e94.\u9009\u62e9\u6570\u636e\u5e93<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">USE \u6570\u636e\u5e93\u540d;<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8868\u64cd\u4f5c<\/li>\n<\/ul>\n\n\n\n<p>\u4e00.\u521b\u5efa\u8868<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">create table \u8868\u540d {<br>\u5217\u540d,\u6570\u636e\u7c7b\u578b,\u7ea6\u675f\u6761\u4ef6<br>};<\/pre>\n\n\n\n<p>\u4e8c.\u67e5\u770b\u8868\u5b9a\u4e49<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">desc \u8868\u540d;<\/pre>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">show create table \u8868\u540d G;;<\/pre>\n\n\n\n<p>*\u4ee5\u4e0a\u8bed\u53e5\u9664\u4e86\u53ef\u4ee5\u770b\u5230\u8868\u5b9a\u4e49\u4ee5\u5916\uff0c\u8fd8\u53ef\u4ee5\u770b\u5230\u8868\u7684engine\uff08\u5b58\u50a8\u5f15\u64ce\uff09\u548ccharset\uff08\u5b57\u7b26\u96c6\uff09*<\/p>\n\n\n\n<p>\u4e09.\u5220\u9664\u8868<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">drop table \u8868\u540d;<\/pre>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-accent-1-color\">\u56db.\u4fee\u6539\u8868<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">\uff08\u91cd\u8981\uff09<\/mark><\/p>\n\n\n\n<p>1.\u4fee\u6539\u8868\u7c7b\u578b<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">ALTER TABLE \u8868\u540d MODIFY [COLUMN] column_definition [FIRST | AFTER col_name];<\/pre>\n\n\n\n<p>\u4f8b\u5982\uff0c\u4fee\u6539\u8868 stud \u7684 st_name \u5b57\u6bb5\u5b9a\u4e49\uff0c\u5c06 varchar(10)\u6539\u4e3a varchar(20)<\/p>\n\n\n\n<p>mysql> alter table Stud modify st_name varchar(20);<\/p>\n\n\n\n<p>2. \u589e\u52a0\u8868\u5b57\u6bb5<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">ALTER TABLE \u8868\u540d ADD [COLUMN] [FIRST | AFTER col_name];<\/pre>\n\n\n\n<p>\u4f8b\u5982\uff0c\u8868 stud \u4e0a\u65b0\u589e\u52a0\u5b57\u6bb5 st_test\uff0c\u7c7b\u578b\u4e3a int(5)<\/p>\n\n\n\n<p>mysql> alter table stud add column st_test int(5);<\/p>\n\n\n\n<p>3. \u5220\u9664\u8868\u5b57\u6bb5<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">ALTER TABLE \u8868\u540d DROP [COLUMN] col_name<\/pre>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5c06\u5b57\u6bb5 st_test \u5220\u9664\u6389<\/p>\n\n\n\n<p>mysql> alter table Stud drop column st_test;<\/p>\n\n\n\n<p>4. \u5b57\u6bb5\u6539\u540d<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">ALTER TABLE \u8868\u540d CHANGE [COLUMN] old_col_name column_definition [FIRST|AFTER col_name];<\/pre>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5c06 st_sex \u6539\u540d\u4e3a st_sex0\uff0c\u540c\u65f6\u4fee\u6539\u5b57\u6bb5\u7c7b\u578b\u4e3a int(5)<\/p>\n\n\n\n<p>mysql> alter table Stud change st_sex st_sex0 int(5);<\/p>\n\n\n\n<p>*<mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">change \u548c modify \u90fd\u53ef\u4ee5\u4fee\u6539\u8868\u7684\u5b9a\u4e49\uff0c\u4e0d\u540c\u7684\u662f change \u540e\u9762\u9700\u8981\u5199\u4e24\u6b21\u5217\u540d\uff0c\u4e0d\u65b9\u4fbf\u3002\u4f46\u662f change \u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u4fee\u6539\u5217\u540d\u79f0\uff0cmodify \u5219\u4e0d\u80fd<\/mark>*<\/p>\n\n\n\n<p>5. \u4fee\u6539\u5b57\u6bb5\u6392\u5217\u987a\u5e8f<\/p>\n\n\n\n<p>\u5728\uff08ADD\/CNAHGE\/MODIFY\uff09\u4e2d\u540e\u9762\u62ec\u53f7\u91cc\u9762\u7684\u53ef\u9009\u9879first|after column_name,\u8fd9\u4e2a\u9009\u9879\u53ef\u4ee5\u7528\u6765\u4fee\u6539\u5b57\u6bb5\u5728\u8868\u4e2d\u7684\u4f4d\u7f6e<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">alter table \u8868\u540d add \u5217\u540d \u6570\u636e\u7c7b\u578b after \u5217\u540d;<\/pre>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5c06\u65b0\u589e\u7684\u5b57\u6bb5 st_test \u52a0\u5728 st_id \u4e4b\u540e<\/p>\n\n\n\n<p>mysql> alter table Stud add st_test date after st_id;<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">*\u9ed8\u8ba4 ADD \u589e\u52a0\u7684\u65b0\u5b57\u6bb5\u662f\u52a0\u5728\u8868\u7684\u6700\u540e\u4f4d\u7f6e\uff0c\u800c CHANGE\/MODIFY \u9ed8\u8ba4\u90fd\u4e0d\u4f1a\u6539\u53d8\u5b57\u6bb5\u7684\u4f4d\u7f6e*<\/mark><\/p>\n\n\n\n<p>\u4fee\u6539\u5df2\u6709\u5b57\u6bb5 st_name\uff0c\u5c06\u5b83\u653e\u5728\u6700\u524d\u9762<\/p>\n\n\n\n<p>mysql> alter table Stud modify st_name varchar(20) default &#8221; first;<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">\u6ce8\u610f\uff1a<\/mark>CHANGE\/FIRST|AFTER COLUMN \u8fd9\u4e9b\u5173\u952e\u5b57\u90fd\u5c5e\u4e8e MySQL \u5728\u6807\u51c6 SQL \u4e0a\u7684\u6269\u5c55\uff0c\u5728\u5176\u4ed6\u6570\u636e\u5e93\u4e0a\u4e0d\u4e00\u5b9a\u9002\u7528\u3002<\/p>\n\n\n\n<p>6.\u8868\u540d\u4fee\u6539<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">ALTER TABLE \u8868\u540d RENAME [TO] new_tablename;<\/pre>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5c06\u8868 Stud \u6539\u540d\u4e3a stud<\/p>\n\n\n\n<p>mysql> alter table Stud rename stud;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00.\u67e5\u8be2\u6570\u636e\u5e93 SHOW DATABASE();  &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-124","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/posts\/124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/comments?post=124"}],"version-history":[{"count":0,"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"wp:attachment":[{"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}