In this article, we are going to learn how to create a temp table and further drop these tables. The temporary tables are session-specific tables that are created within the session. When the session closes, the table is dropped by itself. A temporary table exists within a session only; therefore, a table created by one session is not visible to another session. For example, Session ID 501 has created a temporary table then session ID 502 cannot access it or manipulate it. Two different sessions can have a temporary table with the same name.
The temporary tables are useful when you want to use a small subset of the large table, and it is being used multiple times within a stored procedure. In such cases, instead of applying the filter on the table multiple times, you can save the subset of large tables in a temporary table and use it within a stored procedure.
Important facts of the temporary tables:
- MySQL Temporary table has a decoupled relationship with the database schema, which means that when we drop the MySQL database, it does not necessarily drop the temporary tables within the database
- The InnoDB database engine does not support the compressed, temporary table. If you had enabled the InnoDB_strict_mode parameter, then it will return an error. If you disabled the InnoDB_strict_mode parameter, MySQL will issue a warning and create a table with a non-compressed row format
- To create a temporary table within the stored procedure, the user must have create temporary table privilege. Once it is granted, then to update, delete, or insert the data within that temporary table does not require any special privileges. There is one implication of this behavior. Suppose the stored procedure that uses a temporary table and a USER 1 who had developed the procedure has the create temporary table privilege. Now, USER 2 tries to execute it, but it does not have the create temporary table permission. In such cases, MySQL uses the privileges of the USER 1 to create the temporary table. Once the procedure completes successfully, the privileges will be reverted
-
You cannot create a temporary table based on the definition that the table already has, and it resides on MySQL tablespace, a general tablespace, or InnoDB tablespace. To create a temporary table based on the definition of another table, you must use the following syntax
1Select temporary table.. select * from
- Unlike regular create table statements, the Create temporary table statement does not cause the implicit commit
- The temporary table can have the same name as the MySQL regular table has. For example, even though a table named employee exists on the database, you can create a temporary table named employee in the database. In such cases, the MySQL table becomes inaccessible. Once the temporary table is dropped, the physical table can be accessed again. So it is always a good practice to create a temporary table with some unique name or make sure that the application drops the temporary table immediately after its purpose is completed
MySQL Temporary table examples
The syntax to create a temp table is as follows:
1 2 3 4 5 6 7 |
create temporary table tblemployee ( Column_1 datatype, Column_2 datatype, Column_3 datatype, … ) |
As you can see, the syntax to create a temporary table is the same as the syntax of creating a MySQL table. The only difference is that you must specify the temporary keyword between create and table keywords. This is very important because if you forget to add the keyword, it creates a regular MySQL table.
Now, let us walk through some examples of the temporary table. I am going to demonstrate the following use cases:
- Create a temp table and insert data in the table
- Create a temp table using the output of the select statement
- Drop the temp table
- Create a temp table using the structure of another table
Create a temp table and insert data
To create a new temp table named tblemployee on the MySQL database, execute the following query:
1 2 3 4 5 6 7 |
create temporary table tblemployee ( id int auto_increment Primary key, emp_name varchar(500), emp_address varchar(500), emp_dept_id int ) |
Execute the following query to insert the data in the temp table.
1 2 3 4 5 |
insert into tblemployee(emp_name, emp_address,emp_dept_id) values ('Nisarg Upadhyay','Mehsana',10), ('Nirali U','Mehsana',20), ('Milan J','Mehsana',5); |
Once data is inserted in the temp table, execute the following query to verify that data have been inserted.
1 |
Select * from tblemployee; |
The following is the output:
Now, to verify the scope of the tblemployee table, let us run the same query from the MySQL command-line client. To do that, open it, provide a password to connect to the MySQL Server, and execute the following queries:
1 2 |
Use sakila; Select * from tblemployee; |
Output: as you can see, the query has given an error:
Create a temp table using the output of the select statement
Suppose I want to create a temp table using the output of the select query. For example, I want to insert the details of the films table with its category in a temp table named temp_film_details. To do that, execute the following query.
1 2 3 4 5 6 |
create temporary table tbl_film_details select title, description,release_year,rental_rate ,rental_duration,special_features,replacement_cost,c.name from film a inner join film_category b on a.film_id=b.film_id inner join category c on b.category_id=c.category_id Limit 10; |
Once details are inserted in the temp table, execute the following query to view the data of the temp table.
1 |
Select * from tbl_film_details |
Output:
Drop temp Tables
To drop the temp table, we can use the following syntax:
1 |
Drop temporary table tbl_film_details |
As I mentioned that the on a database a temporary table and an InnoDB table can have same name so make sure you specify the temporary keyword; otherwise, if a table with the same will be dropped
Summary
In this article, we have learned about MySQL temp tables, and the characteristics of it. We also learned how we could create and drop a temp table with different use cases.
Table of contents
- Different ways to identify and change compatibility levels in SQL Server - July 22, 2024
- Copy SQL Databases between Windows 10 and CentOS using SQL Server data tools - October 19, 2022
- Changing the location of FILESTREAM data files in SQL Database - October 14, 2022