Suryapratap Singh
Suryapratap Singh

Reputation: 11

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?

Upvotes: 0

Views: 959

Answers (1)

Akina
Akina

Reputation: 42622

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;

Upvotes: 1

Related Questions