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. The following shows a simple example of using a MySQL temporary table, and the following SQL code can be applied to the 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. 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:Example ¶
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)
Delete MySQL 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