当前位置:网站首页>学习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边栏推荐
- "Thesis Reading" PLATO: Pre-trained Dialogue Generation Model with Discrete Latent Variable
- SQL学习(基础)
- How is the monthly salary table stored in the database?Ask for a design idea
- Error: Rule can only have one resource source (provided resource and test + include + exclude)
- vue 怎么清除tab 切换缓存问题 ?
- List集合
- SecureCRTPortable – 破解
- 黑客入门,从HTB开始
- 缺少比较器,运放来救场!(运放当做比较器电路记录)
- tampercfg内核模块导致机器频繁crash
猜你喜欢

基于ArcGIS水文分析、HEC-RAS模拟技术在洪水危险性及风险评估

Do not access Object.prototype method ‘hasOwnProperty‘ from target object....
![[target detection] small script: extract training set images and labels and update the index](/img/9d/0f88b484cee1b85df6bc1153d9b6b4.png)
[target detection] small script: extract training set images and labels and update the index

重要通知 | “移动云杯”算力网络应用创新大赛初赛延期!!

AWS Security Fundamentals

laravel 抛错给钉钉

关于已拦截跨源请求CORS 头缺少 ‘Access-Control-Allow-Origin‘问题解决

MySQL - storage engine for databases

【量化交易行情不够快?】一文搞定通过Win10 wsl2 +Ubuntu+redis+pickle实现股票行情极速读写

写不完的数学试卷-----试卷生成器(Qt含源码)
随机推荐
How does IT Xiaobai learn PHP systematically
How does vue clear the tab switching cache problem?
SenseTime self-developed robotic arm, the first product is an AI chess-playing robot: Guo Jingjing is also invited as an endorsement
司空见惯 - 股市狠狠下跌后,何時能反弹?
2022-08-09: What does the following go code output?A: No, it will panic; B: Yes, it can run correctly; C: Not sure, see the voting result.package main import (“fmt“ “syn
A method that can make large data clustering 2000 times faster
什么?你还不会JVM调优?
面试面到了一个腾讯30k出来的,有见识到何为精通MySQL调优
【ECCV 2022|Millions of Prizes】PSG Competition: Pursuing the "Most Comprehensive" Scene Understanding
laravel 抛错给钉钉
黑客入门,从HTB开始
Summary of Force Buckle Solution 640 - Solving Equations
win2012安装Oraclerac失败
EVE模拟器的使用-带图超详细(学网络用)「建议收藏」
记录几道整型提升的题目
发送post请求前台无法获取数据
2022-08-10日报: Swin Transformer作者曹越加入智源,开展视觉基础模型研究
[Gazebo Introductory Tutorial] Lecture 3 Static/Dynamic Programming Modeling of SDF Files
从洞察到决策,一文解读标签画像体系建设方法论
C#实现访问OPC UA服务器