在更改列类型之前,让MySQL服务器进入严格模式,并确保varchar(n)列具有足够大的n,以便在将所有整数转换为字符串时保存。如果你不是在严格模式,那么MySQL将
silently truncate your data to fit your string size:
If strict SQL mode is not enabled and you assign a value to a CHAR or VARCHAR column that exceeds the column’s maximum length, the value is truncated to fit and a warning is generated. For truncation of nonspace characters, you can cause an error to occur (rather than a warning) and suppress insertion of the value by using strict SQL mode.
mysql> set sql_mode = 'STRICT_ALL_TABLES';
mysql> alter table table_sample change col_sample col_sample varchar(6);
你会得到一个很好的错误消息,像这样:
ERROR 1406 (22001): Data too long for column 'col_sample' at row ...
如果你的整数不是所有适合你的varchar。
当然,在尝试更改表之前,您将对数据库进行新的验证备份。通过验证我的意思是你已经成功地恢复您的备份到测试数据库。