Leilin Peng Share: MySQL temporary table

  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.

  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 rarely have such a low re-use version of the MySQL database server.

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

  If you use 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 also manually destroyed.

  Examples

  The following shows a simple example of using MySQL temporary table, the following SQL code can be applied to mysql_query PHP script () 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 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, then 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

  );

  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

  This article reprinted from: w3cschool (edit: Leilin Peng Source: network intrusion deleted)

Guess you like

Origin www.cnblogs.com/linpeng1/p/10943322.html