Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

MySQL Temporary Tables คืออะไร และการสร้าง

ตารางชั่วคราว (Temporary Tables) ใน MySQL

Temporary Tables ใช้เก็บข้อมูลชั่วคราว จะถูกลบอัตโนมัติเมื่อเซสชันของไคลเอนต์สิ้นสุด สร้างด้วยคำสั่ง CREATE 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 |
+--------------+-------------+----------------+------------------+

คำสั่งข้างต้นสร้างและแทรกข้อมูลลงตารางชั่วคราว SalesSummary เรียบร้อย