Mysql資料庫alter修改表

2021-10-04 17:33:32 字數 1765 閱讀 1250

如果你想要修改表的資訊,你會發現alter很強大。

我們可以看到這樣一張表。

create

table

`score`

(`student_id`

int(10)

unsigned

notnull

,`event_id`

int(10)

unsigned

notnull

,`score`

int(11)

default

null

,primary

key(

`event_id`

,`student_id`),

key`student_id`

(`student_id`),

constraint

`score_ibfk_1`

foreign

key(

`event_id`

)references

`grade_event`

(`event_id`),

constraint

`score_ibfk_2`

foreign

key(

`student_id`

)references

`student`

(`student_id`))

engine

=innodb

default

charset

=utf8mb4

從上往下看錶的建立語句,alter可以修改表名、列的屬性(列名、資料型別、字符集)、索引的屬性(索引名)、表的儲存引擎、字符集。

舉例:alter table score rename to new_score;

舉例:alter table new_score modify score varchar(3) default null;

注意,此處用的是change,而不是modify。change能做到,而modify做不到,在修改列資料型別的同時,修改列名。

注意此處修改失敗的原因是,表內包含外來鍵。只有innodb引擎支援外來鍵。

資料庫alter用法總結

菜鳥如我,因為需求總是變個不停,所以對表字段的動態的增刪改查是必須要記得 1 刪除列 alter table 表名 drop 列名 2 增加列 alter table 表名 add 列名 型別 alter table table1 add transactor varchar 10 not null...

mysql修改表結構alter

alter ignore table tbl name alter spec alter spec alter specification add column create definition first after column name or add index index name ind...

MySql資料庫之alter表的SQL語句集合

mysql之alter表的sql語句集合,包括增加 修改 刪除字段,重新命名表,新增 刪除主鍵等。1 刪除列 alter table 表名字 drop 列名稱 2 增加列 alter table 表名字 add 列名稱 int not null comment 注釋說明 3 修改列的型別資訊 alt...