Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Bảng tạm MySQL là gì? Cách tạo bảng tạm

Bảng tạm (Temporary tables) dùng để lưu dữ liệu tạm thời. Đặc điểm quan trọng: tự động xóa khi phiên làm việc (client session) kết thúc. Tạo bằng CREATE TEMPORARY TABLE.

Ví dụ

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)

Các truy vấn trên đã tạo và chèn dữ liệu vào bảng tạm SalesSummary.