当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL中关于临时表的一些基本使用方法

MySQL中关于临时表的一些基本使用方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

我想更懂你伴奏,3d解太湖大全neiba,欧诗漫化妆品怎么样

临时表可能是非常有用的,在某些情况下,保持临时数据。最重要的是应该知道的临时表是,他们将当前的客户端会话终止时被删除。

临时表中添加mysql版本3.23。如果您使用的是旧版本的mysql比3.23,可以不使用临时表,但可以使用堆表。

如前所述临时表将只持续只要的会话是存在的。如果运行一个php脚本中的代码,该临时表将被销毁时,会自动执行完脚本后。如果已连接到mysql数据库的服务器上,通过mysql的客户端程序的临时表将一直存在,直到关闭客户端或手动破坏的表。
实例

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

mysql> create temporary table salessummary (
  -> product_name varchar(50) not null
  -> , total_sales decimal(12,2) not null default 0.00
  -> , avg_unit_price decimal(7,2) not null default 0.00
  -> , total_units_sold int unsigned not null default 0
);
query ok, 0 rows affected (0.00 sec)

mysql> insert into salessummary
  -> (product_name, total_sales, avg_unit_price, total_units_sold)
  -> values
  -> ('cucumber', 100.25, 90, 2);

mysql> select * from salessummary;
+--------------+-------------+----------------+------------------+
| product_name | total_sales | avg_unit_price | total_units_sold |
+--------------+-------------+----------------+------------------+
| cucumber   |   100.25 |     90.00 |        2 |
+--------------+-------------+----------------+------------------+
1 row in set (0.00 sec)

当发出一个show tables命令,那么临时表将不会被列在列表中。现在如果将mysql的会话的注销,那么会发出select命令,那么会发现没有在数据库中的数据。即使临时表也就不存在了。
删除临时表:

默认情况下,所有的临时表被删除时,mysql的数据库连接被终止。不过要删除他们之前就应该发出drop table命令。

下面的例子为删除一个临时表。

mysql> create temporary table salessummary (
  -> product_name varchar(50) not null
  -> , total_sales decimal(12,2) not null default 0.00
  -> , avg_unit_price decimal(7,2) not null default 0.00
  -> , total_units_sold int unsigned not null default 0
);
query ok, 0 rows affected (0.00 sec)

mysql> insert into salessummary
  -> (product_name, total_sales, avg_unit_price, total_units_sold)
  -> values
  -> ('cucumber', 100.25, 90, 2);

mysql> select * from salessummary;
+--------------+-------------+----------------+------------------+
| product_name | total_sales | avg_unit_price | total_units_sold |
+--------------+-------------+----------------+------------------+
| cucumber   |   100.25 |     90.00 |        2 |
+--------------+-------------+----------------+------------------+
1 row in set (0.00 sec)
mysql> drop table salessummary;
mysql> select * from salessummary;
error 1146: table 'tutorials.salessummary' doesn't exist


如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网