在 MySQL 中同时更改多个字段类型

发布于 2024-12-12 03:06:46 字数 576 浏览 2 评论 0原文

我有一个表(它是从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦笑流年记忆 2024-12-19 03:06:46

所以...基于 OMG Ponies 的“答案”并假设在 MySQL 中确实没有办法做到这一点...我编写了以下 PHP 脚本,它在不到 1 秒的时间内完成了完整的工作...

$sql = "SHOW COLUMNS FROM `Table_Name`";
$result = @mysql_query($sql, $db);
while($row = mysql_fetch_array($result)){
    $ColumnName = $row[0];
    if($ColumnName!='StateName'){
        $UpdateSql = "ALTER TABLE `Table_Name` CHANGE `$ColumnName` `$ColumnName` DECIMAL( 7, 4 ) NULL DEFAULT NULL ";
        @mysql_query($UpdateSql, $db);
    }
}

希望有一天它会帮助别人。

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...

$sql = "SHOW COLUMNS FROM `Table_Name`";
$result = @mysql_query($sql, $db);
while($row = mysql_fetch_array($result)){
    $ColumnName = $row[0];
    if($ColumnName!='StateName'){
        $UpdateSql = "ALTER TABLE `Table_Name` CHANGE `$ColumnName` `$ColumnName` DECIMAL( 7, 4 ) NULL DEFAULT NULL ";
        @mysql_query($UpdateSql, $db);
    }
}

Hopefully it helps someone else someday.

_畞蕅 2024-12-19 03:06:46

对此的纯 SQL 答案是:

ALTER TABLE [table_name]
MODIFY [col1] float,
MODIFY [col2] varchar(255) null

等等......

The pure SQL answer to this would be:

ALTER TABLE [table_name]
MODIFY [col1] float,
MODIFY [col2] varchar(255) null

and so on...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文