vlambda博客
学习文章列表

MySQL基础---今天我们讲下MySQL中的临时表

  很久没有继续学习MySQL了,之前学习索引没有学透彻,还有很多遗留的点没有去做实例。先不管索引了,我们来学习下MySQL中的临时表。

  还是老样子,先学习临时表的定义和用法:

 MySQL临时表在我们需要保存一些临时数据时是非常有用的.
临时表只在当前连接可见,当连接关闭时,MySQL会自动删除表并释放所有空间.
如果使用PHP脚本创建MySQL临时表,当PHP脚本执行完成后,该临时表也会自动销毁.
如果使用了其他MySQL客户端程序连接MySQL数据库服务器来创建临时表,只有在关闭客户端程序时才会销毁临时表.当然也可以手动销毁。


下面,我们看下实例吧

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| Hello              |
| World              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.04 sec)

可以看出数据库Hello中只有一个数据库表 cool

mysql> show tables from Hello;
+-----------------+
| Tables_in_hello |
+-----------------+
| cool            |
+-----------------+
1 row in set (0.00 sec)


mysql> #让我们来创建一个临时表
mysql> create temporary table world(
    -> product_name varchar(50) not null,
    -> total_sales decimal(12,2) not null default 0.00,
    -> avg_unit decimal(7,2) not null default 0.00,
    -> total_units int unsigned not null default 0
    -> );
Query OK, 0 rows affected (0.03 sec)

临时表创建成功了~~~

mysql> #临时表已经创建好了
mysql> show tables from Hello;
+-----------------+
| Tables_in_hello |
+-----------------+
| cool            |
+-----------------+
1 row in set (0.00 sec)

额,创建了但是使用显示数据库中的数据库表,发现没有world表

mysql> insert into world (product_name,total_sales,avg_unit,total_units) values ('hello',100.25,90,2);
Query OK, 1 row affected (0.02 sec)

我们插些数据再查询下试试

mysql> show tables from Hello;                                                  +-----------------+
| Tables_in_hello |
+-----------------+
| cool            |
+-----------------+
1 row in set (0.00 sec)

还是只有cool,说明临时表没有显示在列表里

mysql> select * from world;
+--------------+-------------+----------+-------------+
| product_name | total_sales | avg_unit | total_units |
+--------------+-------------+----------+-------------+
| hello        |      100.25 |    90.00 |           2 |
+--------------+-------------+----------+-------------+
1 row in set (0.01 sec)

那我们就直接查询临时表的内容吧,是可以查看的


那么当我们关闭这个数据库的连接时,查看是否还会显示临时表数据

mysql> quit;
Bye

mysql> show tables;
+-----------------+
| Tables_in_hello |
+-----------------+
| cool            |
+-----------------+
1 row in set (0.00 sec)
#我们再查看下world数据库表
mysql> select * from world;
ERROR 1146 (42S02): Table 'hello.world' doesn't exist

数据库提示不存在,说明是正确的。


删除MySQL临时表,默认情况下,当你断开与数据库的连接后,临时表就会自动被销毁.也可以在当前MySQL会话中使用 drop table table_name 来删除临时表.

我们来看看实例:

mysql> create temporary table world( product_name varchar(20) not null, total_sales decimal(12,2) not null default 0.00 );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into world (product_name,total_sales) values ('heihei',99.8);
Query OK, 1 row affected (0.01 sec)

查询下这个临时表:

mysql> select * from world;
+--------------+-------------+
| product_name | total_sales |
+--------------+-------------+
| heihei       |       99.80 |
+--------------+-------------+
1 row in set (0.00 sec)


mysql> #删除临时表world
mysql> drop table world;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from world;
ERROR 1146 (42S02): Table 'hello.world' doesn't exist

发现已经查询不到临时表 world了


这里大家需要记住,创建临时表为 create temporary table table_name


好了,今天就到这里吧