×

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 SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL Data Control Language

Data Control Language decides to whom should (which user) permit access privileges. GRANT and REVOKE are the commands of DCL. GRANT: It gives privileges to user. REVOKE: It takes back privileges from granted...

1 minute read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 minutes read.

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 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.

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 SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows: UNION Operator.UNION ALL’ Operator.INTERSECT Operator.MINUS Operator. Rules to be...

8 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

SQL DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.

SQL SELECT WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples. The WHERE clause in SELECT query displays those records as...

5 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.