MySql 的临时表

当工作在非常大的表上时,你可能偶尔需要运行很多查询获得一个大量数据的小的子集,不是对整个表运行这些查询,而是让MySQL每次找出所需的少数记录,将记录选择到一个临时表可能更快些,然后对这个临时表运行查询。

创建临时表很容易,给正常的CREATE TABLE语句加上TEMPORARY关键字:

CREATE TEMPORARY TABLE tmp_table (
name VARCHAR(10) NOT NULL,
value INTEGER NOT NULL
)
++++++++++++++++++++++++++++++++++++++
mysql> select database();
+------------+
| database() |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec)

mysql> create temporary table tmp_table(
    -> name varchar(10) not null,
    -> value integer no null
    -> );
ERROR 1046 (3D000): No database selected
mysql>
-------------------------------没有在数据上操作,你们家切菜都是悬空不用砧板的么
mysql> create temporary table tmp_table(
    -> name varchar(10) not null,
    -> value integer no null   -----------------------〉敲错了字符
    -> );
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'no nu
ll
)' at line 3
mysql> create temporary table tmp_table(
    -> name varchar(10) not null,
    -> value integer not null
    -> );
Query OK, 0 rows affected (0.05 sec)

++++++++++++++++++++++++++++++++++++++

临时表将在你连接MySQL期间存在。当你断开时,MySQL将自动删除表并释放所用的空间。当然你可以在仍然连接的时候删除表并释放空间。
DROP TABLE tmp_table

mysql> select * from tmp_table;---------------〉感觉这个临时表在School真实库里,没有temp_db? 不像sql server 需要切换到temp_db
Empty set (0.00 sec)

mysql> drop table tmp_table;
Query OK, 0 rows affected (0.13 sec)
如果在你创建名为tmp_table临时表时名为tmp_table的表在数据库中已经存在,临时表将有必要屏蔽(隐藏)非临时表tmp_table

++++++++++++++++++++++++++++++++++++++++++

posted @ 2008-12-05 00:26  shuang  阅读(338)  评论(0)    收藏  举报