// tables reference

SQL temporary tables

A table that lives only for your session or transaction. Four engines create ad-hoc temp tables with one statement; Oracle instead pre-creates a table whose data is private to each session.

カテゴリ
テーブル
In this category
2 of 16

構文

CREATE TEMPORARY TABLE table_name (...)  -- PostgreSQL, MySQL, SQLite
CREATE TEMP TABLE table_name (...)  -- PostgreSQL, SQLite
#table_name  -- SQL Server: the # prefix makes it session-local
CREATE GLOBAL TEMPORARY TABLE table_name (...) ON COMMIT {PRESERVE | DELETE} ROWS  -- Oracle

Each block is a separate form. The support table and the examples show which engine takes which.

エンジンサポート

エンジンSupportメモ
SQL Server はい #name is session-local, ##name is global; both live in tempdb
PostgreSQL はい ON COMMIT DROP, DELETE ROWS or PRESERVE ROWS; temp tables live in the pg_temp schema
MySQL はい CREATE TEMPORARY TABLE; dropped when the session ends; no foreign keys
SQLite はい CREATE TEMP TABLE; one temp database per connection
Oracle Different syntax no ad-hoc temp tables: GLOBAL TEMPORARY (pre-created, session-private data) or PRIVATE TEMPORARY (18c+)

例

SQL Server

SELECT customer_id, COUNT(*) AS orders
INTO #big_customers
FROM orders
GROUP BY customer_id
HAVING COUNT(*) > 100;

SELECT * FROM #big_customers; -- gone when the session ends

PostgreSQL

BEGIN;
CREATE TEMP TABLE big_customers ON COMMIT DROP AS
SELECT customer_id FROM orders
GROUP BY customer_id HAVING COUNT(*) > 100;

SELECT * FROM big_customers;
COMMIT; -- big_customers is dropped here

MySQL

CREATE TEMPORARY TABLE tmp_stage (
  id BIGINT PRIMARY KEY,
  email VARCHAR(320)
);

DROP TEMPORARY TABLE tmp_stage; -- or let the session end do it

SQLite

CREATE TEMP TABLE candidates AS
SELECT id, email FROM users WHERE last_login IS NULL;

Oracle

CREATE GLOBAL TEMPORARY TABLE session_scratch (
  id NUMBER PRIMARY KEY
) ON COMMIT DELETE ROWS;

知っておくと便利なこと

  • SQL Server names temp tables with a leading #: #orders is visible only to your session, ##orders to everyone. Two sessions can each create their own #orders; two sessions cannot both own ##orders.
  • PostgreSQL temp tables are dropped at the end of the session. ON COMMIT DROP drops one at the end of the transaction instead, and ON COMMIT DELETE ROWS empties it at every commit while keeping the table. Outside an explicit transaction each statement commits on its own, so an ON COMMIT DROP table is gone before the next statement can read it.
  • Oracle's GLOBAL TEMPORARY TABLE is created once, like a permanent table: the definition is shared and the rows are private per session. The row lifecycle is fixed at creation with ON COMMIT DELETE ROWS or PRESERVE ROWS.
  • MySQL temporary tables do not support foreign keys, and they are visible only to the session that made them, so two connections can use the same name without colliding.

Questions about Temporary tables

How long does a temporary table live?

Until the session ends on SQL Server, PostgreSQL, MySQL and SQLite (a #temp table created inside a SQL Server stored procedure is dropped when the procedure returns), or until the transaction ends if you declared ON COMMIT DROP on PostgreSQL. Oracle's global temporary tables are permanent objects: only their rows are temporary.

Are temp tables the same as table variables on SQL Server?

No. A @table variable is a lightweight declaration scoped to the batch; a #temp table is a real table in tempdb with its own statistics. Tiny row counts suit variables; larger sets usually plan better as #temp.

Can two sessions use the same temp table name?

Yes on SQL Server (#orders is per session), PostgreSQL (temp tables live in a per-session pg_temp schema), MySQL and SQLite. One catch on SQL Server: constraint names on a #temp table are global in tempdb, so if two sessions each create #orders with CONSTRAINT pk_orders, the second fails with error 2714. Leave temp-table constraints unnamed. On PostgreSQL, MySQL and SQLite a temp table also hides a permanent table of the same name from unqualified references.

Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.

// dialect-aware editing

DDL that matches the engine you're connected to

SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.