临时表在某些情况下是非常有用的,用于保存临时数据。最重要的是要知道临时表是在当前客户端会话终止时被删除的。

在临时表中添加MySQL版本3.23。如果您使用的是MySQL比率为3.23的旧版本,则不能使用临时表,但可以使用堆表。

临时表,如前所述,将只在会话存在持续下去。如果一个PHP脚本代码运行和临时表将被破坏,该脚本将自动执行。如果服务器连接到MySQL数据库,通过MySQL客户端程序的临时表将永远存在直到客户端或手动销毁表关闭。

例子

下面的例子是一个临时表在PHP脚本的使用,使用mysql_query()函数,可以使用相同的代码。


MySQL >创建临时表salessummary(
product_name,varchar(50)不为空
-> total_sales,小数(12,2)不为空,默认为0
-> avg_unit_price,小数(7,2)不为空,默认为0
total_units_sold int unsigned不空>,默认为0
);
查询OK,0行受影响(0秒)

MySQL >插入salessummary
(product_name,total_sales,avg_unit_price,和total_units_sold)
->值
->('cucumber',100.25,90, 2);

MySQL SELECT * FROM salessummary >;
+ + + + -------------- ------------- ---------------- ------------------ +
product_name total_sales avg_unit_price | | | | total_units_sold |
+ + + + -------------- ------------- ---------------- ------------------ +
|黄瓜| 100.25 | 90 | 2 |
+ + + + -------------- ------------- ---------------- ------------------ +
1行集(0秒)




当发布一个显示表命令时,临时表将不会被列在列表中。现在,如果MySQL会话被注销,则将发出select命令,数据库中将没有数据。甚至临时表也不存在。

删除临时表:

默认情况下,当删除所有临时表时,MySQL数据库连接将被终止。但是在删除它们之前,应该发布下拉表命令。

下面的示例是删除临时表。


MySQL >创建临时表salessummary(
product_name,varchar(50)不为空
-> total_sales,小数(12,2)不为空,默认为0
-> avg_unit_price,小数(7,2)不为空,默认为0
total_units_sold int unsigned不空>,默认为0
);
查询OK,0行受影响(0秒)

MySQL >插入salessummary
(product_name,total_sales,avg_unit_price,和total_units_sold)
->值
->('cucumber',100.25,90, 2);

MySQL SELECT * FROM salessummary >;
+ + + + -------------- ------------- ---------------- ------------------ +
product_name total_sales avg_unit_price | | | | total_units_sold |
+ + + + -------------- ------------- ---------------- ------------------ +
|黄瓜| 100.25 | 90 | 2 |
+ + + + -------------- ------------- ---------------- ------------------ +
1行集(0秒)
MySQL >删除表salessummary;
MySQL SELECT * FROM salessummary >;
错误1146:table'tutorials salessummary'doesn不存在。