MySQL Temporary Table

 

Summary: in this tutorial, we will discuss about MySQL temporary table and show you how to create, use and drop temporary tables.

Introduction to MySQL temporary table

In MySQL, a temporary table is a special type of table that allows you to store a temporary result set, which you can reuse several times in a single session. A temporary table is very handy when it is impossible or expensive to query data that requires a single SELECT statement with JOIN clauses. You often use temporary tables in stored procedures to store immediate result sets for the subsequent uses.

MySQL temporary tables have some additional features:

  • A temporary table is created by using CREATE TEMPORARY TABLE statement. Notice that theTEMPORARY keyword is added between CREATE and TABLE keywords.
  • MySQL drops the temporary table automatically when the session ends or connection is terminated. Of course, you can use the DROP TABLE statement to drop a temporary table explicitly when you are no longer use it.
  • A temporary table is only available and accessible by the client who creates the table.
  • Different clients can create a temporary table with the same name without causing errors because only the client who creates a temporary table can see it. However, in the same session, two temporary tables cannot have the same name.
  • A temporary table can have the same name as an existing table in a database. For example, if you create a temporary table named employees in the sample database, the existing employeestable becomes inaccessible. Every query you issue against the employees table refers to theemployees temporary table. When you remove the employees temporary table, the permanent employees table is available and accessible again. Though this is allowed however it is not recommended to create a temporary table whose name is same as a name of a permanent table because it may lead to a confusion. For example, in case the connection to the MySQL database server is lost and you reconnect to the server automatically, you cannot differentiate between the temporary table and the permanent table. In the worst case, you may issue a DROP TABLE statement to remove the permanent table instead of the temporary table, which is not expected.

Create MySQL temporary table

Like the CREATE TABLE statement, MySQL provides many options to create a temporary table. To create a temporary table, you just add the TEMPORARY keyword to the CREATE TABLE statement.

For example, the following statement creates a top 10 customers by revenue temporary table based on the result set of a SELECT statement:

Now, you can query data from the top10customers temporary table as from a permanent table:

MySQL Temporary Table - Top 10 customers by revenue

Drop MySQL temporary table

You can use the DROP TABLE statement to remove temporary tables however it is good practice to use the DROP TEMPORARY TABLE statement instead. Because the DROP TEMPORARY TABLE removes only temporary tables, not the permanent tables. In addition, the DROP TEMPORARY TABLE statement helps you avoid the mistake of removing a permanent table when you name your temporary table the same as the name of the permanent table.

For example, to remove the top10customers temporary table, you use the following statement:

Notice that if you try to remove a permanent table with the DROP TEMPORARY TABLE statement, you will get an error message saying that the table you are trying drop is unknown.

Note if you develop an application that uses a connection pooling or persistent connections, it is not guaranteed that the temporary tables are removed automatically when your application is terminated. Because the database connection that the application used may be still open and is placed in a connection pool for other clients to reuse it. This means you should always remove the temporary tables that you created whenever you are done with them.

In this tutorial, you have learned about MySQL temporary table and its characteristic. We also gave you an example of how to create, use and drop a temporary table.

转载于:https://www.cnblogs.com/hephec/p/4570613.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值