46. SQL--临时表的使用
2022/9/6 2:22:49
本文主要是介绍46. SQL--临时表的使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1. 前言
关系型数据库支持临时表,这是一项很棒的功能。临时表的概念由 sql server 引入,用来存储和处理中间结果。
临时表在会话期间创建,会话结束后自动被删除。临时表可以和普通表一样执行各种操作,比如 select、update、insert、join 等。
mysql 3.23 及其更高版本才支持临时表,如果您使用的 mysql 版本低于 3.23,则不能使用临时表,但可以使用堆表(heap table)。
如前所述,临时表仅存在于会话期间。如果您使用 php 脚本连接数据库,当 php 执行完成后,临时表将被销毁。如果您使用 mysql 客户端连接数据库,当客户端关闭后,临时表将被销毁。
创建临时表的基本语法如下:
create temporary table table_name( column1 datatype, column2 datatype, column3 datatype, ..... columnn datatype, primary key( one or more columns ) );
您看,创建临时表的语法和普通表极其相似。临时表创建完成以后,可以使用 insert、delete、update、select 等命令进行增删改查操作。
2. 示例
本例向您展示临时表的用法:
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 命令查看数据库中的表时,临时表将不会被显示。
现在,如果您退出 mysql 会话,然后使用 select 命令查找数据,您将在数据库中找不到任何有效数据,甚至连临时表也不存在。
3. 删除临时表
默认情况下,当数据库连接终止时,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 'tutorials.salessummary' doesn't exist
这篇关于46. SQL--临时表的使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?
- 2024-05-09企业src漏洞挖掘-有意思的命令执行