mysql : loop over tables and alter table add index(mysql:遍历表并更改表添加索引)
问题描述
我有 ~1000 个以相同前缀开头的表:table_prefix_{SOME_ID}
(我可以从另一个表中获取 id)
I have ~1000 tables that start with the same prefix :
table_prefix_{SOME_ID}
(i can take the ids from another table)
在 mysql 中循环 所有 表并执行以下操作的快速方法是什么:
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`)
推荐答案
忘记循环.只需这样做:
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_%';
然后获取结果集并将其作为 SQL 脚本运行.
Then take the result set and run it as a SQL script.
顺便说一句,你的意思可能是 create index index_name on table_name(column_name);
BTW, you probably mean create index index_name on table_name( column_name);
这篇关于mysql:遍历表并更改表添加索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:mysql:遍历表并更改表添加索引


基础教程推荐
- 带有WHERE子句的LAG()函数 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01