Documentation Home
MySQL 9.3 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 40.8Mb
PDF (A4) - 40.9Mb
Man Pages (TGZ) - 261.1Kb
Man Pages (Zip) - 368.3Kb
Info (Gzip) - 4.1Mb
Info (Zip) - 4.1Mb


MySQL 9.3 Reference Manual  / ...  / SQL Statements  / Data Definition Statements  / CREATE TABLE Statement  /  CREATE TEMPORARY TABLE Statement

15.1.22.2 CREATE TEMPORARY TABLE Statement

You can use theTEMPORARY keyword when creating a table. ATEMPORARY 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. Wheninnodb_strict_mode is enabled (the default),CREATE TEMPORARY TABLE returns an error ifROW_FORMAT=COMPRESSED orKEY_BLOCK_SIZE is specified. Ifinnodb_strict_mode is disabled, warnings are issued and the temporary table is created using a non-compressed row format. Theinnodb_file_per-table option does not affect the creation ofInnoDB temporary tables.

CREATE TABLE causes an implicit commit, except when used with theTEMPORARY keyword. SeeSection 15.3.3, “Statements That Cause an Implicit Commit”.

TEMPORARY tables have a very loose relationship with databases (schemas). Dropping a database does not automatically drop anyTEMPORARY tables created within that database.

To create a temporary table, you must have theCREATE TEMPORARY TABLES privilege. 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 asDROP TABLE,INSERT,UPDATE, orSELECT.

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 theCREATE TEMPORARY TABLES privilege but is able to execute a definer-context stored procedure that executes with the privileges of a user who does haveCREATE TEMPORARY TABLES and that creates a temporary table. 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 useCREATE TEMPORARY TABLE ... LIKE to create an empty table based on the definition of a table that resides in themysql tablespace,InnoDB system tablespace (innodb_system), or a general tablespace. The tablespace definition for such a table includes aTABLESPACE attribute that defines the tablespace where the table resides, and the aforementioned tablespaces do not support temporary tables. 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;
Note

Support forTABLESPACE = innodb_file_per_table andTABLESPACE = innodb_temporary clauses withCREATE TEMPORARY TABLE is deprecated; expect it to be removed in a future version of MySQL.