mysql資料庫修改欄位及新增字段指令碼

2022-09-15 02:12:09 字數 2355 閱讀 2330

1.修改欄位的長度

alter table 《表名》 modify column 欄位名  資料型別(修改後的長度)

例句:alter table test_table modify column id int(20)

2.修改欄位的名稱

alter table 《表名》 change 《欄位名》 《欄位新名稱》 《欄位的型別》。

例句:alter table test_table change attence_name name  varchar(20)

3.新增字段

新增預設為空的字段

alter table 表名 add column 欄位名 字段型別 default null; 

新增不為空的字段

alter table 表名add column 欄位名 字段型別  not null;

例句:alter table test_table add column attence_name varchar(20) default null; 

alter table test_table add column age varchar(20) not null;

4.刪除字段

alter table 《表名》 drop column 欄位名;

例句:alter table test_table drop column age;

5.批量增加字段

方法一可以使用事務

語法:begin;                                           //事務開始

alter table 表名  add 欄位名  字段型別(長度);

alter table 表名 add 欄位名  字段型別(長度);

alter table 表名 add 欄位名  字段型別(長度);

alter table 表名 add 欄位名  字段型別(長度);

commit;    

例子: 

begin;                                           //事務開始

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;     

方法二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));

6.批量修改欄位名稱

語法: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

7.刪除表

drop table table_name ;

MYSQL 新增和修改資料庫字段屬性

1 新增資料庫字段 例子 新增發貨人qq欄位,允許為空 alter table consignor add column qq varchar 64 null comment 發貨人qq號 新增某個表的字段型別及指定為空或非空 alter table 表名稱 add 欄位名稱 欄位名稱 字段型別 是...

如何在MySQL資料庫新增和修改字段以及運算元據庫

一 資料的操作 1.登入資料庫 mysql u root p 資料庫名稱 2 建立資料庫 命令 create database 資料庫名 charset utf8 3 開啟資料庫 命令 use 資料庫名 4 顯示建立資料庫的語句 命令 show create database 資料庫名 5 刪除資料...

資料庫修改字段

一 更改字段型別長度 a mysql alter table 表名 change 原欄位名 新欄位名 字段型別 b oracle alter table 表名 rename column 原欄位名 to 新欄位名 二 更改字段型別 alter table 表名 alter column 欄位名 更改...