Mysql临时表

MySQL临时表

  MySQL临时表在我们需要保存一些临时数据时是非常有用的。临时表只在当前连接可见,当关闭连接时,MySQL会自动删除表并释放所有空间。在MySQL中,临时表是一种在当前会话中存在的表,它在会话结束时会自动被销毁。

创建临时表
create temporary table temp_table (
	id int primary key,
	name varchar(32) default null


);

或者简写为

create temporary table temp_table as select column1,column2 from source_table where condition;

插入数据到临时表
insert into temp_table(column1, column2) values(value1, value2);
查询临时表
select * from temp_table;
修改临时表

临时表的修改操作与普通表类似

alter table temp_table add column new_column datatype;
删除临时表

临时表会在会话结束时自动被销毁,但是也可以使用drop table明确删除它。

drop temporary table if exists temp_table;
或者
drop table temp_table;

临时表对于需要在某个会话中存储中间结果集或进行复杂查询时非常有用。临时表的作用范围仅限于创建它的会话,其它会话无法直接访问或引用该临时表,在多个会话之间共享数据时,可以考虑使用普通表而不是临时表。
临时表会在会话结束时被删除,但也可以使用drop命令删除它,这样可以更早地释放资源。

用查询的方式直接创建临时表
create temporary table temp_table as (
select * from source_table limit 0, 50
);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值