How to copy a table

There are three ways to duplicate a table in MySQL:

  1. Only the table structure is copied

Only the table structure is copied, including progressive, index, and other structural content, but the data in the table is not copied. The syntax is as follows:

create table tableName like some_table;
Copy the code

For example, copy the structure of the STUDENTS table to the newly created Users table:

create table users like students;
Copy the code
  1. Only the table data is copied

Copy only the basic structure of the table and all data, but not primary keys, indexes, and so on. The syntax is as follows:

create table tableNmae select * from some_table;
Copy the code

For example, copy the data from the STUDENTS table to the newly created Users table:

create table users select * from students;
Copy the code
  1. A complete copy

A full copy is a complete copy of the table structure and data. A full copy requires two steps: first copy the table structure and then insert the table data. For example, copy the data and structure of the STUDENTS table to the newly created Users table:

create table users like students;
insert into users select * from students;
Copy the code