当前位置:网站首页>学习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
边栏推荐
- 【ECCV 2022|Millions of Prizes】PSG Competition: Pursuing the "Most Comprehensive" Scene Understanding
- How does vue clear the tab switching cache problem?
- Summary of tensorflow installation stepping on the pit
- 第三方软件测评有什么作用?权威软件检测机构推荐
- 注意力模型---Attention Model
- laravel 抛错给钉钉
- antd组件中a-modal设置固定高度,内容滚动显示
- 重要通知 | “移动云杯”算力网络应用创新大赛初赛延期!!
- 电脑重装系统提示activex部件不能创建对象如何解决
- 发送post请求前台无法获取数据
猜你喜欢
IT小白怎么系统的php学习
Flask框架——MongoEngine使用MongoDB数据库
PyTorch 多机多卡训练:DDP 实战与技巧
Stream通过findFirst()查找满足条件的一条数据
[target detection] small script: extract training set images and labels and update the index
使用mysq语句操作数据库
List集合
1W word detailed thread local storage ThreadLocal
[JS Advanced] Creating sub-objects and replacing this_10 in ES5 standard specification
laravel throws the error to Dingding
随机推荐
2012年下半年 系统架构设计师 下午试卷 II
2022年五大云虚拟化趋势
PEST 分析法
laravel 抛错给钉钉
写不完的数学试卷-----试卷生成器(Qt含源码)
字节终面:CPU 是如何读写内存的?
这一次,话筒给你:向自由软件之父斯托曼 提问啦!
vue 怎么清除tab 切换缓存问题 ?
win2012安装Oraclerac失败
R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:gtExtras包的gt_highlight_rows函数高亮(highlight)表格中特定的数据行、配置高亮行的特定数据列数据加粗
Borg Maze (bfs+最小生成树)
高薪程序员&面试题精讲系列135之你对分布式是怎么理解的?CAP理论你知道吗?
How does vue clear the tab switching cache problem?
力扣解法汇总640-求解方程
Summary of tensorflow installation stepping on the pit
sql语句 异常 Err] 1064 – You have an error in your SQL syntax; check the manual that corresponds to your
学习日记8
【POI 2008, BLO】割点
安装mysql报错处理
vivado闪退或者message无显示