How to Create Temporary Tables in MySQL with Examples

Updated

MySQL database server illustration

Create a TEMPORARY table with CREATE TEMPORARY TABLE name (...) — it’s visible only in the session that created it, disappears when that session ends, and can share a name with a permanent table without an error (it shadows it, which is the part most guides skip). Below is real output from MySQL 8.4.10 for all three behaviors.

When a temporary table speeds a query up

Temporary tables help when they let you avoid re-scanning a large source table for multiple downstream queries, not as a blanket speedup, and that trade-off doesn’t reduce to a single millisecond figure without a specific query to measure.

Session-scoped: invisible to a concurrent second connection

-- Session A (kept open):
CREATE TEMPORARY TABLE hw_w18_temp_scope2 (id INT);
INSERT INTO hw_w18_temp_scope2 VALUES (42);
SELECT * FROM hw_w18_temp_scope2;  -- 42, session A sees its own table

-- Session B, a completely separate connection, while A is still open:
SELECT * FROM hw_w18_temp_scope2;
ERROR 1146 (42S02): Table 'hw_w18_test.hw_w18_temp_scope2' doesn't exist

Same schema, same table name, two live connections — session B gets a “doesn’t exist” error, not a permission error, because as far as the server’s shared catalog is concerned it doesn’t exist outside session A.

SHOW TABLES doesn’t list it either, even in the owning session

CREATE TEMPORARY TABLE hw_w18_temp_showtest (id INT);
SHOW TABLES LIKE 'hw_w18_temp_showtest';
Empty set (0.00 sec)

SELECT COUNT(*) FROM information_schema.tables
  WHERE table_schema='hw_w18_test' AND table_name='hw_w18_temp_showtest';
0

Both checks ran in the same session that created the table, immediately after creating it. Neither SHOW TABLES nor information_schema.tables lists a TEMPORARY table — if your tooling enumerates tables through either of those, it will never see one, which matters if you’re scripting cleanup or auditing schema.

The shadowing footgun: same name, different table, no warning

CREATE TABLE hw_w18_shadow_demo (id INT, source VARCHAR(20));
INSERT INTO hw_w18_shadow_demo VALUES (1,'permanent-row');
SELECT * FROM hw_w18_shadow_demo;              -- 1, permanent-row

CREATE TEMPORARY TABLE hw_w18_shadow_demo (id INT, source VARCHAR(20));
INSERT INTO hw_w18_shadow_demo VALUES (2,'TEMP-row-shadowing');
SELECT * FROM hw_w18_shadow_demo;              -- 2, TEMP-row-shadowing (permanent table now unreachable)

DROP TEMPORARY TABLE hw_w18_shadow_demo;
SELECT * FROM hw_w18_shadow_demo;              -- 1, permanent-row (back to normal)

CREATE TEMPORARY TABLE with an existing permanent table’s name succeeds silently and makes every unqualified reference in that session hit the temporary one instead — including INSERT, UPDATE, and DELETE. A script that runs DELETE FROM users inside a block that also created TEMPORARY TABLE users for scratch work is deleting from the temp copy, which can look correct in that session and be a no-op against the real table. Name temporary tables so this can’t happen — a tmp_ or session-specific prefix is a five-second habit that closes the whole class of bug.

Which storage engine a temporary table gets by default

On this server:

CREATE TEMPORARY TABLE hw_w18_temp_defaultengine (id INT);
SHOW CREATE TABLE hw_w18_temp_defaultengine;
-- ENGINE=InnoDB

SHOW VARIABLES LIKE 'default_tmp_storage_engine';   -- InnoDB
SHOW VARIABLES LIKE 'internal_tmp_mem_storage_engine';  -- TempTable

An explicit CREATE TEMPORARY TABLE with no ENGINE= clause gets InnoDB, same as a permanent table — see the InnoDB vs. MyISAM comparison for what that buys you (crash safety, row locking) versus ENGINE=MEMORY, which you still have to request explicitly if you want it. Separately, MySQL’s own *internal*, optimizer-created temporary tables (for GROUP BY, sorts, etc.) default to the TempTable engine, not MEMORY — a change from older MySQL versions that most tutorials online have not caught up to. Every behavior above can be re-run on your own server: create-temporary-tables-examples.sh connects as mysql -u root, checks the cross-connection error, the empty SHOW TABLES result, the shadowing sequence and both default engines, and leaves its database, hw_w18_verify_temp_tables, for you to drop.

On replication: statement-based replication of a session-scoped TEMPORARY TABLE only works if every statement touching it runs on the same connection to the replica, which is why CREATE TEMPORARY TABLE inside stored procedures used for replicated writes is a known source of replica drift; row-based replication (the default since MySQL 5.7) sidesteps this because it never replicates temp-table DDL at all.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.