Creating a Temporary Table with CREATE TEMPORARY TABLE

Every table I’ve created so far has been a permanent table, called a base table, which stores data persistently until you destroy (DROP) the table explicitly. SQL also lets you create temporary tables to use for working storage or intermediate results. Temporary tables commonly are used to:

  • Store the result of a complex, time-consuming query once and use the result repeatedly in subsequent queries, improving performance greatly.

  • Create an image, or snapshot, of a table at a particular moment in time. (You can add a column with the DEFAULT value CURRENT_TIMESTAMP for recording the exact time.)

  • Hold the result of a subquery; see the DBMS Tip in “Creating Outer Joins with OUTER JOIN” in Chapter ...

Get SQL: Visual QuickStart Guide now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.