4.2.27. MySQL temporary table

发布时间 :2025-10-25 12:31:18 UTC      

MySQL temporary tables are very useful when we need to save some temporary data. Temporary tables are visible only in the current connection, and when you close the connection, Mysql automatically deletes the table and frees up all space.

Temporary tables are added in version 3.23 of MySQL. If you have a version of MySQL earlier than version 3.23, you cannot use temporary tables of MySQL. However, it is rare to use such a low version of the MySQL database service these days.

The MySQL temporary table is only visible in the current connection. If you use the PHP script to create a MySQL temporary table, it will be automatically destroyed every time the PHP script is executed.

If you use another MySQL client program to connect to the MySQL database server to create a temporary table, the temporary table will only be destroyed when the client program is closed, or you can destroy it manually.

Example

The following shows a simple example of using a MySQL temporary table, and the following SQL code can be applied to the mysql_query() Function.

mysql> CREATE TEMPORARY TABLE SalesSummary (
    -> product_name VARCHAR(50) NOT NULL
    -> , total_sales DECIMAL(12,2) NOT NULL DEFAULT 0.00
    -> , avg_unit_price DECIMAL(7,2) NOT NULL DEFAULT 0.00
    -> , total_units_sold INT UNSIGNED NOT NULL DEFAULT 0
);
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO SalesSummary
    -> (product_name, total_sales, avg_unit_price, total_units_sold)
    -> VALUES
    -> ('cucumber', 100.25, 90, 2);

mysql> SELECT * FROM SalesSummary;
+--------------+-------------+----------------+------------------+
| product_name | total_sales | avg_unit_price | total_units_sold |
+--------------+-------------+----------------+------------------+
| cucumber     |      100.25 |          90.00 |                2 |
+--------------+-------------+----------------+------------------+
1 row in set (0.00 sec)

When you use SHOW TABLES When the command displays a list of data tables, you will not be able to see the SalesSummary table.

If you exit the current MySQL session, use the SELECT Command to read the previously created temporary table data, you will find that the table does not exist in the database, because the temporary table has been destroyed when you exit.

Delete MySQL temporary table

By default, when you disconnect from the database, the temporary table is automatically destroyed. Of course, you can also use it in the current MySQL session DROP TABLE Command to manually delete the temporary table.

The following is an example of manually deleting a temporary table:

mysql> CREATE TEMPORARY TABLE SalesSummary (
    -> product_name VARCHAR(50) NOT NULL
    -> , total_sales DECIMAL(12,2) NOT NULL DEFAULT 0.00
    -> , avg_unit_price DECIMAL(7,2) NOT NULL DEFAULT 0.00
    -> , total_units_sold INT UNSIGNED NOT NULL DEFAULT 0
);
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO SalesSummary
    -> (product_name, total_sales, avg_unit_price, total_units_sold)
    -> VALUES
    -> ('cucumber', 100.25, 90, 2);

mysql> SELECT * FROM SalesSummary;
+--------------+-------------+----------------+------------------+
| product_name | total_sales | avg_unit_price | total_units_sold |
+--------------+-------------+----------------+------------------+
| cucumber     |      100.25 |          90.00 |                2 |
+--------------+-------------+----------------+------------------+
1 row in set (0.00 sec)
mysql> DROP TABLE SalesSummary;
mysql>  SELECT * FROM SalesSummary;
ERROR 1146: Table 'RUNOOB.SalesSummary' doesn't exist

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.