Some basic methods of using temporary tables in Mysql _mysql

Source: Internet
Author: User
Tags mysql version php script

Temporary tables can be very useful, in some cases, to maintain temporary data. Most importantly, the temporary tables that should be known are deleted when they terminate the current client session.

Add MySQL version 3.23 to the temp table. If you are using an older version of MySQL than 3.23, you may not use a temporary table, but you can use a heap table.

As mentioned previously the temporary table will only persist as long as the session is present. If you run code in a PHP script, the temporary table will be destroyed when the script is automatically executed. If you are connected to a MySQL database server, the temporary table for the client program via MySQL will persist until the client or manually corrupted table is turned off.
Instance

Here is an example of using a temporary table in a PHP script that uses the mysql_query () function to use the same code.

 mysql> CREATE temporary TABLE salessummary (-> product_name-VARCHAR () 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_pric

E, 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. Now if you log off the MySQL session, the Select command will be issued and you will find no data in the database. Even the temporary table will not exist.
To delete a temporary table:

By default, MySQL's database connection is terminated when all temporary tables are deleted. However, you should issue a drop table command before you can delete them.

The following example deletes a temporary table.

mysql> CREATE Temporary TABLE salessummary (-> product_name VARCHAR () not NULL->, Total_sales DECIMAL (1 2,2) NOT NULL default 0.00->, Avg_unit_price DECIMAL (7,2) NOT NULL default 0.00->, Total_units_sold INT unsi
gned not NULL DEFAULT 0); Query OK, 0 rows Affected (0.00 sec) mysql> INSERT into Salessummary-> (product_name, Total_sales, Avg_unit_pric

E, 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


Related Article

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.