MySQL MariaDB

How to use the SELECT INTO TEMP table statement in MySQL

Sometimes, we are supposed to fetch out data from different MySQL tables which we can use for some specific purpose. For our ease, we create a temporary table and place all the data there which we can later use. When the session is over, the table will vanish on its own, else it can delete it using the DROP command. Similarly, to create the clone of the table we use the statement “SELECT INTO”, which not only copies the contents of the selected table but also inserts all the content into the newly created table.

In this write-up, we will learn how to use the statement “SELECT INTO TEMP TABLE” in MySQL and how this statement works.

What is Select into temp table

According to the discussion above, we know the statement “SELECT INTO TEMP TABLE” is a combination of two different statements that have their own separate function, by using this statement we can execute multiple functions using a single statement. The functions that can be performed with the above statement are:

  • Creating a new temporary table
  • Making a clone of the existing table
  • Reading its file
  • Inserting its all file into the newly created temporary table

The general syntax of using this statement is:

SELECT * Column1, Column2,....,ColumnN
INTO #destinationForTemporarytable
FROM existing table
WHERE Condition

But this syntax is applicable in SQL only not in MySQL, but we can have the same results of the above statement in MySQL in other ways.

How to insert data in a temporary table using MySQL?

To copy data from any existing table into the temporary table in MySQL, we should first create a temporary table, named, temporary_Data, using clause “TEMPORARY TABLE” and also define columns of the table.

CREATE TEMPORARY TABLE temporary_Data (ids INT,name VARCHAR(50));

To display all the tables of the database, use:

SHOW TABLES;

The created table is not in the list of tables which confirms the table is temporary, now to display the temporary table, we use:

SELECT * FROM temporary_Data;

The output is showing “Empty set” because there is no data inserted in the table, but it confirmed the existence of the temporary table. To copy the entire data of any existing table having the same number of columns, we will first insert the data into a temporary table by using “INSERT INTO”  and then select the existing table from where we are supposed to copy the data.

The general syntax would be like this:

INSERT INTO temporary_tabel_name SELECT * FROM existing table_name;

Following the general syntax, we will copy the data from the existing table, named, Guys into the newly created temporary table, named, “temporary_data”.

INSERT INTO temporary_Data SELECT * FROM  Employee_data;

To display the temporary table,

SELECT * FROM temporary_Data;

All the data of table “Employee_data” has been copied in the temporary table “temporary_Data”.  Now if we want to copy and paste the data of a specific column, let’s say, we want to copy “id” from the existing table “Grocery_bill” to the column “ids” of the existing temporary table, “temporary_Data”, we will run the following statements.

INSERT INTO temporary_Data(ids) SELECT id FROM Grocery_bill;

To view the temporary table:

SELECT * FROM temporary_Data;

From the output, we can observe the column from the table “Grocey_bill” has been copied and pasted in the column of the temporary table where “NULL” is in the columns next to the new entries showing there are no values in them. Hence we can copy the entire column as well as any specific columns to the new table.

We can also delete the temporary table by using the clause “DROP TEMPORARY TABLE”, for example, if we want to delete the temporary table, named, “temporary_Data”, we use:

DROP TEMPORARY TABLE temporary_Data;

The temporary table has been deleted successfully.

Conclusion

To extract different data from different tables is slightly easy if we collect all the data in a temporary table. This write-up assists us in understanding that we can create the temporary table by using the clause  “TEMPORARY TABLE” and can copy either the entire data or some specific column from The existing table into the temporary table.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.