什么是MySQL临时表?我们如何创建它们?
如其名称,临时表是在其中存储临时数据的表。临时表最重要的一点是当当前客户端会话终止时,它们将被删除。可以通过使用CREATE语句创建它们,但是我们必须在创建时使用关键字”Temporary”。为了说明如何创建临时表,我们使用以下示例:
阅读更多:MySQL 教程
示例
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)
以上查询已经创建并插入了一些值到一个名为‘SalesSummary’的临时表。