简体   繁体   English

MySQL PHP alter table,将所有MEDIUMINT更改为INT

[英]MySQL PHP alter table, change all MEDIUMINT to INT

In the past I've used a PHP script to change the language of all fields, but I can't find anything for switching all UNSIGNED MEDIUMINT to UNSIGNED INT. 在过去,我使用PHP脚本来更改所有字段的语言,但是找不到任何可以将所有UNSIGNED MEDIUMINT切换为UNSIGNED INT的内容。 Is this possible? 这可能吗? Should I manually switch them all? 我应该手动切换它们吗? :o :○

Turns out mediumint is too small... so I have to change a lot of fields /: 事实证明,mediumint太小了...所以我必须改变很多字段/:

edit: thanks for the input, I might just do it manually now that I know there's no simple automated way of doing it. 编辑:感谢输入,我可能现在手动完成,因为我知道没有简单的自动化方法。 I just didn't want to waste my time changing 100+ fields manually if there was an automated way. 如果有自动方式,我只是不想浪费时间手动更改100多个字段。

MySQL does not have an automated way to say "take all of the columns of type X and change them to type Y." MySQL没有一种自动的方式来说“获取所有X类型的列并将其更改为类型Y”。 You may be able to query the information_schema database to figure out which columns you need to alter, and then you can build the ALTER TABLE query based on that information. 您可以查询information_schema数据库以确定需要更改的列,然后可以基于该信息构建ALTER TABLE查询。

Specifically, have a look at the information_schema.columns table. 具体来说,看一下information_schema.columns表。

You can use the below query to find all mediumint columns (with respective table names) in a specific database and then iterate over the results to generate and execute ALTER queries in your choice of programming language. 您可以使用以下查询在特定数据库中查找所有mediumint列(具有相应的表名),然后迭代结果以生成并执行您选择的编程语言的ALTER查询。

SELECT 
    table_name, column_name, numeric_precision, column_type
FROM
    information_schema.columns
WHERE
    TABLE_SCHEMA = 'DATABASE_NAME'
        AND data_type = 'mediumint';

Has to be done in SQL, so if it makes sense in your case to write a script to do it, go for it, but most likely you will want to do it manually. 必须在SQL中完成,所以如果在你的情况下写一个脚本来做它是有意义的,那就去做吧,但很可能你会想要手动完成它。

Since you are going from a data type that is less descriptive to a data type that is more descriptive, you should be fine. 由于您从描述性较低的数据类型转向更具描述性的数据类型,因此您应该没问题。

Usint the ALTER TABLE Syntax should work: 使用ALTER TABLE语法应该工作:

ALTER TABLE table_name MODIFY column_name INT UNSIGNED

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM