Temporary tables can be very useful, in some cases, to maintain temporary data. Most importantly, the temporary tables that should be known are deleted when they terminate the current client session.
Add MySQL version 3.23 to the temp table. If you are using an older version of MySQL than 3.23, you may not use a temporary table, but you can use a heap table.
As mentioned previously the temporary table will only persist as long as the session is present. If you run code in a PHP script, the temporary table will be destroyed when the script is automatically executed. If you are connected to a MySQL database server, the temporary table for the client program via MySQL will persist until the client or manually corrupted table is turned off.
Instance
Here is an example of using a temporary table in a PHP script that uses the mysql_query () function to use the same code.
mysql> CREATE temporary TABLE salessummary (-> product_name-VARCHAR () 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_pric
E, 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. Now if you log off the MySQL session, the Select command will be issued and you will find no data in the database. Even the temporary table will not exist.
To delete a temporary table:
By default, MySQL's database connection is terminated when all temporary tables are deleted. However, you should issue a drop table command before you can delete them.
The following example deletes a temporary table.
mysql> CREATE Temporary TABLE salessummary (-> product_name VARCHAR () not NULL->, Total_sales DECIMAL (1 2,2) NOT NULL default 0.00->, Avg_unit_price DECIMAL (7,2) NOT NULL default 0.00->, Total_units_sold INT unsi
gned not NULL DEFAULT 0); Query OK, 0 rows Affected (0.00 sec) mysql> INSERT into Salessummary-> (product_name, Total_sales, Avg_unit_pric
E, 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