如何更改 MySQL 表列的数据类型?
你可以使用 modify 命令来进行更改。我们先创建一张表。
mysql> create table DemoTable ( StudentId varchar(200) not null, StudentName varchar(20), StudentAge int, StudentAddress varchar(20), StudentCountryName varchar(20) ); Query OK, 0 rows affected (0.73 sec)
现在检查表说明。
mysql> desc DemoTable;
这将生成以下输出 −
+--------------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------------+--------------+------+-----+---------+-------+ | StudentId | varchar(200) | NO | | NULL | | | StudentName | varchar(20) | YES | | NULL | | | StudentAge | int(11) | YES | | NULL | | | StudentAddress | varchar(20) | YES | | NULL | | | StudentCountryName | varchar(20) | YES | | NULL | | +--------------------+--------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
以下是将数据类型从 varchar 更改为 BIGINT 的查询 −
mysql> alter table DemoTable modify StudentId BIGINT(10) UNSIGNED NOT NULL DEFAULT 0; Query OK, 0 rows affected (1.51 sec) Records: 0 Duplicates : 0 Warnings : 0
上面,我们已将列 ‘StudentId’ 的数据类型从 varchar(200) 更改为 BIGINT(10)。让我们再次检查表说明。
mysql> desc DemoTable;
这将生成以下输出 −
+--------------------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------------+---------------------+------+-----+---------+-------+ | StudentId | bigint(10) unsigned | NO | | 0 | | | StudentName | varchar(20) | YES | | NULL | | | StudentAge | int(11) | YES | | NULL | | | StudentAddress | varchar(20) | YES | | NULL | | | StudentCountryName | varchar(20) | YES | | NULL | | +--------------------+---------------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
广告