@yanglt7
2018-06-06T06:47:17.000000Z
字数 2852
阅读 914
MySQL
MySQL 临时表在我们需要保存一些临时数据时是非常有用的。临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间。
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
-> );
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 |
+--------------+-------------+----------------+------------------+
mysql> show columns from SalesSummary;
+------------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| product_name | varchar(50) | NO | | NULL | |
| total_sales | decimal(12,2) | NO | | 0.00 | |
| avg_unit_price | decimal(7,2) | NO | | 0.00 | |
| total_units_sold | int(10) unsigned | NO | | 0 | |
+------------------+------------------+------+-----+---------+-------+
当你使用 SHOW TABLES命令显示数据表列表时,你将无法看到 SalesSummary表。
如果你退出当前MySQL会话,再使用 SELECT命令来读取原先创建的临时表数据,那你会发现数据库中没有该表的存在,因为在你退出时该临时表已经被销毁了。
默认情况下,当你断开与数据库的连接后,临时表就会自动被销毁。当然你也可以在当前MySQL会话使用 DROP TABLE 命令来手动删除临时表。
mysql> drop table SalesSummary;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from SalesSummary;
ERROR 1146 (42S02): Table 'test.salessummary' doesn't exist
如果我们需要完全的复制MySQL的数据表,包括表的结构,索引,默认值等。 如果仅仅使用CREATE TABLE ... SELECT 命令,是无法实现的。
完整的复制MySQL数据表,步骤如下:
尝试以下实例来复制表 runoob_tbl 。
步骤一:
获取数据表的完整结构。
mysql> show create table runoob_tbl \G;
*************************** 1. row ***************************
Table: runoob_tbl
Create Table: CREATE TABLE `runoob_tbl` (
`runoob_id` int(11) NOT NULL AUTO_INCREMENT,
`runoob_title` varchar(100) NOT NULL,
`runoob_author` varchar(40) NOT NULL,
`submission_date` date DEFAULT NULL,
PRIMARY KEY (`runoob_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
ERROR:
No query specified
步骤二:
修改SQL语句的数据表名,并执行SQL语句。
mysql> CREATE TABLE `clone_tbl` (
-> `runoob_id` int(11) NOT NULL auto_increment,
-> `runoob_title` varchar(100) NOT NULL default '',
-> `runoob_author` varchar(40) NOT NULL default '',
-> `submission_date` date default NULL,
-> PRIMARY KEY (`runoob_id`),
-> UNIQUE KEY `AUTHOR_INDEX` (`runoob_author`)
-> ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
步骤三:
执行完第二步骤后,你将在数据库中创建新的克隆表 clone_tbl。 如果你想拷贝数据表的数据你可以使用 INSERT INTO... SELECT 语句来实现。
mysql> insert into clone_tbl (runoob_id,runoob_title,runoob_author,submission_date)
-> select runoob_id,runoob_title,runoob_author,submission_date from runoob_tbl;
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
执行以上步骤后,你将完整的复制表,包括表结构及表数据。