Change engine for all MySQL database tables

9 May 2017

If you want quickly change database engine for all your database table, you can use this short script:

SET @DB_NAME = 'your database name';
SET @FROM_ENGINE = 'current engine name';
SET @TO_ENGINE = 'change to engine name';

SELECT  CONCAT('ALTER TABLE `', table_name, '` ENGINE=', @TO_ENGINE ,';') AS sql_statements
FROM    information_schema.tables AS tb
WHERE   table_schema = @DB_NAME
AND     `ENGINE` = @FROM_ENGINE
AND     `TABLE_TYPE` = 'BASE TABLE'
ORDER BY table_name DESC;

where @DB_NAME, @FROM_ENGINE, @TO_ENGINE are variables. You should set your values to this variables.

For example, you want change engine from MyISAM, to InnoDB in database MegaDB:

SET @DB_NAME = 'MegaDB';
SET @FROM_ENGINE = 'MyISAM';
SET @TO_ENGINE = 'InnoDB';

As a result, you get another list of queries. Example:

ALTER TABLE `User` ENGINE=Aria;
ALTER TABLE `Tag` ENGINE=Aria;
ALTER TABLE `Article_Tag` ENGINE=Aria;
ALTER TABLE `Article` ENGINE=Aria;

And finally, just run these queries too ;)