当前位置:网站首页>MySQL复制表结构、表数据的方法
MySQL复制表结构、表数据的方法
2022-07-30 19:30:00 【asdfadafd】
1、复制表结构及数据到新表
create table new_table_name select * from old_table_name;
这种方法的一个最不好的地方就是新表中没有了旧表的primary key、Extra(auto_increment)等属性。
2、只复制表结构到新表
create table new_table_name select * from old_table_name where 1=2;
或
create table new_table_name like old_table_name;
3、复制旧表的数据到新表(假设两个表结构一样)
insert into new_table_name select * from old_table_name;
4、复制旧表的数据到新表(假设两个表结构不一样)
insert into new_table_name(字段1,字段2,字段3) select (字段1,字段2,字段3) from old_table_name;
5、表不在同一数据库中(如:db1 table1,db2 table2)
完全复制
insert into db1.table1 select * from db2.table2;
不复制重复记录
insert into db1.table1 select distinct* from db2.table2;
复制前10条记录
insert into db1.table1 select 10* from db2.table2;
6、查看表的创建SQL
show create table table_name;
这样会将表的创建SQL列出。我们只需要将该SQL拷贝出来,更改table_name,就可以创建一个完全一样的表。
7、清除表数据
delete from table_name;
或
truncate table table_name;
不带where参数的delete语句可以删除mysql表中所有内容;
使用truncate table也可以清空mysql表中所有内容。
但是使用delete清空表中的记录,内容的ID仍然从删除点的ID继续建立,而不是从1开始。
而truncate相当于保留了表的结构而重新建立了一张同样的新表。
效率上truncate比delete快。
但truncate删除后不记录mysql日志,不可以恢复数据。
delete的效果有点像将mysql表中所有记录一条一条删除到删完。
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- MindSpore:【模型训练】【mindinsight】timeline的时间和实际用时相差很远
- VBA 运行时错误‘-2147217900(80040e14):自动化(Automation)错误
- Range.CopyFromRecordset 方法 (Excel)
- 跨进程启动后台服务
- The advanced version of the cattle brushing series (search for rotating sorted arrays, inversion of the specified range in the linked list)
- Witness the magical awakening of the mini world in HUAWEI CLOUD
- Alibaba Cloud Martial Arts Headline Event Sharing
- 055 c# print
- What is the difference between a cloud database and an on-premises database?
- VS Code connects to SQL Server
猜你喜欢
Zabbix 5.0 Monitoring Tutorial (1)
What is the difference between a cloud database and an on-premises database?
MySQL sub-database sub-table
MindSpore:【MindSpore1.1】Mindspore安装后验证出现cudaSetDevice failed错误
VBA batch import Excel data into Access database
2种手绘风格效果比较,你更喜欢哪一种呢?
NXP IMX8QXP replacement DDR model operation process
JS提升:Promise中reject与then之间的关系
MySQL数据库主从配置
MindSpore:【模型训练】【mindinsight】timeline的时间和实际用时相差很远
随机推荐
牛客刷题系列之进阶版(搜索旋转排序数组,链表内指定区间反转)
MySQL database - DQL data query language
WEBSOCKETPP使用简介+demo
Niuke.com - Huawei Question Bank (100~108)
什么是 RESTful API?
青蛙跳台阶(递归和非递归)-------小乐乐走台阶
【hbuilder】运行不了部分项目 , 打开终端 无法输入指令
centos7安装mysql8
跨进程启动后台服务
ImportError: attempted relative import with no known parent package
Linux下安装Mysql5.7,超详细完整教程,以及云mysql连接
Swiper rotates pictures and plays background music
ResNet18-实现图像分类
MindSpore:【JupyterLab】查看数据时报错
Entering the applet for the first time
开心的聚餐
vxe-table实现复选框鼠标拖动选中
来了!东方甄选为龙江农产品直播带货
HCIP --- 企业网的三层架构
MindSpore:对image作normalize的目的是什么?