① 添加新的一列test_column,並將其做爲主鍵,FIRST將其放在表中第一行,auto_increement是自動增加mysql
alter table test_table add column test_column int not null auto_increment FIRST add primary key(test_column);
可使用SQL語句「alter table ai3 add id0 int auto_increment primary key first;」來添加主鍵列。可使用SQL語句「alter table ai4 modify id int auto_increment primary key;」來修改主鍵列。sql
② 刪除列索引
alter table test_table drop column test_column;
③ 修改某一列的字段長度(例如原本是30字節改成50字節長)rem
alter table test_table modify column test_column varchar(50);
④ 徹底修改某一列(假設本來列名是test1_column,類型是int)innodb
alter table test_table change column test1_column test_column varchar(30);
⑤ 僅僅想重命名某一列(首先須要瞭解這一列的類型,假如本來是int且不爲空,列名是error_name_column)table
alter table test_table change column error_name_column test_column int not null;
① 修改指定表的存儲引擎,假設本來是MYISAMclass
alter table test_table engine=innodb;
② 刪除指定表的主鍵test
alter table test_table drop primary key;
這裏有個狀況須要指出,若是該主鍵列是自動增加(auto_increment)的,由於mysql要求自動增加列必須是索引,因此刪除主鍵也就刪除了主鍵索引,這是不符合mysql要求的,是沒法實現的,會報錯,必須先刪除自動增加(經過修改列屬性),後刪除主鍵im
③ 爲指定表添加主鍵error
alter table test_table add primary key(test_column);
④ 爲指定表添加索引(普通索引),test_index是索引名
alter table test_table add index test_index(test_column);
⑤ 刪除指定表索引
alter table test_table drop index test_index;
⑥ 重命名錶
alter table test_table rename new_name_table;
若是想在一個已經建好的表中添加一列,能夠用諸如:
alter table TABLE_NAME add column NEW_COLUMN_NAME varchar(20) not null;
這條語句會向已有的表中加入新的一列,這一列在表的最後一列位置。若是咱們但願添加在指定的一列,能夠用:
alter table TABLE_NAME add column NEW_COLUMN_NAME varchar(20) not null after COLUMN_NAME;
注意,上面這個命令的意思是說添加新列到某一列後面。若是想添加到第一列的話,能夠用:
alter table TABLE_NAME add column NEW_COLUMN_NAME varchar(20) not null first;