This article mainly introduces some basic usage methods of temporary tables in MySQL, which is the basic knowledge in MySQL getting started. it may be very useful for anyone who needs to refer to the temporary tables, in some cases, temporary data is maintained. The most important thing is that the temporary tables are deleted when the current client session ends.
Add MySQL 3.23 to the temporary table. If you are using an old version of MySQL with a ratio of 3.23, you can use a non-temporary table, but you can use a heap table.
As mentioned above, temporary tables will only last as long as Sessions exist. If you run the code in a PHP script, the temporary table will be destroyed and the script will be automatically executed. If you have connected to the MySQL database server, the temporary table of the MySQL client program will exist until the client is closed or the table is manually damaged.
Instance
The following is an example. you can use the same code in a PHP script using 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 a show tables command is issued, the temporary table will not be listed in the list. If you cancel a MySQL session, the SELECT command will be issued, and no data is found in the database. The temporary table does not exist.
Delete temporary table:
By default, when all temporary tables are deleted, the MySQL database connection is terminated. However, before deleting them, you should issue the drop table command.
The following example deletes 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 'TUTORIALS.SalesSummary' doesn't exist