在 MySQL 中同时更改多个字段类型
我有一个表(它是从 Excel 导入的),其中有多种自动识别/分配的类型(int、float、decimal 等)。大约有 160 列......我想将它们标准化。我知道要更改字段类型,我使用 ALTER TABLE 和 CHANGE 命令...但是您必须指定列名称...而且我真的不想为每一列单独执行此操作。我尝试过这样的事情:
ALTER TABLE `Table_Name` CHANGE * * DECIMAL( 7, 4 ) NULL DEFAULT NULL
但这没有用。让事情变得稍微复杂一点...实际上有一个字段是主键 StateName
,它不是数字(值类似于 TX、VA、FL、NY)...我该怎么做不需要管它。
简而言之,我正在寻找类似的东西:
ALTER TABLE `Table_Name` CHANGE * * DECIMAL( 7, 4 ) NULL DEFAULT NULL EXCEPT WHERE Column='StateName'
我刚刚编写了该语法......我知道它还很遥远。
这可以做到吗?
I have a table (it was imported from Excel) that has a wide variety of automatically identified/assigned types (int, float, decimal, etc). There are about 160 columns...and i want to standardize them. I know that to change the field type i use ALTER TABLE and the CHANGE command...but you have to specify the column name...and i really don't want to have to do this individually for every column. I tried something like this:
ALTER TABLE `Table_Name` CHANGE * * DECIMAL( 7, 4 ) NULL DEFAULT NULL
but that did not work. To make matters slightly more complicated...there is actually one field which is the primary key StateName
that is not numeric (the values are like TX,VA,FL,NY)...and do i need to leave it alone.
In short, i am looking for something like:
ALTER TABLE `Table_Name` CHANGE * * DECIMAL( 7, 4 ) NULL DEFAULT NULL EXCEPT WHERE Column='StateName'
I just made up that syntax...i know it is nowhere close.
Can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以...基于 OMG Ponies 的“答案”并假设在 MySQL 中确实没有办法做到这一点...我编写了以下 PHP 脚本,它在不到 1 秒的时间内完成了完整的工作...
希望有一天它会帮助别人。
So...based on the "answer" from OMG Ponies and assuming that there really is not a way to do this within MySQL...i wrote the following PHP script which accomplished the complete job in less than 1 second...
Hopefully it helps someone else someday.
对此的纯 SQL 答案是:
等等......
The pure SQL answer to this would be:
and so on...