创建新表时,可以通过在 CREATE TABLE 语句中添加 ENGINE 表选项来指定使用的存储引擎:When you create a new table, you can specify which storage engine
to use by adding an ENGINE table option to the
CREATE TABLE statement:
-- ENGINE=INNODB not needed unless you have set a different -- default storage engine. CREATE TABLE t1 (i INT) ENGINE = INNODB; -- Simple table definitions can be switched from one to another. CREATE TABLE t2 (i INT) ENGINE = CSV; CREATE TABLE t3 (i INT) ENGINE = MEMORY;
当你省略 ENGINE 选项时,将使用默认存储引擎。在 MySQL 8.4 中,默认引擎是 InnoDB。你可以通过使用 --default-storage-engine 服务器启动选项,或者在 my.cnf 配置文件中设置 default-storage-engine 选项来指定默认引擎。When you omit the ENGINE option, the default
storage engine is used. The default engine is
InnoDB in MySQL 8.4. You
can specify the default engine by using the
--default-storage-engine server
startup option, or by setting the
default-storage-engine option in
the my.cnf configuration file.
你可以通过设置 default_storage_engine 变量,为当前会话设置默认存储引擎:You can set the default storage engine for the current session by
setting the
default_storage_engine variable:
SET default_storage_engine=NDBCLUSTER;
使用 TEMPORARY 创建的 CREATE
TEMPORARY TABLE 表的存储引擎,可以通过设置 default_tmp_storage_engine 独立于永久表的引擎进行设置,可在启动时或运行时设置。The storage engine for TEMPORARY tables created
with CREATE
TEMPORARY TABLE can be set separately from the engine
for permanent tables by setting the
default_tmp_storage_engine,
either at startup or at runtime.
要将表从一种存储引擎转换为另一种存储引擎,请使用指明新引擎的 ALTER TABLE 语句:To convert a table from one storage engine to another, use an
ALTER TABLE statement that
indicates the new engine:
ALTER TABLE t ENGINE = InnoDB;
请参阅第 15.1.20 节,“CREATE TABLE 语句”和第 15.1.9 节,“ALTER TABLE 语句”。See Section 15.1.20, “CREATE TABLE Statement”, and Section 15.1.9, “ALTER TABLE Statement”.
如果你尝试使用未编译或已编译但未激活的存储引擎,MySQL 会改为使用默认存储引擎创建表。例如,在复制环境中,可能你的源服务器为了最大安全性使用InnoDB表,但复制服务器为了速度而牺牲持久性或并发性,使用其他存储引擎。If you try to use a storage engine that is not compiled in or that
is compiled in but deactivated, MySQL instead creates a table
using the default storage engine. For example, in a replication
setup, perhaps your source server uses InnoDB
tables for maximum safety, but the replica servers use other
storage engines for speed at the expense of durability or
concurrency.
默认情况下,每当 CREATE TABLE 或 ALTER TABLE 无法使用默认存储引擎时,会生成警告。为了防止在所需引擎不可用时出现混淆或意外行为,请启用 NO_ENGINE_SUBSTITUTION SQL 模式。如果所需引擎不可用,该设置会产生错误而不是警告,并且表不会被创建或修改。参见 第 7.1.11 节,“Server SQL 模式”。By default, a warning is generated whenever
CREATE TABLE or
ALTER TABLE cannot use the default
storage engine. To prevent confusing, unintended behavior if the
desired engine is unavailable, enable the
NO_ENGINE_SUBSTITUTION SQL mode.
If the desired engine is unavailable, this setting produces an
error instead of a warning, and the table is not created or
altered. See Section 7.1.11, “Server SQL Modes”.
MySQL 可能会根据存储引擎将表的索引和数据存储在一个或多个其他文件中。表和列的定义存储在 MySQL 数据字典中。各个存储引擎会为它们管理的表创建所需的任何附加文件。如果表名包含特殊字符,表文件的名称将包含这些字符的编码版本,如第 11.2.4 节,“标识符到文件名的映射”所述。MySQL may store a table's index and data in one or more other files, depending on the storage engine. Table and column definitions are stored in the MySQL data dictionary. Individual storage engines create any additional files required for the tables that they manage. If a table name contains special characters, the names for the table files contain encoded versions of those characters as described in Section 11.2.4, “Mapping of Identifiers to File Names”.