MySQL

How to Create a Temporary Table in MySQL

In this tutorial, we are going to see how to create a temporary table in MySQL. A temporary table in MySQL is an extremely useful and flexible feature that allows you to accomplish complex tasks quickly.
 

How to create a temporary table in MySQL

To create a temporary table in MySQL, we add the TEMPORARY keyword in CREATE TABLE statement. The following example shows how to create a temporary table.

CREATE TEMPORARY TABLE Product (
	name VARCHAR(50) NOT NULL,
	price DECIMAL(7,2) NOT NULL,
	total DECIMAL(12,2) NOT NULL
);
 
Insert some data into “Product” table.

INSERT INTO Product (name, price, total) VALUES ('pc', 150, 350.99);

Display all data in “Product” table.

SELECT * FROM Product;

Output:

+----------+-----------+----------+
|   Name   |   Price   |   Total  |
+----------+-----------+----------+
|    PC    |    150    |  350.99  |
+----------+-----------+----------+

When you run SHOW TABLES command, your temporary table is not listed in the list. If you log out of MySQL session and run SELECT command again, you will not find any data available in the database. Even your temporary table will be deleted.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *