Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MySQL Temporary Tables

MySQL Temporary Tables

MySQL temporary tables are very useful when we need to store some temporary data.

Temporary tables are visible only in the current connection; when the connection is closed, MySQL automatically deletes the table and frees all space.

In MySQL, a temporary table is a table that exists in the current session and is automatically destroyed when the session ends.

MySQL temporary tables are visible only in the current connection; if you use a PHP script to create a MySQL temporary table, the table is automatically destroyed each time the PHP script finishes executing.

If you use another MySQL client program to connect to the MySQL database server to create a temporary table, the table will be destroyed only when the client program is closed; of course, you can also destroy it manually.

Creating a Temporary Table

CREATE TEMPORARY TABLE temp_table_name (

  column1 datatype,

  column2 datatype,

  ...

);

Or abbreviated as:



CREATE TEMPORARY TABLE temp_table_name AS

SELECT column1, column2, ...

FROM source_table

WHERE condition;

Inserting Data into a Temporary Table



INSERT INTO temp_table_name (column1, column2, ...)

VALUES (value1, value2, ...);

Querying a Temporary Table



SELECT * FROM temp_table_name;

Modifying a Temporary Table

Modifying a temporary table is similar to modifying a regular table; you can use the ALTER TABLE command.



ALTER TABLE temp_table_name

ADD COLUMN new_column datatype;

Dropping a Temporary Table

Temporary tables are automatically destroyed when the session ends, but you can also explicitly drop them using DROP TABLE.



DROP TEMPORARY TABLE IF EXISTS temp_table_name;

Example

Example

-- Create a temporary table

CREATE

TEMPORARY

TABLE

temp_orders

AS

SELECT

FROM

orders

WHERE

order_date

=

'2023-01-01'

;

-- Query the temporary table

SELECT

FROM

temp_orders;

-- Insert data into the temporary table

INSERT

INTO

temp_orders

(

order_id

,

customer_id

,

order_date

)

VALUES

(

1001

,

1

,

'2023-01-05'

)

;

-- Query the temporary table

SELECT

FROM

temp_orders;

-- Drop the temporary table

DROP

TEMPORARY

TABLE

IF

EXISTS

temp_orders;

Temporary tables are very useful when you need to store intermediate result sets or perform complex queries within a session.

The scope of a temporary table is limited to the session that created it. Other sessions cannot directly access or reference the temporary table. When sharing data between multiple sessions, consider using a regular table instead of a temporary table.

Note that temporary tables are automatically dropped when the session ends, but you can also explicitly drop them using DROP TEMPORARY TABLE, which frees resources earlier.

Example

The following shows a simple example of using a MySQL temporary table; the following SQL code can be used with the mysql_query() function in PHP scripts.



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 SHOW TABLESWhen you use the command to display the list of data tables, you will not see the SalesSummary table.

If you exit the current MySQL session and then use the SELECTcommand to read the data of the previously created temporary table, you will find that the table no longer exists in the database, because it was destroyed when you exited.


Dropping a MySQL Temporary Table

By default, when you disconnect from the database, the temporary table is automatically destroyed. Of course, you can also use the ** DROP TABLE ** command in the current MySQL session to manually drop the temporary table.

The following is an example of manually dropping a 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 'RUNOOB.SalesSummary' doesn't exist