[英]How do I check if table exists before alter table
I need query to update all tables and set ENGINE = INNODB
To many databases.我需要查询来更新所有表并将ENGINE = INNODB
设置为许多数据库。 But some of the databases don't have all the same tables;但是有些数据库没有所有相同的表; some databases have more tables than others.一些数据库比其他数据库有更多的表。
So the problem is that所以问题是
ALTER TABLE `ads` ENGINE = INNODB;
ALTER TABLE `modules` ENGINE = INNODB;
ALTER TABLE `ad_extras` ENGINE = INNODB;
Throws an error when the table modules
doe snot exist.当表modules
存在时抛出错误。 I see that I cannot make a direct IF statement' I tried:我发现我无法直接做出 IF 声明,我试过:
IF EXISTS (SHOW TABLES LIKE 'modules') BEGIN
ALTER TABLE `modules` ENGINE = INNODB;
END IF
But it throws但它抛出
Unrecognized statement type (near IF EXISTS)无法识别的语句类型(IF EXISTS 附近)
Any ideas?有任何想法吗?
If this is just an ad-hoc task,如果这只是一项临时任务,
select concat('ALTER ', TABLE_NAME, " ENGINE = INNODB;")
from information_schema.TABLES
where TABLE_SCHEMA = '<your schema>';
Execute the output again.再次执行输出。
If Exists works only in a stored procedure. If Exists 仅适用于存储过程。
a fixed edition of Jacob's answer:雅各布答案的固定版本:
SET SESSION group_concat_max_len = 1000000;
select group_concat(concat('ALTER Table ', TABLE_NAME, ' ENGINE = INNODB;') SEPARATOR '\n')
from information_schema.TABLES
where TABLE_SCHEMA = 'your_schema_name' and TABLE_TYPE<>'VIEW';
This will give you a script to copy and run separately.这将为您提供一个单独复制和运行的脚本。
像这个查询一样简单地测试怎么样:
DROP TABLE IF EXISTS `yourtablename`;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.