[关闭]
@yanglt7 2018-06-06T06:47:17.000000Z 字数 2852 阅读 914

11 MySQL 临时表、复制表

MySQL


临时表

MySQL 临时表在我们需要保存一些临时数据时是非常有用的。临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间。

实例

  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. mysql> insert into SalesSummary
  8. -> (product_name,total_sales,avg_unit_price,total_units_sold)
  9. -> values
  10. -> ('cucumber',100.25,90,2);
  11. mysql> select * from SalesSummary;
  12. +--------------+-------------+----------------+------------------+
  13. | product_name | total_sales | avg_unit_price | total_units_sold |
  14. +--------------+-------------+----------------+------------------+
  15. | cucumber | 100.25 | 90.00 | 2 |
  16. +--------------+-------------+----------------+------------------+
  17. mysql> show columns from SalesSummary;
  18. +------------------+------------------+------+-----+---------+-------+
  19. | Field | Type | Null | Key | Default | Extra |
  20. +------------------+------------------+------+-----+---------+-------+
  21. | product_name | varchar(50) | NO | | NULL | |
  22. | total_sales | decimal(12,2) | NO | | 0.00 | |
  23. | avg_unit_price | decimal(7,2) | NO | | 0.00 | |
  24. | total_units_sold | int(10) unsigned | NO | | 0 | |
  25. +------------------+------------------+------+-----+---------+-------+

当你使用 SHOW TABLES命令显示数据表列表时,你将无法看到 SalesSummary表。
如果你退出当前MySQL会话,再使用 SELECT命令来读取原先创建的临时表数据,那你会发现数据库中没有该表的存在,因为在你退出时该临时表已经被销毁了。

删除MySQL 临时表

默认情况下,当你断开与数据库的连接后,临时表就会自动被销毁。当然你也可以在当前MySQL会话使用 DROP TABLE 命令来手动删除临时表。

  1. mysql> drop table SalesSummary;
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> select * from SalesSummary;
  4. ERROR 1146 (42S02): Table 'test.salessummary' doesn't exist

复制表

如果我们需要完全的复制MySQL的数据表,包括表的结构,索引,默认值等。 如果仅仅使用CREATE TABLE ... SELECT 命令,是无法实现的。

完整的复制MySQL数据表,步骤如下:

实例

尝试以下实例来复制表 runoob_tbl 。
步骤一:
获取数据表的完整结构。

  1. mysql> show create table runoob_tbl \G;
  2. *************************** 1. row ***************************
  3. Table: runoob_tbl
  4. Create Table: CREATE TABLE `runoob_tbl` (
  5. `runoob_id` int(11) NOT NULL AUTO_INCREMENT,
  6. `runoob_title` varchar(100) NOT NULL,
  7. `runoob_author` varchar(40) NOT NULL,
  8. `submission_date` date DEFAULT NULL,
  9. PRIMARY KEY (`runoob_id`)
  10. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
  11. 1 row in set (0.00 sec)
  12. ERROR:
  13. No query specified

步骤二:
修改SQL语句的数据表名,并执行SQL语句。

  1. mysql> CREATE TABLE `clone_tbl` (
  2. -> `runoob_id` int(11) NOT NULL auto_increment,
  3. -> `runoob_title` varchar(100) NOT NULL default '',
  4. -> `runoob_author` varchar(40) NOT NULL default '',
  5. -> `submission_date` date default NULL,
  6. -> PRIMARY KEY (`runoob_id`),
  7. -> UNIQUE KEY `AUTHOR_INDEX` (`runoob_author`)
  8. -> ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

步骤三:
执行完第二步骤后,你将在数据库中创建新的克隆表 clone_tbl。 如果你想拷贝数据表的数据你可以使用 INSERT INTO... SELECT 语句来实现。

  1. mysql> insert into clone_tbl (runoob_id,runoob_title,runoob_author,submission_date)
  2. -> select runoob_id,runoob_title,runoob_author,submission_date from runoob_tbl;
  3. Query OK, 5 rows affected (0.01 sec)
  4. Records: 5 Duplicates: 0 Warnings: 0

执行以上步骤后,你将完整的复制表,包括表结构及表数据。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注