標籤:
ALTER TABLE 語句用於在已有的表中添加、修改或刪除列
1.ADD [COLUMN] column name (column definitions) [FIRST or AFTER column_name]
增加列並且可以指定在某一個列名(column_name)的之前或之後增加這個列
2. ADD INDEX [index_name] (column_list)
增加列的索引
3.ADD PRIMARY KEY (column_list)
增加列的主鍵
4.ADD UNIQUE [index_name] (column_list)
增加列的唯一約束
5.ALTER [COLUMN] column_name {SET DEFAULT default_value or DROP DEFAULT}
設定列的預設約束或者刪除其預設約束
6.CHANGE [COLUMN] old_col_name create_definition
改變一個列的名稱或者,屬性
7.DROP [COLUMN] col_name
刪除一列
8.DROP PRIMARY KEY
刪除列的主鍵屬性
9.DROP INDEX index_name
刪除鍵的索引
10.MODIFY [COLUMN] create_definition RENAME [AS] new_tbl_name
作用和change相同,只是不需要寫兩遍old_colname
注:employee 資料庫需要下載,用法是一樣的
ALTER TABLE employee ADD COLUMN Account_Number INT
ALTER TABLE employee ADD INDEX (ID)
ALTER TABLE employee ADD PRIMARY KEY (ID)
ALTER TABLE employee ADD UNIQUE (ID)
ALTER TABLE employee CHANGE ID salary INT
ALTER TABLE employee DROP Customer_ID
ALTER TABLE employee DROP PRIMARY KEY
ALTER TABLE employee DROP INDEX Customer_ID
ALTER TABLE employee MODIFY First_Name varchar(100)
ALTER TABLE employee RENAME Customer
下面是使用執行個體:
1.alter動作表欄位
(1)增加欄位
alter table 表名 add 欄位名 欄位類型;
alter table student add name varchar(10);
(2)修改欄位
alter table 表名 change 舊欄位名 新欄位名 欄位類型;
alter table student change name name varchar(20)not null default ‘liming‘;//修改欄位類型
alter table student change name name1 varchar(20)not null default ‘liming‘;//修改欄位名
(3)刪除欄位
alter table 表名 drop 欄位名;
alter table student drop name;
2.alter 索引操作
(1)增加索引
alter table 表名 add index 索引名 (欄位名1,欄位名2.....);
alter table student add index stu_name(name);
(2)刪除索引
alter table 表名 drop index 索引名;
alter table student drop index stu_name;
(3)查看某個表的索引
show index from 表名;
(4)增加唯一限制條件的索引
alter table 表名 add unique 索引名(欄位名);
3.主鍵操作
增加主鍵:
alter table 表名 add primary key(欄位名);
- 添加主鍵約束:alter table 表名 add constraint 主鍵 (形如:PK_表名) primary key 表名(主鍵欄位);
- 添加外鍵約束:alter table 從表 add constraint 外鍵(形如:FK_從表_主表) foreign key 從表(外鍵欄位) references 主表(主鍵欄位);
//主鍵549830479
alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);
//增加一個新列549830479
alter table t2 add d timestamp;
alter table infos add ex tinyint not null default ‘0‘;
//刪除列549830479
alter table t2 drop column c;
//重新命名列549830479
alter table t1 change a b integer;
//改變列的類型549830479
alter table t1 change b b bigint not null;
alter table infos change list list tinyint not null default ‘0‘;
//重新命名表549830479
alter table t1 rename t2;
加索引549830479
mysql> alter table tablename change depno depno int(5) not null;
mysql> alter table tablename add index 索引名 (欄位名1[,欄位名2 …]);
mysql> alter table tablename add index emp_name (name);
加主關鍵字的索引549830479
mysql> alter table tablename add primary key(id);
加唯一限制條件的索引549830479
mysql> alter table tablename add unique emp_name2(cardnumber);
刪除某個索引549830479
mysql>alter table tablename drop index emp_name;
修改表:549830479
增加欄位:549830479
mysql> ALTER TABLE table_name ADD field_name field_type;
修改原欄位名稱及類型:549830479
mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;
刪除欄位:549830479
mysql> ALTER TABLE table_name DROP field_name;
刪除主鍵: alter table Employees drop primary key;
MySQL的alter的使用