简体   繁体   中英

How to change all tables engines from MYISAM to INNODB in multiple databses?

I know I can issue an alter table individually to change the table storage from MyISAM to InnoDB.

I am wondering if there is a way to quickly change all of them to InnoDB?

This is one-time task.

Use

SELECT CONCAT(TABLE_SCHEMA, '.', TABLE_NAME) tablename 
FROM INFORMATION_SCHEMA.TABLES
WHERE ENGINE = 'MyISAM'
  AND TABLE_SCHEMA IN (databases names list);

Get the tables list, build ALTERing script, and execute it manually.


This operation in stored procedure form. NOT TESTED!!!

CREATE PROCEDURE alter_engines ()
BEGIN

DECLARE done INT DEFAULT FALSE;
DECLARE cur CURSOR FOR
    SELECT CONCAT('ALTER TABLE ' , TABLE_SCHEMA, '.', TABLE_NAME, ' ENGINE = InnoDB;') 
    FROM INFORMATION_SCHEMA.TABLES
    WHERE ENGINE = 'MyISAM'
                          /*   do not forget   */
      AND TABLE_SCHEMA IN (databases names list);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN cur;

alter_loop: LOOP
    FETCH cur INTO @sql;
    IF done THEN
      LEAVE alter_loop;
    END IF;
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END LOOP;

CLOSE cur;

END;

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