向现有 MySQL 表中添加新枚举列?
若要向现有 MySQL 表中添加新枚举列,可以使用 ALTER 命令。如下所示
ALTER TABLE yourTableName ADD yourColumnName ENUM('yourValue1','yourValue2’....N) NOT NULL;
我们先创建一个表
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(200), StudentAge int ); Query OK, 0 rows affected (0.62 sec)
使用 DESC 命令检查表的描述
mysql> DESC DemoTable;
这将产生以下输出
+-------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+----------------+ | StudentId | int(11) | NO | PRI | NULL | auto_increment | | StudentName | varchar(200) | YES | | NULL | | | StudentAge | int(11) | YES | | NULL | | +-------------+--------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec)
以下为在现有表中添加新枚举列的查询。我们将其设置为学生性别
mysql> ALTER TABLE DemoTable ADD StudentGender ENUM('Male','Female') NOT NULL; Query OK, 0 rows affected (0.40 sec) Records: 0 Duplicates: 0 Warnings: 0
让我们再次检查表的描述
mysql> desc DemoTable;
这将产生以下输出,并为 GENDER 显示枚举值
+---------------+-----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+-----------------------+------+-----+---------+----------------+ | StudentId | int(11) | NO | PRI | NULL | auto_increment | | StudentName | varchar(200) | YES | | NULL | | | StudentAge | int(11) | YES | | NULL | | | StudentGender | enum('Male','Female') | NO | | NULL | | +---------------+-----------------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
查看上述示例输出,列 StudentGender 的数据类型为 ENUM。
广告