mysql创建临时表
create temporary tem_name(id int,name varchar(20));
- 临时表的存在的周期是当前登录的session有效,断开连接将会被立即删除,也可手动将其drop table
- 临时表使用有一些限制条件:
(1)临时表在memory、myisam、merge或者innodb上使用,并且不支持mysql cluster簇;
show tables;
语句不会列出临时表,在 information_schema 中也不存在临时表信息
show create table;
可以查看临时表
(2)不能使用rename来重命名临时表。但是可以alter table rename 代替:
alter table tem_name rename tem_name_new;
(3)可以复制临时表得到一个新的临时表,如:
create temporary table tem_name_new1 select * from tem_name_new;
(4)但在同一个query语句中,相同的临时表只能出现一次。如:
可以使用: select * from temp_tb;
但不能使用: select * from temp_tb, temp_tb as t;
(5)但不同的临时表可以出现在同一个query语句中,如临时表temp_tb1, temp_tb2:
select * from temp_tb1, temp_tb2;
- 临时表可以手动删除:
drop temporary table if exists temp_tb;
4.临时表主要用于对大数据量的表上作一个子集,提高查询效率。在创建临时表时声明类型为HEAP,则Mysql会在内存中创建该临时表,即内存表
create temporary table tem_tb_test1 type='heap'
select * from temp_tb;
5.临时表和内存表的ENGINE 不同,临时表默认的是Mysql指定的默认Engine,而内存表是MEMORY