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

前端 未结 6 593
长发绾君心
长发绾君心 2021-01-30 04:03

I need to create a temp table with same columns and type as a permanent table. What is the best way to do it? (The Permanent table has over 100 columns)

i.e.

Usu

6条回答
  •  闹比i
    闹比i (楼主)
    2021-01-30 04:28

    This is a MySQL-specific answer, not sure where else it works --

    You can create an empty table having the same column definitions with:

    CREATE TEMPORARY TABLE temp_foo LIKE foo;
    

    And you can create a populated copy of an existing table with:

    CREATE TEMPORARY TABLE temp_foo SELECT * FROM foo;
    

    And the following works in postgres; unfortunately the different RDBMS's don't seem very consistent here:

    CREATE TEMPORARY TABLE temp_foo AS SELECT * FROM foo;
    

提交回复
热议问题