如何更改 MySQL 数据库表的数据库引擎?

mysqlmysqli database

首先,确定 MySQL 数据库的类型,即其引擎是 InnoDB 还是 MyISAM。为此,请使用 information_schema.columns.tables 中的引擎列。

语法如下。

SELECT ENGINE FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = ’yourDatabaseName’
AND TABLE_NAME = ’yourTableName’;

Here, I have a table with the name ‘StudentInformations’ −

mysql> create table StudentInformations
   -> (
   -> StudentId int not null auto_increment,
   -> StudentFirstName varchar(20),
   -> StudentLastName varchar(20),
   -> Primary Key(StudentId)
   -> );
Query OK, 0 rows affected (0.57 sec)

现在,您可以使用上述语法的实现来知道表使用的是 InnoDB 还是 MyISAM。我们的数据库是‘test’。

对于相同的查询如下 −

mysql> select engine from information_schema.tables
   -> where table_schema = 'test'
   -> and table_name = 'StudentInformations';

以下是输出 −

+--------+
| ENGINE |
+--------+
| InnoDB |
+--------+
1 row in set (0.05 sec)

使用 alter 命令更改 'StudentInformations' 表的引擎。更改任何表的引擎的语法如下。

ALTER TABLE yourTableName ENGINE = ‘yourEngineName’;

现在让我们将引擎 InnoDB 更改为 MyISAM。查询如下 −

mysql> alter table StudentInformations ENGINE = 'MyISAM';
Query OK, 6 rows affected (1.84 sec)
Records − 6 Duplicates − 0 Warnings − 0

上面显示的结果显示有 6 行受到影响,因为表中有 6 行。

要检查表是否从 InnoDB 转换为 MyISAM,以下是查询 −

mysql> select engine from information_schema.tables
-> where table_schema = 'test'
-> and table_name = 'StudentInformations';

以下是显示引擎已成功更新的输出 −

+--------+
| ENGINE |
+--------+
| MyISAM |
+--------+
1 row in set (0.00 sec)

相关文章