Fork me on GitHub

MySQL学习16_临时表和复制表

MySQL临时表

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

创建临时表

关键字是temporary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- 创建临时表
create temporary table user(
id int (10) unsigned not null auto_increment comment "用户id",
name varchar(30) not null comment "用户名",
email varchar(30) not null comment "邮箱",
age tinyint unsigned not null commment "年龄",
password varchar(30) not null comment "密码",
primary key(id) -- 创建自增主键
);

insert into user
(name, email, age, password)
values
('xiaoming', 123456@qq.com, 25, Password('123456');

当退出了当前的MySQL对话,再次使用select命令来读取临时表中的数据,发现表已经被销毁了。

删除临时表

关键字是drop

1
drop table user;

复制表

只复制表结构到新表

1
2
3
create table new_table select * from old_table where 1=2;  -- 不会复制时的主键类型和自增方式

create table new_tabel like old_table; -- 所有的字段类型复制到新表

复制表结构和数据到新表

1
create table new_table select * from old_table;

复制数据到新表

1
2
3
insert into new_tabel select * from old_table;    -- 两个表结构相同

insert into new_table(column1,.....) select column1, column2, ... from old_table; -- 表结构不同

本文标题:MySQL学习16_临时表和复制表

发布时间:2019年10月15日 - 14:10

原始链接:http://www.renpeter.cn/2019/10/15/MySQL%E5%AD%A6%E4%B9%A016_%E4%B8%B4%E6%97%B6%E8%A1%A8%E5%92%8C%E5%A4%8D%E5%88%B6%E8%A1%A8.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea