13.1.20.2 CREATE TEMPORARY TABLE Statement语句

You can use the TEMPORARY keyword when creating a table. 创建表时可以使用TEMPORARY关键字。A TEMPORARY table is visible only within the current session, and is dropped automatically when the session is closed. 临时表仅在当前会话中可见,并在会话关闭时自动删除。This means that two different sessions can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. 这意味着两个不同的会话可以使用相同的临时表名,而不会相互冲突,也不会与相同名称的现有非临时表冲突。(The existing table is hidden until the temporary table is dropped.)(在删除临时表之前,将隐藏现有表。)

InnoDB does not support compressed temporary tables. InnoDB不支持压缩的临时表。When innodb_strict_mode is enabled (the default), CREATE TEMPORARY TABLE returns an error if ROW_FORMAT=COMPRESSED or KEY_BLOCK_SIZE is specified. 启用innodb_strict_mode(默认)时,如果指定了ROW_FORMAT=COMPRESSEDKEY_BLOCK_SIZE,则创建临时表将返回错误。If innodb_strict_mode is disabled, warnings are issued and the temporary table is created using a non-compressed row format. 如果禁用innodb_strict_mode,将发出警告,并使用非压缩行格式创建临时表。The innodb_file_per-table option does not affect the creation of InnoDB temporary tables.innodb_file_per-table选项不影响InnoDB临时表的创建。

CREATE TABLE causes an implicit commit, except when used with the TEMPORARY keyword. CREATE TABLE导致隐式提交,除非与TEMPORARY关键字一起使用。See Section 13.3.3, “Statements That Cause an Implicit Commit”.请参阅第13.3.3节,“导致隐式提交的语句”

TEMPORARY tables have a very loose relationship with databases (schemas). TEMPORARY表与数据库(模式)的关系非常松散。Dropping a database does not automatically drop any TEMPORARY tables created within that database.删除数据库不会自动删除在该数据库中创建的任何TEMPORARY表。

To create a temporary table, you must have the CREATE TEMPORARY TABLES privilege. 要创建临时表,必须具有CREATE TEMPORARY TABLES权限。After a session has created a temporary table, the server performs no further privilege checks on the table. 会话创建临时表后,服务器不再对该表执行进一步的权限检查。The creating session can perform any operation on the table, such as DROP TABLE, INSERT, UPDATE, or SELECT.创建会话可以对表执行任何操作,例如DROP TABLEINSERTUPDATESELECT

One implication of this behavior is that a session can manipulate its temporary tables even if the current user has no privilege to create them. 这种行为的一个含义是会话可以操作其临时表,即使当前用户没有创建临时表的权限。Suppose that the current user does not have the CREATE TEMPORARY TABLES privilege but is able to execute a definer-context stored procedure that executes with the privileges of a user who does have CREATE TEMPORARY TABLES and that creates a temporary table. 假设当前用户没有CREATE TEMPORARY TABLES权限,但能够执行定义器上下文存储过程,该存储过程以具有CREATE TEMPORARY TABLES权限并创建临时表的用户的权限执行。While the procedure executes, the session uses the privileges of the defining user. 过程执行时,会话使用定义用户的权限。After the procedure returns, the effective privileges revert to those of the current user, which can still see the temporary table and perform any operation on it.过程返回后,有效权限将恢复为当前用户的权限,当前用户仍可以查看临时表并对其执行任何操作。

You cannot use CREATE TEMPORARY TABLE ... LIKE to create an empty table based on the definition of a table that resides in the mysql tablespace, InnoDB system tablespace (innodb_system), or a general tablespace. 无法使用CREATE TEMPORARY TABLE ... LIKE根据mysql表空间、InnoDB系统表空间(InnoDB_system)或常规表空间中的表定义创建空表。The tablespace definition for such a table includes a TABLESPACE attribute that defines the tablespace where the table resides, and the aforementioned tablespaces do not support temporary tables. 此类表的表空间定义包括一个TABLESPACE属性,该属性定义了表所在的表空间,并且前面提到的表空间不支持临时表。To create a temporary table based on the definition of such a table, use this syntax instead:要根据此类表的定义创建临时表,请改用以下语法:

CREATE TEMPORARY TABLE new_tbl SELECT * FROM orig_tbl LIMIT 0;
Note注意

Support for TABLESPACE = innodb_file_per_table and TABLESPACE = innodb_temporary clauses with CREATE TEMPORARY TABLE is deprecated as of MySQL 8.0.13; expect it be removed in a future version of MySQL.从MySQL 8.0.13开始,不建议使用带有REATE TEMPORARY TABLETABLESPACE=innodb_file_per_table子句和TABLESPACE = innodb_temporary子句;有望在MySQL的未来版本中删除它。