zoukankan      html  css  js  c++  java
  • MYSQL- 创建和删除临时表

      临时表可能是非常有用的,在某些情况下,保持临时数据。最重要的是应该知道的临时表是,他们将当前的客户(www.111cn.net)端会话终止时被删除
      当你创建临时表的时候,你可以使用temporary关键字。如:

    create temporary table tmp_table(name varchar(10) not null,passwd char(6) not null)

      临时表只在当前连接可见,当这个连接关闭的时候,会自动drop。这就意味着你可以在两个不同的连接里使用相同的临时表名,并且相互不会冲突,或者使用 已经存在的表,但不是临时表的表名。(当这个临时表存在的时候,存在的表被隐藏了,如果临时表被drop,存在的表就可见了)。创建临时表你必须有create temporary table 权限。

      临时表用完后要记得drop掉:

    DROP TEMPORARY TABLE IF EXISTS sp_output_tmp;

      

      创建临时表

      实例 下面是一个例子,使用临时表在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)

      删除临时表:
      默认情况下,所有的临时表被删除时,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
  • 相关阅读:
    微信扫码支付模式一和模式二的区别
    spring boot MongoDB的集成和使用
    Java 8 中的 Streams API 详解
    大并发量的订单的解析
    Springboot项目打成war包,部署到tomcat上,正常启动访问报错404
    最详细的虚拟机安装centos7教程
    nginx限制上传大小和超时时间设置说明/php限制上传大小
    一级域名和二级域名的区别是什么?分别有什么作用?
    快递物流查询接口介绍
    java 使用volatile实现线程数据的共享
  • 原文地址:https://www.cnblogs.com/cxeye/p/4380814.html
Copyright © 2011-2022 走看看