Mysql中用SQL增加、删除字段,修改字段名、字段类型、注释,调整字段顺序总结...
生活随笔
收集整理的這篇文章主要介紹了
Mysql中用SQL增加、删除字段,修改字段名、字段类型、注释,调整字段顺序总结...
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
轉(zhuǎn)自:http://www.111cn.net/database/mysql/71648.htm
1.增加一個字段
| ?代碼如下 | 復(fù)制代碼 |
| //增加一個字段,默認為空 alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL; //增加一個字段,默認不能為空 alter table user add COLUMN new2 VARCHAR(20) NOT NULL; | |
2.批量怎加字段
方法一
這里可以使用事務(wù)
| ?代碼如下 | 復(fù)制代碼 |
bagin;?????????????????????????????????????????? //事務(wù)開始 alter table em_day_data add f_day_house7 int(11); alter table em_day_data add f_day_house8 int(11); alter table em_day_data add f_day_house9 int(11); alter table em_day_data add f_day_house10 int(11); commit;???????????????????????????????????????????? //提交事務(wù),事務(wù)結(jié)束 | |
事務(wù)(transaction)是由一系列操作序列構(gòu)成的程序執(zhí)行單元,這些操作要么都做,要么都不做,是一個不可分割的工作單位。
方法二
mysql 批量為表添加多個字段
alter table 表名 add (字段1 類型(長度),字段2 類型(長度),字段3 類型(長度));
| ?代碼如下 | 復(fù)制代碼 |
| alter table em_day_data add (f_day_house11 int(11),f_day_house12 int(11),f_day_house13 int(11)); | |
?
3.刪除一個字段
| ?代碼如下 | 復(fù)制代碼 |
| //刪除一個字段 alter table user DROP COLUMN new2; | |
?
4.修改一個字段
| ?代碼如下 | 復(fù)制代碼 |
| //修改一個字段的類型 alter table user MODIFY new1 VARCHAR(10); //修改一個字段的名稱,此時一定要重新指定該字段的類型 alter table user CHANGE new1 new4 int; | |
5.批量修改字段名稱
| ?代碼如下 | 復(fù)制代碼 |
| alter table 表 change 修改前字段名? 修改后字段名稱 int(11) not null, change 修改前字段名? 修改后字段名稱 int(11) not null, change 修改前字段名? 修改后字段名稱 int(11) not null, change 修改前字段名? 修改后字段名稱 int(11) not null, change 修改前字段名? 修改后字段名稱 int(11) not null | |
例子:
| ?代碼如下 | 復(fù)制代碼 |
alter table em_day_data change f_day_house11 f_day_hour11 int(11) not null, change f_day_house12 f_day_hour12 int(11) not null, change f_day_house13 f_day_hour13 int(11) not null, change f_day_house14 f_day_hour14 int(11) not null, change f_day_house15 f_day_hour15 int(11) not null, change f_day_house16 f_day_hour16 int(11) not null, change f_day_house17 f_day_hour17 int(11) not null | |
6,添加注釋
| ?代碼如下 | 復(fù)制代碼 |
| // 可以為表添加注釋 ALTER TABLE `table_name` COMMENT'注釋'; // 為字段添加注釋,同樣適用于修改 ALTER TABLE `table_name` CHANGE `column_name` `column_name` type(longth) UNSIGNED NULL DEFAULT NULL COMMENT '注釋' | |
7,調(diào)整字段順序:
alter table 表名
change 字段名 新字段名 字段類型 默認值 after 字段名(跳到哪個字段之后)
例子:
?
| ?代碼如下 | 復(fù)制代碼 |
| alter table appstore_souapp_app_androidmarket; change getPriceCurrency getPriceCurrency varchar(50)?? default null AFTER getPrice; | |
轉(zhuǎn)載于:https://www.cnblogs.com/hanlong/p/5711359.html
總結(jié)
以上是生活随笔為你收集整理的Mysql中用SQL增加、删除字段,修改字段名、字段类型、注释,调整字段顺序总结...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信用卡挂失补办后还可以解挂吗?临时挂失期
- 下一篇: 中信魔力银联信用卡好用吗?有哪些权益?