如何修改现有列的数据类型?

要修改现有列的数据类型,可以使用MODIFY。让我们首先创建一个表-

mysql> create table DemoTable
   (
   ClientId varchar(100),
   ClientName varchar(100),
   ClientAge int,
   ClientProjectDeadline timestamp,
   ClientCountryName varchar(100),
   isMarried boolean,
   ClientNumber bigint
   );

检查表的描述-

mysql> desc DemoTable;

这将产生以下输出-

+-----------------------+--------------+------+-----+---------+-------+
| Field                 | Type         | Null | Key | Default | Extra |
+-----------------------+--------------+------+-----+---------+-------+
| ClientId              | varchar(100) | YES  |     | NULL    |       |
| ClientName            | varchar(100) | YES  |     | NULL    |       |
| ClientAge             | int(11)      | YES  |     | NULL    |       |
| ClientProjectDeadline | timestamp    | YES  |     | NULL    |       |
| ClientCountryName     | varchar(100) | YES  |     | NULL    |       |
| isMarried             | tinyint(1)   | YES  |     | NULL    |       |
| ClientNumber          | bigint(20)   | YES  |     | NULL    |       |
+-----------------------+--------------+------+-----+---------+-------+
7 rows in set (0.34 sec)

现在,将column(ClientNumber)数据类型bigint更改为varchar(20)-

mysql> alter table DemoTable modify ClientNumber varchar(20);
Records: 0 Duplicates: 0 Warnings: 0

让我们再次检查表的描述-

mysql> desc DemoTable;

这将产生以下输出-

+-----------------------+--------------+------+-----+---------+-------+
| Field                 | Type         | Null | Key | Default | Extra |
+-----------------------+--------------+------+-----+---------+-------+
| ClientId              | varchar(100) | YES  |     | NULL    |       |
| ClientName            | varchar(100) | YES  |     | NULL    |       |
| ClientAge             | int(11)      | YES  |     | NULL    |       |
| ClientProjectDeadline | timestamp    | YES  |     | NULL    |       |
| ClientCountryName     | varchar(100) | YES  |     | NULL    |       |
| isMarried             | tinyint(1)   | YES  |     | NULL    |       |
| ClientNumber          | varchar(20)  | YES  |     | NULL    |       |
+-----------------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

查看上面的示例输出,数据类型已从bigint更改为varchar(20)。