Some basic usage of temporary tables in MySQL _ MySQL

Source: Internet
Author: User
This article mainly introduces some basic usage methods of temporary tables in MySQL, which is the basic knowledge in MySQL getting started. it may be very useful for anyone who needs to refer to the temporary tables, in some cases, temporary data is maintained. The most important thing is that the temporary tables are deleted when the current client session ends.

Add MySQL 3.23 to the temporary table. If you are using an old version of MySQL with a ratio of 3.23, you can use a non-temporary table, but you can use a heap table.

As mentioned above, temporary tables will only last as long as Sessions exist. If you run the code in a PHP script, the temporary table will be destroyed and the script will be automatically executed. If you have connected to the MySQL database server, the temporary table of the MySQL client program will exist until the client is closed or the table is manually damaged.
Instance

The following is an example. you can use the same code in a PHP script using the mysql_query () function.

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 a show tables command is issued, the temporary table will not be listed in the list. If you cancel a MySQL session, the SELECT command will be issued, and no data is found in the database. The temporary table does not exist.
Delete temporary table:

By default, when all temporary tables are deleted, the MySQL database connection is terminated. However, before deleting them, you should issue the drop table command.

The following example deletes 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 'TUTORIALS.SalesSummary' doesn't exist

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.