mysql修改資料表 alter表操作

2021-09-24 11:46:36 字數 954 閱讀 7181

格式:alter tabel 表名 修改內容;

常用八種操作如下:

修改表名

alter table 舊表名 rename 新錶名;
修改欄位名

alter table 表名 change 舊欄位名 新欄位名 新資料型別;

注意:新資料型別不能為空

修改欄位的資料型別

alter table 表名 modify 欄位名 資料型別;
修改欄位的排列位置

alter table 表名 modify 欄位名1 資料型別 first| (after 欄位名2);
新增字段

alter table 表名 add 新欄位名 資料型別 [約束條件][first | (after 已存在的欄位名)]
刪除字段

alter table 表名 drop 欄位名;
新增約束

alter table 表名 add constraint 約束名 約束型別 (欄位名)

注意: 約束型別就是 primary key 或者 foreign key

刪除約束

alter table 表名 drop primary key

alter table 表名 drop foreign key 約束名

附加說明
alter table 表名 engine=新的儲存引擎型別

alter table 表名 default charset=新的字符集

alter table 表名 auto_increment=新的初始值

alter table 表名 pack_keys=新的壓縮型別

MySQL 修改資料表

修改資料表是指修改資料庫中已有資料表的結構。mysql 使用 alter table 語句修改表。mysql 通過 alter table 語句修改表名,語法規則如下 alter table 舊表名 rename to 新錶名 其中 to 為可選引數,使用與否均不影響結果。修改欄位的資料型別,就是把...

MySQL修改資料表

alter ignore table tb name alter spec,alter spec.alter specification add column create definition first after column name 新增新字段 add index index name i...

MySQL學習 修改資料表

1 取出乙個表的部分內容,形成乙個新錶 原表user 取出其中的 userid,username,userpass三項內容形成新錶user1 2 在原有表的基礎上新增新的列定義 下圖,增加新的gender列和email列演示 alter table user add gender varchar 2...