当前位置:网站首页>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
边栏推荐
猜你喜欢
MySQL performance optimization (hardware, system configuration, table structure, SQL statements)
牛客刷题系列之进阶版(搜索旋转排序数组,链表内指定区间反转)
ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution
Download Win11 how to change the default path?Download Win11 change the default path method
VBA batch import Excel data into Access database
云数据库和本地数据库有什么区别?
Zabbix 5.0 监控教程(一)
MySQL sub-database sub-table
NXP IMX8QXP replacement DDR model operation process
MySQL八股文背诵版
随机推荐
055 c# print
vxe-table实现复选框鼠标拖动选中
试写C语言三子棋
Linux下安装MySQL教程
LeetCode每日一题(1717. Maximum Score From Removing Substrings)
VBA 连接Access数据库和Excle
VBA connects Access database and Excel
MySQL performance optimization (hardware, system configuration, table structure, SQL statements)
【PyTorchVideo教程01】快速实现视频动作识别
iPhone真是十三香?两代产品完全对比,或许上一代更值得买
Object和Map的区别
【hbuilder】运行不了部分项目 , 打开终端 无法输入指令
Perfectly Clear QuickDesk & QuickServer图像校正优化工具
MindSpore:对image作normalize的目的是什么?
Linux下载安装mysql5.7版本教程最全详解
【MindSpore1.2.0-rc1产品】num_workers问题
MySQL性能优化(硬件,系统配置,表结构,SQL语句)
ResNet18-实现图像分类
VS Code 连接SQL Server
M3SDA:用于多源域自适应的矩匹配