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=COMPRESSED
或KEY_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 TABLE
、INSERT
、UPDATE
或SELECT
。
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 TABLEnew_tbl
SELECT * FROMorig_tbl
LIMIT 0;
Support for 从MySQL 8.0.13开始,不建议使用带有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.REATE TEMPORARY TABLE
的TABLESPACE=innodb_file_per_table
子句和TABLESPACE = innodb_temporary
子句;有望在MySQL的未来版本中删除它。