CREATETEMPORARYTABLESalesSummary( ->product_nameVARCHAR(50)NOTNULL ->,total_sa"/> CREATETEMPORARYTABLESalesSummary( ->product_nameVARCHAR(50)NOTNULL ->,total_sa">

Home  >  Article  >  Database  >  What are MySQL temporary tables? How do we create them?

What are MySQL temporary tables? How do we create them?

WBOY
WBOYforward
2023-09-06 10:57:02927browse

什么是 MySQL 临时表?我们如何创建它们?

As the name suggests, a temporary table is a table where we can save temporary data. The most important thing about temporary tables is that they are deleted when the current client session terminates. It can be created with the help of CREATE statement but we have to use keyword "Temporary" while creating it. To illustrate the creation of temporary table, we are using the following example -

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)

The above query has created the value and inserted it into a temporary table named "SalesSummary" middle.

The above is the detailed content of What are MySQL temporary tables? How do we create them?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete