How to copy structure and data from one table to another table in MySQL


As per following queries we can copy table structure as well data to another duplicate table in MySQL.

How to create duplicate table structure in MySQL ?

This query will copy the table structure and indexes, but data not copy.

CREATE TABLE new_table LIKE old_table;

How to create copy of table with data in MySQL ?

This query will copy structure as well as data but indexes are not copy.

CREATE TABLE new_table SELECT * FROM old_table;

How to copy of table with data in MySQL ?

This query will copy structure as well as data but indexes are not copy.

CREATE TABLE new_table SELECT * FROM old_table;

Copy Everything

Copy the structure, data, indexes, primary key, foreign key constraints and triggers use this query

CREATE TABLE new_table LIKE old_table; 
INSERT new_table SELECT * FROM old_table; #only data

How to copy a table from one database to another in MySQL ?

CREATE TABLE destination_db_name.new_table LIKE source_db_name.old_table;

INSERT destination_db_name.new_table
SELECT * FROM source_db_name.old_table;


Related

find the nth highest salary in mysql without limit
How to find greater than average salary records in MySQL
how to find duplicate records in MySQL query
How to use second index forcefully in MySQL
How to get total number of rows in MySQL table with and without count