×

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL

 Introduction to Temporary Tables

  • Temporary table is a table which is used to store temporary data that can be used further in the same client session.
  • By default, temporary table is removed once the client session is terminated.
  • Temporary table can be removed explicitly using the statement “DROP TABLE”. This table is accessible only to its creator.
  • More than one temporary table can exist in the same session but they must contain different names if they are in the same session. But if the temporary tables are in the different session, then the tables can exist with the same names.
  • Temporary tables can have same name as that of normal tables within the same database. If such condition exists, then after the creation of temporary table all the queries executed will now be referencing to the temporary table instead of the normal table. But once this temporary table created with the same name is removed, the normal table is accessible and now the queries will be referencing to the normal table.
  • Creating a Temporary table

Syntax:

CREATE TEMPORARY TABLE TABLENAME (column_name1 datatype (size), column_name2 datatype (size), column_nameN datatype (size));

          Example:

We already have a database with name “employeedb” and a table with name “employee” in that database. Now we will create a temporary table with same name “employee”.

 mysql> USE employeedb;
 Database changed
 mysql> CREATE TEMPORARY TABLE employee(Emp_ID INT, Emp_Name VARCHAR(20),Emp_Salary INT);
 Query OK, 0 rows affected (0.32 sec)
 mysql> SELECT *FROM employee;
 Empty set (0.00 sec)
 mysql> INSERT INTO employee VALUES(1,"Mayuri",45000);
 Query OK, 1 row affected (0.08 sec)
 mysql> INSERT INTO employee VALUES(2,"Sakshi",50000);
 Query OK, 1 row affected (0.04 sec)
 mysql> SELECT *FROM employee;
 +--------+----------+------------+
 | Emp_ID | Emp_Name | Emp_Salary |
 +--------+----------+------------+
 |      1 | Mayuri   |      45000 |
 |      2 | Sakshi   |      50000 |
 +--------+----------+------------+
 2 rows in set (0.00 sec) 
How to Create Temporary Table in SQL
  • Now we have created a new table named as “employee” in the database “employeedb”.
  • So, just after the creation of temporary table when we are trying to retrieve the data from employee table, we got empty result set. This happens because now the employee table specified in the SELECT query is referencing to the newly created temporary table “employee” and not the existing table “employee”.
  • After that when we executed the INSERT query on employee table, then also this query is operated on the temporary table “employee” because we are executing this query after the temporary table creation.
  •  Drop a temporary table

Syntax:

DROP TABLE TABLENAME;

          Example:

Now we will delete a temporary table named as “employee”.

 mysql> SELECT *FROM employee;
 +--------+----------+------------+
 | Emp_ID | Emp_Name | Emp_Salary |
 +--------+----------+------------+
 |      1 | Mayuri   |      45000 |
 |      2 | Sakshi   |      50000 |
 +--------+----------+------------+
 2 rows in set (0.00 sec)
 mysql> DROP TABLE employee;
 Query OK, 0 rows affected (0.08 sec)
 mysql> SELECT *FROM employee;
 +--------+----------+------------+
 | Emp_ID | Emp_Name | Emp_Salary |
 +--------+----------+------------+
 |      1 | Mayuri   | 40000      |
 |      2 | Mayuri   | 40000      |
 |      3 | Mayuri   | 40000      |
 |      4 | Mayuri   | 40000      |
 |      5 | Mayuri   | 40000      |
 +--------+----------+------------+
 5 rows in set (0.09 sec) 
How to Create Temporary Table in SQL
  • When we execute the SELECT query, we are getting two records as an output because we have executed this query after temporary table creation. So, this SELECT query is operated on the temporary table.
  • After that we have executed the drop query. Using DROP command, newly created employee table (temporary table) will be dropped.
  • Again, when we execute the SELECT query, now it will be operated on our employee table (original table) not the temporary table because the temporary table is already removed from the database.

Related Topics

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 minutes read.

SQL Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

SQL Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

SQL Count

Structured Query Language Count() Function is used with Structured Query Language SELECT Statement. SQL Count() function returns the number of items that match the specified criteria in the SELECT statement. Count()...

2 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

SQL COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.