MYSQL ALTER TABLE命令用于修改表结构,例如添加/修改/删除字段、索引、主键等等,本文章通过实例向大家介绍MYSQL ALTER TABLE语句的使用方法,需要的朋友可以参考一下。
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] (index_col_name,...) or ADD PRIMARY KEY (index_col_name,...) or ADD UNIQUE [index_name] (index_col_name,...) or ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT} 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
下面来看几个实例:
1、向表employee中添加Account_Number字段并设置其字段类型为INT
ALTER TABLE employee ADD COLUMN Account_Number INT
2、修改表employee中的ID字段为索引
ALTER TABLE employee ADD INDEX (ID)
3、修改表employee中的ID字段为主键PRIMARY KEY
ALTER TABLE employee ADD PRIMARY KEY (ID)
4、修改表employee中的ID字段为唯一索引UNIQUE
ALTER TABLE employee ADD UNIQUE (ID)