当前位置:网站首页>学习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
边栏推荐
- 第五讲 测试技术与用例设计
- 司空见惯 - 股市狠狠下跌后,何時能反弹?
- 【JS高级】ES5标准规范之创建子对象以及替换this_10
- Lithium battery technology
- SenseTime self-developed robotic arm, the first product is an AI chess-playing robot: Guo Jingjing is also invited as an endorsement
- Send a post request at the front desk can't get the data
- 指针(C语言初解)
- Circle 创始人回应美财政部禁止 Tornado :隐私与安全之间关系紧张
- 这一次,话筒给你:向自由软件之父斯托曼 提问啦!
- FPN详解
猜你喜欢
Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
日志@Slf4j介绍使用及配置等级
实现一个深克隆
注意力模型---Attention Model
Open source SPL wipes out tens of thousands of database intermediate tables
八大排序总是忘?快来这里~
“Oracle 封禁了我的账户”
写不完的数学试卷-----试卷生成器(Qt含源码)
WebView的优化与常见问题解决方案
等保2.0一个中心三重防护指的是什么?如何理解?
随机推荐
Matlab画分段函数「建议收藏」
ES5和SE6来实现一个Promise效果
Classifying irises using decision trees
文件系统设计
FPN详解
1W word detailed thread local storage ThreadLocal
2012年下半年 系统架构设计师 下午试卷 II
王学岗————直播推流(软便)03x264集成与camera推流
高数_证明_弧微分公式
面试面到了一个腾讯30k出来的,有见识到何为精通MySQL调优
【有限元分析】异型密封圈计算泄漏量与参数化优化过程(带分析源文件)
vivado闪退或者message无显示
公网IP和内网IP的区别[通俗易懂]
静态变量存储在哪个区
epoll学习:思考一种高性能的服务器处理框架
WebView的优化与常见问题解决方案
MQTT服务器搭建
Unfinished mathematics test paper ----- test paper generator (Qt includes source code)
Open source SPL wipes out tens of thousands of database intermediate tables
指针(C语言初解)