mysql : loop over tables and alter table add index

☆樱花仙子☆ 提交于 2019-12-09 06:19:17

问题


I have ~1000 tables that start with the same prefix : table_prefix_{SOME_ID} (i can take the ids from another table)

what is the fast way to loop over all the tables in mysql and do :

   ALTER TABLE `table_prefix_{some_id}` ADD INDEX `fields` (`field`)

回答1:


Forget looping. Just do this:

select concat( 'alter table ', a.table_name, ' add index `fields` (`field`);' )
from information_schema.tables a 
where a.table_name like 'table_prefix_%';

Then take the result set and run it as a SQL script.

BTW, you probably mean create index index_name on table_name( column_name);



来源:https://stackoverflow.com/questions/3565060/mysql-loop-over-tables-and-alter-table-add-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!