简体   繁体   中英

MySQL - How to alter column datatype in all databases?

On my remote server, I need to update a column in a table in all databases. How can I do this all at once? I have over than hundred databases with the same table.

ALTER TABLE tablename MODIFY columnname VARCHAR(255);

You can write a SQL query to emit a script that does this. The query looks at the information_schema table describing columns in your server, and generates a sequence of ALTER queries. You then run those queries.

SELECT CONCAT(' ALTER TABLE `', TABLE_SCHEMA, '`.`', TABLE_NAME, 
              '` MODIFY `',COLUMN_NAME,'` VARCHAR(255);') ddl 
  FROM information_schema.`COLUMNS` 
 WHERE TABLE_NAME = 'tablename' 
   AND COLUMN_NAME IN ('columnname')

There isn't any oneliner to do ALTER TABLE *.tablename or anything like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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