Three ways to copy MySQL tables (summary)

Three ways to copy MySQL tables (summary)

Copy table structure and its data

The following statement will copy the data into a new table.

Note: This statement actually just creates a table with the results of the select statement, so the new table will not have a primary key or index.

create table table_name_new as (select * from table_name_old);

Copy only the table structure

create table table_name_new as select * from table_name_old where 1=2;

or

create table table_name_new like table_name_old;

Note: The former method will not copy the primary key type and index, while the latter method will copy all field types of the old table to the new table.

Copy only table data

If the two tables have the same structure

insert into table_name_new select * from table_name_old;

If the two tables have different structures

insert into table_name_new(column1,column2...) select column1,column2... from table_name_old;

Note: Many articles say that data can be copied using the following statement. The table_name_new table does not need to exist and will be automatically created during execution. In fact, the SELECT ... INTO form stores the query results in a variable or writes them to a file, that is, table_name_new is a variable or a file.

select column1,column2,.... into table_name_new from table_name_old;

This concludes this article on three ways to copy MySQL tables (summary). For more information about copying MySQL tables, please search 123WORDPRESS.COM’s previous articles or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Three implementation methods of Mysql copy table and grant analysis
  • Mysql method to copy a column of data in one table to a column in another table
  • Copy fields between different tables in MySQL
  • How to use worm replication in Mysql data table
  • MySQL replication table details and example code
  • How to copy MySQL query results to a new table (update, insert)
  • mysql copy table structure and data example code
  • Tutorial on copying data from a table to a new table in MySQL
  • Tutorial on table replication and large data table backup in MySQL
  • Mysql method of copying table structure and table data
  • How to copy MySQL table

<<:  Summary of 28 common JavaScript string methods and usage tips

>>:  Working principle and example analysis of Linux NFS mechanism

Recommend

Where is the project location deployed by IntelliJ IDEA using Tomcat?

After IntelliJ IDEA deploys a Javaweb project usi...

Detailed steps to install Mysql5.7.19 using yum on Centos7

There is no mysql by default in the yum source of...

Mysql specifies the date range extraction method

In the process of database operation, it is inevi...

How to build a complete samba server in Linux (centos version)

Preface smb is the name of a protocol that can be...

Solution to MySQL IFNULL judgment problem

Problem: The null type data returned by mybatis d...

MySQL database master-slave configuration tutorial under Windows

The detailed process of configuring the MySQL dat...

Sample code using the element calendar component in Vue

First look at the effect diagram: The complete co...

Linux system AutoFs automatic mount service installation and configuration

Table of contents Preface 1. Install the service ...

Example of disabling browser cache configuration in Vue project

When releasing a project, you will often encounte...

100 ways to change the color of an image using CSS (worth collecting)

Preface “When it comes to image processing, we of...

What is the function and writing order of the a tag pseudo class

The role of the a tag pseudo-class: ":link&qu...

Introduction to Semantic XHTML Tags

The first point to make is that people can judge t...

Implementation of scheduled backup in Mysql5.7

1. Find mysqldump.exe in the MySQL installation p...

Detailed explanation of samba + OPENldap to build a file sharing server

Here I use samba (file sharing service) v4.9.1 + ...