有没有一种方法可以将Integer字段转换为Varchar而不丢失MySQL中的数据?
您可以使用ALTER命令将Integer转换为Varchar。让我们先创建一个表
mysql> create table DemoTable ( UserId int, UserFirstName varchar(20), UserLastName varchar(20), UserAge int );
现在使用DESC命令检查表的描述:
mysql> desc DemoTable;
这将产生以下输出-
+---------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+-------+ | UserId | int(11) | YES | | NULL | | | UserFirstName | varchar(20) | YES | | NULL | | | UserLastName | varchar(20) | YES | | NULL | | | UserAge | int(11) | YES | | NULL | | +---------------+-------------+------+-----+---------+-------+ 4 rows in set (0.17 sec)
现在,您可以将整数数据类型更改为varchar,而不会丢失数据:
mysql> alter table DemoTable change UserId UserId varchar(20); Records: 0 Duplicates: 0 Warnings: 0
检查表说明:
mysql> desc DemoTable;
这将产生以下输出-
+---------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+-------+ | UserId | varchar(20) | YES | | NULL | | | UserFirstName | varchar(20) | YES | | NULL | | | UserLastName | varchar(20) | YES | | NULL | | | UserAge | int(11) | YES | | NULL | | +---------------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)