Explain Codes LogoExplain Codes Logo

Best way to create a temp table with same columns and type as a permanent table

sql
temp-tables
sql-server
postgresql
Nikita BarsukovbyNikita Barsukov·Dec 2, 2024
TLDR

Quickly mirror the structure of a permanent table into a temporary one using the "SELECT INTO" method. Specifically, run SELECT TOP 0 * INTO #TempTable FROM OriginalTable;, to make an empty #TempTable identical to the OriginalTable's structure.

/* Let's get this party started! */ SELECT TOP 0 * INTO #TempTable FROM OriginalTable;

This speedy technique is perfect for replicating a schema sans the data.

How to streamline the process?

Raising our productivity bar and leveraging SQL server capabilities, the SELECT INTO command can save us substantial time. PostgreSQL also offers a similar handy code:

/* Hey there PostgreSQL, you're not left behind! */ CREATE TEMP TABLE temp_table AS SELECT * FROM original_table LIMIT 0;

This creates a column-identical temporary table temp_table, empty of data, calling it the architectural twin for original_table.

Point to ponder: Limitations with primary keys

While this method is uncomplicated and effective, remember it does not replicate primary keys and constraints. So, you get the structure but not the regulatory complexities - essentially an architectural plan without the building codes.

Also, for PostgreSQL, the new temporary table will be within a session-specific schema, say 'pg_temp_3', providing a robust layer of isolation and data security.

Under the hood: What's not copied?

Your carbon-copy temp table doesn't carry everything from its permanent sibling. Column structures are cloned, but index properties and constraints aren't. Here's the typical missing list:

  • Indexes: Without these, temp table queries may not be as optimized.
  • Constraints: You don’t have a safety net against erroneous data insertion.
  • Triggers: Automatic responses to change events are amiss.
  • Row identities: You'll need to re-identify primary keys and unique IDs.

Code variants for different SQL flavors

Presenting, methods to create temp tables across different SQL flavors:

SQL Server

Ever friendly SQL Server allows on-the-fly creation of temp tables with SELECT INTO:

/* Copy-pasting, the DBA way! */ SELECT TOP 0 * INTO #CoolTempTable FROM OriginalStylishTable;

PostgreSQL

For the synonym-lovers at PostgreSQL, note this syntax adjustment:

/* PostgreSQL going "Can I copy your homework, but make it different?" */ CREATE TEMP TABLE cool_temp_table AS SELECT * FROM original_stylish_table LIMIT 0;

MySQL

MySQL uses a similar technique, with temp tables being session-based:

/* MySQL's version of a secret diary, visible to your session only */ CREATE TEMPORARY TABLE cool_temp_table AS SELECT * FROM original_stylish_table WHERE FALSE;

Handling outliers

Some days, you'll need your temp table to dress differently:

  • Column Renaming: Use aliases in your SELECT statement for friendlier column names.
  • Changing Data Types: Modify data types with CAST or CONVERT as needed.
  • Row Identifiers: Add extra SELECT columns to mimic identity columns or unique identifiers.