這篇文章主要介紹MySQL修改表的方法,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)建站專注于郊區(qū)企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站建設(shè)。郊區(qū)網(wǎng)站建設(shè)公司,為郊區(qū)等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站開發(fā),專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)
在 MySQL 中可以使用 ALTER TABLE
語句來改變原有表的結(jié)構(gòu),例如增加或刪減列、創(chuàng)建或取消索引、更改原有列類型、重新命名列或表等。
常用的語法格式如下:
ALTER TABLE <表名> [修改選項]
修改選項的語法格式如下:
{ ADD COLUMN <列名> <類型> | CHANGE COLUMN <舊列名> <新列名> <新列類型> | ALTER COLUMN <列名> { SET DEFAULT <默認(rèn)值> | DROP DEFAULT } | MODIFY COLUMN <列名> <類型> | DROP COLUMN <列名> | RENAME TO <新表名> }
示例:
一、修改表信息
1、修改表名
alter table test_a rename to sys_app;
2、修改表注釋
alter table sys_application comment '系統(tǒng)信息表';
二、修改字段信息
1、修改字段類型和注釋
alter table sys_application modify column app_name varchar(20) COMMENT '應(yīng)用的名稱';
2、修改字段類型
alter table sys_application modify column app_name text;
3、單獨修改字段注釋
目前沒發(fā)現(xiàn)有單獨修改字段注釋的命令語句。
4、設(shè)置字段允許為空
alter table sys_application modify column description varchar(255) null COMMENT '應(yīng)用描述';
5、增加一個字段,設(shè)好數(shù)據(jù)類型,且不為空,添加注釋
alter table sys_application add `url` varchar(255) not null comment '應(yīng)用訪問地址';
6、增加主鍵
alter table t_app add aid int(5) not null ,add primary key (aid);
7、增加自增主鍵
alter table t_app add aid int(5) not null auto_increment ,add primary key (aid);
8、修改為自增主鍵
alter table t_app modify column aid int(5) auto_increment ;
9、修改字段名字(要重新指定該字段的類型)
alter table t_app change name app_name varchar(20) not null;
10、刪除字段
alter table t_app drop aid;
11、在某個字段后增加字段
alter table `t_app` add column gateway_id int not null default 0 AFTER `aid`; #(在哪個字段后面添加)
12、調(diào)整字段順序
alter table t_app change gateway_id gateway_id int not null after aid ; #(注意gateway_id出現(xiàn)了2次)
以上是mysql修改表的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文標(biāo)題:mysql修改表的方法
當(dāng)前地址:http://aaarwkj.com/article14/jjggge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、品牌網(wǎng)站制作、網(wǎng)站維護、品牌網(wǎng)站建設(shè)、用戶體驗、虛擬主機
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)