Latest web development tutorials

MySQL temporary tables

MySQL temporary tables we need to save some temporary data is very useful. The temporary table is visible in the current connection, when the connection is closed, Mysql will automatically delete the table and release all the space.

Temporary tables in MySQL version 3.23 is added, if your MySQL version earlier than version 3.23 can not use temporary tables MySQL. But now there is another rarely use such a low version of the MySQL database server.

MySQL temporary tables only visible in the current connection, if you use PHP MySQL script to create a temporary table, then when did the PHP script execution is complete, the temporary table is automatically destroyed.

If you use other MySQL client program to connect MySQL database server to create a temporary table, then only when you close the client program will destroy the temporary table, of course, you can also manually destroyed.

Examples

The following shows a simple example of the use of MySQL temporary table, the following SQL code can be applied to the PHP script 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 command to display a list of the data table, you will not see SalesSummary table.

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


Delete MySQL temporary tables

By default, when you disconnect the connection to the database, the temporary table will automatically be destroyed. Of course, you can also manually delete the temporary table using DROP TABLE command in the current MySQL session.

The following is to manually delete the temporary table Example:

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 'w3big.SalesSummary' doesn't exist