Site icon TestingDocs.com

MySQL CREATE TEMPORARY TABLE Statement

Overview

In this tutorial, we will learn MySQL CREATE TEMPORARY TABLE statement. We can use the CREATE TEMPORARY TABLE statement to create temporary table.

Syntax

CREATE TEMPORARY TABLE <table_name>

<SELECT query>

To create temporary tables, we must have the CREATE TEMPORARY TABLES privilege.

A TEMPORARY table is visible only to the current session, and is dropped automatically when the session is closed. This means that two different sessions can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. The statement creates a table that is kept until the client disconnects.

Example

Let’s create a temporary table using the Country table from the world MySQL database.

mysql> CREATE TEMPORARY TABLE TEMP_US_Countries
              -> SELECT Name, Population
              -> FROM Country
              -> WHERE Continent = ‘North America’;

Query OK, 37 rows affected (0.06 sec)
Records: 37 Duplicates: 0 Warnings: 0

mysql> SELECT COUNT(*) FROM
-> TEMP_US_Countries;
+———-+
| COUNT(*) |
+———-+
| 37 |
+———-+

A temporary table is only visible to the client that created it. A temporary table of the same name as an existing permanent table causes the temporary table to override the existing table until the connection or the temporary table is dropped.

MySQL Tutorials

MySQL Tutorials on this website:

https://www.testingdocs.com/mysql-tutorials-for-beginners/

For more information on MySQL Database:

https://www.mysql.com/

Exit mobile version