1.增加一個欄位
| 代碼如下 |
複製代碼 |
//增加一個欄位,預設為空白
alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL;
//增加一個欄位,預設不可為空
alter table user add COLUMN new2 VARCHAR(20) NOT NULL; |
2.批量怎加欄位
方法一
這裡可以使用事務
| 代碼如下 |
複製代碼 |
bagin; //事務開始
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; //提交事務,事務結束 |
事務(transaction)是由一系列操作序列構成的程式執行單元,這些操作要麼都做,要麼都不做,是一個不可分割的工作單位。
方法二
mysql 批量為表添加多個欄位
alter table 表名 add (欄位1 類型(長度),欄位2 類型(長度),欄位3 類型(長度));
| 代碼如下 |
複製代碼 |
| alter table em_day_data add (f_day_house11 int(11),f_day_house12 int(11),f_day_house13 int(11)); |
3.刪除一個欄位
| 代碼如下 |
複製代碼 |
//刪除一個欄位
alter table user DROP COLUMN new2; |
4.修改一個欄位
| 代碼如下 |
複製代碼 |
//修改一個欄位的類型
alter table user MODIFY new1 VARCHAR(10);
//修改一個欄位的名稱,此時一定要重新指定該欄位的類型
alter table user CHANGE new1 new4 int; |
5.批量修改欄位名稱
| 代碼如下 |
複製代碼 |
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 |
例子:
| 代碼如下 |
複製代碼 |
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,添加註釋
| 代碼如下 |
複製代碼 |
// 可以為表添加註釋
ALTER TABLE `table_name` COMMENT'注釋';
// 為欄位添加註釋,同樣適用於修改
ALTER TABLE `table_name` CHANGE `column_name` `column_name` type(longth) UNSIGNED NULL DEFAULT NULL COMMENT '注釋'
|
7,調整欄位順序:
alter table 表名
change 欄位名 新欄位名 欄位類型 預設值 after 欄位名(跳到哪個欄位之後)
例子:
| 代碼如下 |
複製代碼 |
alter table appstore_souapp_app_androidmarket;
change getPriceCurrency getPriceCurrency varchar(50) default null AFTER getPrice; |