当前位置:网站首页>学习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边栏推荐
猜你喜欢

正则表达式(包含各种括号,echo,正则三剑客以及各种正则工具)

系统架构系列文章三--解决传统企业核心系统的性能问题

SenseTime self-developed robotic arm, the first product is an AI chess-playing robot: Guo Jingjing is also invited as an endorsement

发送post请求前台无法获取数据

Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!

1W word detailed thread local storage ThreadLocal

Using data intelligence, Amazon cloud technology helps companies build endogenous brand growth

缺少比较器,运放来救场!(运放当做比较器电路记录)
Existing in the rain of PFAS chemical poses a threat to the safety of drinking water

2022年中国软饮料市场洞察
随机推荐
2022-08-10日报: Swin Transformer作者曹越加入智源,开展视觉基础模型研究
正则表达式(包含各种括号,echo,正则三剑客以及各种正则工具)
leetcode 739. Daily Temperatures Daily Temperatures (Moderate)
tampercfg内核模块导致机器频繁crash
Pointer (preliminary solution of C language)
镜像瘦身:每一层都不能放过
串口服务器调试助手使用教程,串口调试助手使用教程【操作方式】
池化技术有多牛?来,告诉你阿里的Druid为啥如此牛逼!
SenseTime self-developed robotic arm, the first product is an AI chess-playing robot: Guo Jingjing is also invited as an endorsement
安装mysql报错处理
网络安全——XSS之被我们忽视的Cookie
微信扫码登陆(1)—扫码登录流程讲解、获取授权登陆二维码
numpy.meshgrid()理解
MySQL - storage engine for databases
tensorflow安装踩坑总结
“国资云”和“国家云”能给市场带来怎样的变革?
PHP judges whether the file has content, and if there is no content, copy another file to write
写不完的数学试卷-----试卷生成器(Qt含源码)
1W word detailed thread local storage ThreadLocal
SQL学习(基础)