如何更改MySQL資料庫表的資料庫引擎?


首先,確定MySQL資料庫的型別,即其引擎是InnoDB還是MyISAM。為此,請使用information_schema.columns.tables中的engine列。

語法如下所示。

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

這裡,我有一個名為“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)

更新於:2019年7月30日

瀏覽量:378

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告