alter table 語句用於在已有的表中新增、修改或刪除列。
alter_specification:
add [column] create_definition [first | after column_name ]
or add index [index_name] (index_col_name,...)
or add primary key (index_col_name,...)
or add unique [index_name] (index_col_name,...)
or alter [column] col_name
or change [column] old_col_name create_definition
or modify [column] create_definition
or drop [column] col_name
or drop primary key
or drop index index_name
or rename [as] new_tbl_name
or table_options
alter table允許你修改乙個現有表的結構。例如,你可以增加或刪除列、創造或消去索引、改變現有列的型別、或重新命名列或表本身。你也能改變表的注釋和表的型別
你可以在單個alter table語句中發出多個add、alter、drop和change子句。這是mysql對ansi sql92的乙個擴充,sql92在每個alter table語句中只允許乙個子句。
change col_name、drop col_name和drop index是mysql對 ansi sql92 的擴充。
modify是 oracle 對alter table的擴充。
drop index刪除乙個索引。這是mysql對 ansi sql92 的乙個擴充。
以下例子展示了alter table的使用。首先展示表t1。表t1採用如下方法建立:
create table t1 (a integer,b char(10));
alter table t1 rename t2;
把列a從integer更改為tinyint not null (名稱保持不變),並把列b從char(10)更改為char(20), 同時把列b重新命名為列c:
alter table t2 modify a tinyint not null,change b c char(20);
新增乙個新的timestamp列,名稱為d:
alter table t2 add d timestamp
在列d和列a中新增索引:
alter table t2 add index(d),add index(c)
刪除列c:
alter table t2 drop column c
新增乙個新的auto_increment整數列,名稱為c:
alter table t2 add c int unsigned not null auto_increment
->add primary key(c);
當你新增乙個auto_increment列時列值被自動地按序號填入,對於myisam表,您可以在alter table之前執行set insert_id=value來設定第乙個序號,也可以使用auto_increment=value表選項來設定
如果你想要改變列的型別而非名字,就算他們是一樣的,change語法仍然需要2個列名。例如:
alter table table1 change b b bigint not null;
然而,在mysql3.22.16a,你也可以使用modify來改變列的型別而不是重新命名它:
alter table t1 modify b bigint not null;
mysql資料庫在指定位置增加字段
create table t1(age int,address varchar(50));
alter table t add column name varchar(20) after age;
alter table t1 add column id int first;
MySQL中alter的用法
update命令主要對錶資料進行修改 alter命令主要是對錶結構進行修改,主要包括新增 修改 刪除。1 新增 a 新增字段 列 alter table 表名 add 欄位名 字段屬性 例 alter table test add num int 10 not null auto increamen...
MySQL中的ALTER命令
當我們需要修改資料表名或者修改資料表字段時,就需要使用到mysql alter命令。如下命令使用了 alter 命令及 drop 子句來刪除以上建立表的 i 字段 alter table testalter tbl drop i 如果資料表中只剩餘乙個欄位則無法使用drop來刪除字段。mysql 中...
Mysql中alter的用法
1 刪除列 alter table 表名字 drop 列名稱 2 增加列 alter table 表名字 add 列名稱 int not null comment 注釋說明 3 修改列的型別資訊 alter table 表名字 change 列名稱 新列名稱 這裡可以用和原來列同名即可 bigint...