如何转换MySQL字段类型? [英] How to convert MySQL field types?

查看:431
本文介绍了如何转换MySQL字段类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遇到了转换功能.据我了解,基本语法是:

I have already come across the convert function. As I understand it, the basic syntax is:

select convert(columnName, targetFieldType) as newColumnName from table;

运行此命令不会给我任何错误,但是当我检查数据类型时,它们没有改变.即使我使用commit;数据保持不变.特别是,我试图将长类型的数据转换为varchar.有什么想法吗?

Running this command doesn't give me any errors, but when I check the data types they are unchanged. Even when I use commit; the data remains unchanged. In particular, I'm trying to convert data with long type to varchar. Any ideas?

推荐答案

给定的SELECT查询将返回新类型的值,但不会更改数据库中的表格.

The given SELECT query will return the value in the new type, but it doesn't change the field type of the table in your database.

如果要永久更改磁盘上的表,请使用:

If you want to permanently change the table on disk, use:

ALTER TABLE table CHANGE columnName newColumnName targetFieldType NOT NULL;

请注意,对于大型表,这可能需要一段时间,因为MySQL会重写整个表.

Note that for large tables, this can take a while, because MySQL rewrites the entire table.

注意:如果您还想在此列中允许使用NULL值,请删除NOT NULL限定符.

Note: remove the NOT NULL qualifier if you also want to allow NULL values in this column.

有关更多信息,请参见MySQL参考中的 ALTER TABLE语法手动.

For more information, see ALTER TABLE Syntax in MySQL Reference Manual.

这篇关于如何转换MySQL字段类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆