复制表的几种方式
只复制表结构create table tablename like sometable;
只复制表结构,包括主键和索引,但是不会复制表数据
只复制表数据create table tablename select * from sometable;
复制表的大体结构及全部数据,不会复制主键、索引等
完整复制create table tablename like sometable;
insert into tablename select * from sometable;
分两步完成,先复制表结构,再插入数据
相关免费学习推荐:mysql视频教程
例子
连接数据库,使用student数据库并查看所有数据表use student;show tables;
效果图:
2. 创建t1数据表,插入两条记录并为name字段设置索引
create table t1( id int not null auto_increment primary key, name varchar(50) comment '姓名');insert into t1(name) values('张三');insert into t1(name) values('李四');create index idx_name on t1(name);
效果图:
3. 查看t1数据表的所有记录
select * from t1;
效果图:
4. 查看t1数据表的索引
show index from t1\g;
效果图:
5. 创建t2数据表(只复制表结构)
create table t2 like t1;
效果图:
6. 查看t2数据表的记录
select * from t2;
效果图:
7. 查看t2数据表的索引
show index from t2\g;
效果图:
8. 查看t2数据表的结构
show create table t2\g;
效果图:
9. 创建t3数据表(只复制表数据)
create table t3 select * from t1;
效果图:
10. 查看t3数据表的表结构
show create table t3\g;
效果图:
11. 查看t3数据表的索引
show index from t3;
效果图:
12. 查看t3数据表的所有记录
select * from t3;
效果图:
13. 创建t4数据表(完整复制)
create table t4 like t1;insert into t4 select * from t1;
效果图:
14. 查看t4数据表的结构
show create table t4\g;
效果图:
15. 查看t4数据表的索引
show index from t4\g;
效果图:
16. 查看t4数据表的所有记录
select * from t4;
效果图:
注:本文是博主mysql学习的总结,不支持任何商用,转载请注明出处!如果你也对mysql学习有一定的兴趣和理解,欢迎随时找博主交流~
相关免费学习推荐:mysql数据库(视频)
以上就是介绍mysql复制表的几种方式的详细内容。