当前位置:网站首页>How to copy table structure and table data in MySQL
How to copy table structure and table data in MySQL
2022-07-30 19:40:00 【asdfadafd】
1, copy table structure and data to new table
create table new_table_name select * from old_table_name;
One of the worst aspects of this method is that the new table does not have the primary key, Extra (auto_increment) and other attributes of the old table.
2, only copy the table structure to the new table
create table new_table_name select * from old_table_name where 1=2;
or
create table new_table_name like old_table_name;
3. Copy the data of the old table to the new table (assuming the two tables have the same structure)
insert into new_table_name select * from old_table_name;
4. Copy the data from the old table to the new table (assuming the two tables have different structures)
insert into new_table_name(field1,field2,field3) select (field1,field2,field3) from old_table_name;
5. The tables are not in the same database (eg: db1 table1, db2 table2)
Full copy
insert into db1.table1 select * from db2.table2;
Do not copy duplicate records
insert into db1.table1 select distinct* from db2.table2;
Copy the first 10 records
insert into db1.table1 select 10* from db2.table2;
6. View the SQL created for the table
show create table table_name;
This will list the table's creation SQL.We just need to copy the SQL and change the table_name to create an identical table.
7. Clear table data
delete from table_name;
or
truncate table table_name;
The delete statement without the where parameter can delete all the contents in the mysql table;
Use truncate table to also clear all the contents in the mysql table.
But using delete to clear the records in the table, the ID of the content is still established from the ID of the deletion point instead of starting from 1.
And truncate is equivalent to preserving the structure of the table and re-establishing a new same table.
Truncate is faster than delete in efficiency.
But mysql log is not recorded after truncate is deleted, and data cannot be recovered.
The effect of delete is a bit like deleting all records in the mysql table one by one until the deletion is complete.
Let me introduce myself first. The editor graduated from Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Ali in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- VBA批量将Excel数据导入Access数据库
- MindSpore:对image作normalize的目的是什么?
- Linux下载安装mysql5.7版本教程最全详解
- Download Win11 how to change the default path?Download Win11 change the default path method
- 牛客网——华为题库(100~108)
- Golang logging library zerolog use record
- coming!Dongfang Selection brings goods to the live broadcast of Longjiang agricultural products
- 【MindSpore】多卡训练保存权重问题
- Google's AlphaFold claims to have predicted almost every protein structure on Earth
- SimpleOSS third-party library libcurl and engine libcurl error solution
猜你喜欢
vxe-table实现复选框鼠标拖动选中
VS Code connects to SQL Server
coming!Dongfang Selection brings goods to the live broadcast of Longjiang agricultural products
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
M3SDA:用于多源域自适应的矩匹配
SimpleOSS第三方库libcurl与引擎libcurl错误解决方法
牛客刷题系列之进阶版(组队竞赛,排序子序列,倒置字符串, 删除公共字符,修理牧场)
LeetCode 0952. Calculate Maximum Component Size by Common Factor: Mapping / Union Search
MindSpore:【语音识别】DFCNN网络训练loss不收敛
阿里面试这些微服务还不会?那还是别去了,基本等通知
随机推荐
浅聊对比学习(Contrastive Learning)第一弹
Tensorflow2.0 confusion matrix does not match printing accuracy
VBA batch import Excel data into Access database
VS Code connects to SQL Server
MindSpore:【JupyterLab】按照新手教程训练时报错
HCIP --- 企业网的三层架构
7.30模拟赛总结
架构师如何成长
ELK日志分析系统
Linux下安装MySQL教程
MySQL数据库 ---MySQL表的增删改查(进阶)
mysql8 installation under linux
How to install and use PostgreSQL 14.4
Frog jumping steps (recursive and non-recursive) ------- Xiaolele walks the steps
【MindSpore】用coco2017训练Model_zoo上的 yolov4,迭代了两千多batch_size之后报错,大佬们帮忙看看。
MySQL Functions (Classic Collection)
LeetCode每日一题(1717. Maximum Score From Removing Substrings)
VBA 运行时错误‘-2147217900(80040e14):自动化(Automation)错误
Centos7 install mysql8
看完《二舅》,我更内耗了