MySQL Temporary Table
2015-06-12 00:22
567 查看
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 SELECTstatement 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
CREATETEMPORARYTABLEstatement. Notice that the
TEMPORARYkeyword is added between
CREATEand
TABLEkeywords.
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
employeesin the sample database, the existing
employeestable becomes inaccessible. Every query you issue against the
employeestable refers to the
employeestemporary table. When you remove the
employeestemporary table, the permanent
employeestable 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 TABLEstatement to remove the permanent table instead of the temporary table, which is not expected.
Create MySQL temporary table
Like the CREATETABLEstatement, MySQL provides many options to create a temporary table. To create a temporary table, you just add theTEMPORARYkeyword to the
CREATETABLEstatement.
For example, the following statement creates a top 10 customers by revenue temporary table based on the result set of a
SELECTstatement:
CREATETEMPORARYTABLEtop10customers
SELECTp.customerNumber,
c.customerName,
FORMAT(SUM(p.amount),2) total
FROM payments p
INNER JOIN customers c ON c.customerNumber = p.customerNumber
GROUP BY p.customerNumber
ORDER BY total DESC
LIMIT 10
Now, you can query data from the
top10customerstemporary table as from a permanent table:
1 | SELECT* FROM top10customers |
Drop MySQL temporary table
You can use the DROP TABLEstatement to remove temporary tables however it is good practice to use theDROP TEMPORARYTABLEstatement instead. Because the
DROP TEMPORARYTABLEremoves only temporary tables, not the permanent tables. In addition, the
DROP TEMPORARYTABLEstatement 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
top10customerstemporary table, you use the following statement:
1 | DROP TEMPORARYTABLEtop10customers |
DROP TEMPORARYTABLEstatement, 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.
相关文章推荐
- 主从延时解决办法
- MySQL修改列属性,长度
- mysql的 mix模式作为主从备份,程序编写规范
- R语言与mysql结合处理交通数据及其算法优化
- MySQL Regexps
- 上课笔记第二十九天、mysql、mysql编译安装及组成、客户端、变量和类型
- 2015061105 - Mysql大小写敏感的问题
- (DBA之路【四】)mysql show 指令集合
- mysql 同步
- 【实验总结二】如何从文本中批量导入数据
- MySQL第五个学习笔记 该数据表的操作
- mysql 5.5服务器基本优化
- LAMP环境搭建1-mysql5.5
- SQL结构化查询语言及Mysql基本操作
- mysql 主从切换
- 【MySql】MySQL的转义字符“\” \'使用说明
- mysql使用时的一些常用命令
- Mysql主从复制(数据故障恢复恢复)---论坛那些事儿
- mysql 表如果 key相同则累加,不相同则插入
- mysql 5.6 主从复制配制