Thursday, May 16, 2013

Create temporary table in MySQL

Temporary tables are useful when you need to do something on the fly. Instead of writing full CREATE TABLE statement and then inserting from another table, you can do the following with one query: 
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
Example:
CREATE TEMPORARY TABLE tmp_tbl as (SELECT * FROM tbl) 
Table with name tmp_tbl is created with the same columns and data is copied from tbl.

You can do the same with using LIKE:
CREATE TABLE tmp_tbl LIKE tbl
 But, again you will need another query for inserting data.

No comments:

Post a Comment