当前位置:网站首页>学习MySQL 临时表
学习MySQL 临时表
2022-08-10 14:18:00 【微服务 spring cloud】
MySQL 临时表
MySQL 临时表在我们需要保存一些临时数据时是非常有用的。临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间。
临时表在MySQL 3.23版本中添加,如果你的MySQL版本低于 3.23版本就无法使用MySQL的临时表。不过现在一般很少有再使用这么低版本的MySQL数据库服务了。

MySQL临时表只在当前连接可见,如果你使用PHP脚本来创建MySQL临时表,那每当PHP脚本执行完成后,该临时表也会自动销毁。
如果你使用了其他MySQL客户端程序连接MySQL数据库服务器来创建临时表,那么只有在关闭客户端程序时才会销毁临时表,当然你也可以手动销毁。
实例
以下展示了使用MySQL 临时表的简单实例,以下的SQL代码可以适用于PHP脚本的mysql_query()函数。
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)当你使用 SHOW TABLES命令显示数据表列表时,你将无法看到 SalesSummary表。
如果你退出当前MySQL会话,再使用 SELECT命令来读取原先创建的临时表数据,那你会发现数据库中没有该表的存在,因为在你退出时该临时表已经被销毁了。
删除MySQL 临时表
默认情况下,当你断开与数据库的连接后,临时表就会自动被销毁。当然你也可以在当前MySQL会话使用 DROP 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 'XXXXXX.SalesSummary' doesn't exist边栏推荐
- SecureCRTPortable – 破解
- How is the monthly salary table stored in the database?Ask for a design idea
- Lack of comparators, op amps come to the rescue!(Op amp is recorded as a comparator circuit)
- "Thesis Reading" PLATO: Pre-trained Dialogue Generation Model with Discrete Latent Variable
- Makefile missing separator. Stop.怎么解决「建议收藏」
- Summary of Force Buckle Solution 640 - Solving Equations
- MySQL advanced (thirty-three) MySQL data table adding fields
- 开源SPL消灭数以万计的数据库中间表
- MySQL - storage engine for databases
- 易观分析联合中小银行联盟发布海南数字经济指数,敬请期待!
猜你喜欢
随机推荐
Parallels 将扩展桌面平台产品,以进一步改善在 Mac 上运行 Windows 的用户体验和工作效率
How does IT Xiaobai learn PHP systematically
Circle 创始人回应美财政部禁止 Tornado :隐私与安全之间关系紧张
八大排序总是忘?快来这里~
第五讲 测试技术与用例设计
AWS Security Fundamentals
Borg Maze (bfs+最小生成树)
基于ArcGIS水文分析、HEC-RAS模拟技术在洪水危险性及风险评估
快速了解大端模式和小端模式
系统架构系列文章三--解决传统企业核心系统的性能问题
面试面到了一个腾讯30k出来的,有见识到何为精通MySQL调优
中学数学建模书籍及相关的视频等(2022.08.09)
Send a post request at the front desk can't get the data
[JS Advanced] Creating sub-objects and replacing this_10 in ES5 standard specification
【MinIO】工具类使用
注意力模型---Attention Model
Matlab画分段函数「建议收藏」
leetcode 739. Daily Temperatures 每日温度(中等)
Classifying irises using decision trees
Flask框架——MongoEngine使用MongoDB数据库








