如何在MySQL中更改表引擎?

您可以借助alter命令更改表引擎。语法如下-

alter table yourTableName ENGINE = yourEngineName;

为了理解上述语法,让我们使用引擎MyISAM创建一个表。以后您可以更改任何其他引擎。以下是创建表的查询。

mysql> create table ChangeEngineTableDemo
−> (
   −> MovieId int,
   −> MovieName varchar(100),
   −> IsPopular bool
−> )ENGINE = 'MyISAM';

查看上面的查询,表引擎是MyISAM,现在您可以将其更改为任何其他引擎。在这里,我们将更改引擎类型InnoDB。更改引擎类型的查询如下-

mysql> alter table ChangeEngineTableDemo ENGINE = InnoDB;
Records: 0 Duplicates: 0 Warnings: 0

要在show命令的帮助下检查引擎类型是否已更改,以下是查询-

mysql> show create table ChangeEngineTableDemo;

以下是输出-

+-----------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table                 | Create Table                                                                                                                                                                                                                        |
+-----------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ChangeEngineTableDemo | CREATE TABLE `changeenginetabledemo` (
`MovieId` int(11) DEFAULT NULL,
`MovieName` varchar(100) DEFAULT NULL,
`IsPopular` tinyint(1) DEFAULT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci                                                                                                            |
+-----------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.03 sec)