ALTER TABLE tbl_name ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name]
e.g. 现在假设数据库内有一张表 person,person 的字段信息如下
1 2 3 4 5 6 7 8
mysql> SHOW COLUMNS FROM person; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 2 rows in set (0.33 sec)
现在需要在此基础上添加一个字段 age ,类型为 TINYINT(无符号), 不为空且默认值为 10,操作如下
1 2 3
mysql> ALTER TABLE person ADD COLUMN age TINYINT UNSIGNED NOT NULL DEFAULT 10; Query OK, 0 rows affected (0.52 sec) Records: 0 Duplicates: 0 Warnings: 0`
现在再次查看 person 表的字段信息
1 2 3 4 5 6 7 8 9 10
mysql> SHOW COLUMNS FROM person; +-------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | age | tinyint(3) unsigned | NO | | 10 | | +-------+---------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
字段 age 已经添加成功了
在上面给出的增加字段的代码中,我们省略了 FIRST 或者 AFTER col_name,这两个意思分别是:
FIRST : 将新添加的字段放在所有字段的第一位
AFTER col_name : 将新增加的字段放在指定字段的后面
如果省略这个描述,那么新增加的列就会默认添加在最后。接下来再添加一个字段 gender(性别)在字段 name 之后
1 2 3 4
mysql> ALTER TABLE person ADD COLUMN gender ENUM('female', 'male', 'unknow') DEFAULT 'unknow' AFTER name; Query OK, 0 rows affected (0.53 sec) Records: 0 Duplicates: 0 Warnings: 0
再次查看 person 数据表的字段信息
1 2 3 4 5 6 7 8 9 10 11
mysql> SHOW COLUMNS FROM person; +--------+--------------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+--------------------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | gender | enum('female','male','unknow') | YES | | unknow | | | age | tinyint(3) unsigned | NO | | 10 | | +--------+--------------------------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
添加多列
1
AFTER TABLE tbl_name ADD [COLUMN] (col_name column_definition, ...)
mysql> ALTER TABLE person DROP COLUMN first_name, DROP COLUMN last_name; Query OK, 0 rows affected (0.50 sec) Records: 0 Duplicates: 0 Warnings: 0
再次查看数据表结构
1 2 3 4 5 6 7 8 9 10
mysql> SHOW COLUMNS FROM person; +-------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | age | tinyint(3) unsigned | NO | | 10 | | +-------+---------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
现在 person 表中就只剩下 3 列了
当然如果你想同时删除一行,然后添加一行的话也是可以的,这之间用逗号分隔即可
比如现在想删除 age 字段,添加 gender 字段
1 2 3 4
mysql> ALTER TABLE person DROP age, ADD gender ENUM('female', 'male', 'unknow') DEFAULT 'unknow'; Query OK, 0 rows affected (0.56 sec) Records: 0 Duplicates: 0 Warnings: 0
那么现在 person 数据表的结构就变成了下面这个样子:
1 2 3 4 5 6 7 8 9 10
mysql> SHOW COLUMNS FROM person; +--------+--------------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+--------------------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | gender | enum('female','male','unknow') | YES | | unknow | | +--------+--------------------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
age 字段已经被删除,取而代之的是一个 gender 字段
约束的添加
现在创建一张表 user,内含字段 id,name。用于这一小节的测试:
1 2 3 4 5 6
mysql> CREATE TABLE user( -> id SMALLINT UNSIGNED, -> name VARCHAR(20) NOT NULL -> ); Query OK, 0 rows affected (0.28 sec)
这里的 CONSTRAINT PK_user_id 是可以省略不写的,其中 PK_user_id ,即 symbol ,这个相当于这个约束的名字。此时查看数据表的结构
1 2 3 4 5 6 7 8 9
mysql> SHOW COLUMNS FROM user; +-------+----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------------------+------+-----+---------+-------+ | id | smallint(5) unsigned | NO | PRI | NULL | | | name | varchar(20) | NO | | NULL | | +-------+----------------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
mysql> ALTER TABLE user ADD UNIQUE (name); Query OK, 0 rows affected (0.24 sec) Records: 0 Duplicates: 0 Warnings: 0
这里为了简便,将一些可以省略的都省略了,接下来再次查看一下数据表的结构
1 2 3 4 5 6 7 8 9
mysql> SHOW COLUMNS FROM user; +-------+----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------------------+------+-----+---------+-------+ | id | smallint(5) unsigned | NO | PRI | NULL | | | name | varchar(20) | NO | UNI | NULL | | +-------+----------------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
ALTER TABLE tbl_name ALTER [COLUMN] col_name SET DEFAULT literal;
最后我们还可以给数据表中的字段添加默认约束,比如现在再为 user 表添加一个 age 字段,再添加完之后,再为 age 字段添加默认约束
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
mysql> ALTER TABLE user ADD age TINYINT UNSIGNED NOT NULL; Query OK, 0 rows affected (0.92 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW COLUMNS FROM user; +-------+----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------------------+------+-----+---------+-------+ | id | smallint(5) unsigned | NO | PRI | NULL | | | name | varchar(20) | NO | UNI | NULL | | | uid | int(10) unsigned | YES | MUL | NULL | | | age | tinyint(3) unsigned | NO | | NULL | | +-------+----------------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
先为 user 添加一个新的字段,但是并未指定默认约束,所以接下来就为 age 字段添加默认约束
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
mysql> ALTER TABLE user ALTER age SET DEFAULT 0; Query OK, 0 rows affected (0.36 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW COLUMNS FROM user; +-------+----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------------------+------+-----+---------+-------+ | id | smallint(5) unsigned | NO | PRI | NULL | | | name | varchar(20) | NO | UNI | NULL | | | uid | int(10) unsigned | YES | MUL | NULL | | | age | tinyint(3) unsigned | NO | | 0 | | +-------+----------------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
mysql> ALTER TABLE user DROP INDEX uid; Query OK, 0 rows affected (0.44 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW CREATE TABLE user; +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | user | CREATE TABLE `user` ( `id` smallint(5) unsigned NOT NULL, `name` varchar(20) NOT NULL, `uid` int(10) unsigned DEFAULT NULL, `age` tinyint(3) unsigned NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
此时索引也已经删除
删除默认约束
1
ALTER TABLE tbl_name ALTER [COLUMN] col_name DROP DEFAULT;
这里删除默认约束就比较简单了,只需要知道列的名字就可以了。最后再来删除掉 age 字段的默认约束
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
mysql> ALTER TABLE user ALTER age DROP DEFAULT; Query OK, 0 rows affected (0.07 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW COLUMNS FROM user; +-------+----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------------------+------+-----+---------+-------+ | id | smallint(5) unsigned | NO | PRI | NULL | | | name | varchar(20) | NO | UNI | NULL | | | uid | int(10) unsigned | YES | MUL | NULL | | | age | tinyint(3) unsigned | NO | | NULL | | +-------+----------------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
此时,age 字段就没有默认值了。
修改数据表
修改列定义
1
ALTER TABLE tbl_name MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name];
有时候在定义完数据列之后,发现在定义上出现了问题,那么就可以通过修改列定义去更正
比如现在我们通过查看 user 数据表
1 2 3 4 5 6 7 8 9 10 11
mysql> SHOW COLUMNS FROM user; +-------+----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------------------+------+-----+---------+-------+ | id | smallint(5) unsigned | NO | | NULL | | | name | varchar(20) | NO | | NULL | | | uid | int(10) unsigned | YES | | NULL | | | age | tinyint(3) unsigned | NO | | NULL | | +-------+----------------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
我们发现 age 这个字段没有默认值,并且现在我想让该字段位于 name 这个字段之后,那么我们就可以进行如下操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
mysql> ALTER TABLE user MODIFY age TINYINT UNSIGNED DEFAULT 10 AFTER name; Query OK, 0 rows affected (0.45 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW COLUMNS FROM user; +-------+----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------------------+------+-----+---------+-------+ | id | smallint(5) unsigned | NO | | NULL | | | name | varchar(20) | NO | | NULL | | | age | tinyint(3) unsigned | YES | | 10 | | | uid | int(10) unsigned | YES | | NULL | | +-------+----------------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
如果不想修改 age 字段的列定义,只是想移动 age 字段的位置的话,那么就需要把 age 字段原来的定义重新写一遍即可。