Wu Yuxiong - natural born MySQL study notes: MySQL temporary table

MySQL temporary table when we need to save some temporary data is very useful. Temporary tables only visible in the current connection, when the connection is closed, Mysql will automatically delete the table and release all the space.
MySQL temporary table is only visible in the current connection, if you use PHP MySQL script to create a temporary table, then whenever PHP script execution is complete, the temporary table is automatically destroyed.
If other MySQL client programs connect to the 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 manually destroyed.
Simple example using MySQL temporary table, the following SQL code can be applied to 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
);
mysql> INSERT INTO SalesSummary
    -> (product_name, total_sales, avg_unit_price, total_units_sold)
    -> VALUES
    -> ('cucumber', 100.25, 90, 2);

mysql> SELECT * FROM SalesSummary;
When using the 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 original data create a temporary table, you will find that there is no database in the table, because when you exit the temporary table has been destroyed.
Delete the temporary table MySQL
By default, when you disconnect the connection to the database, the temporary table will automatically be destroyed. Of course, you can also use the DROP TABLE command in the current session to manually delete the temporary MySQL table.

The following are examples of manually delete the 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
);
mysql> INSERT INTO SalesSummary
    -> (product_name, total_sales, avg_unit_price, total_units_sold)
    -> VALUES
    -> ('cucumber', 100.25, 90, 2);
mysql> DROP TABLE SalesSummary;
mysql>  SELECT * FROM SalesSummary;

 

Guess you like

Origin www.cnblogs.com/tszr/p/12114512.html