老莫的笔记本  
  
查看: 1137|回复: 1

MYSQL 临时表与复制表

[复制链接]

662

主题

878

帖子

5141

积分

超级版主

Rank: 8Rank: 8

积分
5141
发表于 2018-7-27 23:38:40 | 显示全部楼层 |阅读模式

临时表:   创建后只在当前连接可见,当关闭时 mysql 会自动删除该表。
先BB下临时表的特点: 当使用PHP 来创建MYSQL 临时表,PHP脚本执行完后,临时表会自动销毁。如果是客户端连接 ,则关闭客户端时候关闭临时表。
  1. mysql> CREATE TEMPORARY TABLE SalesSummary (
  2.     -> product_name VARCHAR(50) NOT NULL
  3.     -> , total_sales DECIMAL(12,2) NOT NULL DEFAULT 0.00
  4.     -> , avg_unit_price DECIMAL(7,2) NOT NULL DEFAULT 0.00
  5.     -> , total_units_sold INT UNSIGNED NOT NULL DEFAULT 0
  6. );
  7. Query OK, 0 rows affected (0.00 sec)
  8. mysql> INSERT INTO SalesSummary
  9.     -> (product_name, total_sales, avg_unit_price, total_units_sold)
  10.     -> VALUES
  11.     -> ('cucumber', 100.25, 90, 2);
  12. mysql> SELECT * FROM SalesSummary;
  13. +--------------+-------------+----------------+------------------+
  14. | product_name | total_sales | avg_unit_price | total_units_sold |
  15. +--------------+-------------+----------------+------------------+
  16. | cucumber     |      100.25 |          90.00 |                2 |
  17. +--------------+-------------+----------------+------------------+
  18. 1 row in set (0.00 sec)
复制代码
当使用show tables 命令显示数据表是们无法看到 临时表,

手动删除临时表
  1. mysql> CREATE TEMPORARY TABLE SalesSummary (
  2.     -> product_name VARCHAR(50) NOT NULL
  3.     -> , total_sales DECIMAL(12,2) NOT NULL DEFAULT 0.00
  4.     -> , avg_unit_price DECIMAL(7,2) NOT NULL DEFAULT 0.00
  5.     -> , total_units_sold INT UNSIGNED NOT NULL DEFAULT 0
  6. );
  7. Query OK, 0 rows affected (0.00 sec)
  8. mysql> INSERT INTO SalesSummary
  9.     -> (product_name, total_sales, avg_unit_price, total_units_sold)
  10.     -> VALUES
  11.     -> ('cucumber', 100.25, 90, 2);
  12. mysql> SELECT * FROM SalesSummary;
  13. +--------------+-------------+----------------+------------------+
  14. | product_name | total_sales | avg_unit_price | total_units_sold |
  15. +--------------+-------------+----------------+------------------+
  16. | cucumber     |      100.25 |          90.00 |                2 |
  17. +--------------+-------------+----------------+------------------+
  18. 1 row in set (0.00 sec)
  19. mysql> DROP TABLE SalesSummary;
  20. mysql>  SELECT * FROM SalesSummary;
  21. ERROR 1146: Table 'RUNOOB.SalesSummary' doesn't exist
复制代码



回复

使用道具 举报

662

主题

878

帖子

5141

积分

超级版主

Rank: 8Rank: 8

积分
5141
 楼主| 发表于 2018-7-27 23:43:44 | 显示全部楼层
MYSQL 复制表的两种方式:

1.只复制表结构到新表;
create table 新表 select * from 旧表 where 1=2
或者
CREATE TABLE 新表 LIKE 旧表

2.复制表结构及数据到新表:
creat table 新表 select * from 旧表

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表