Home >Database >Mysql Tutorial >How Can I Duplicate a MySQL Table Including Structure, Data, and Indices?
When working with MySQL databases, there may be instances where you need to create a copy of an existing table, complete with its structure, data, and indices. This proves useful for various scenarios, such as creating backups, testing environments, or replicating data across multiple databases.
The method mentioned by you can copy either the data and structure or the structure and indices, but not both simultaneously. To achieve a complete duplication, you can follow this procedure:
Create the New Table's Structure and Indices:
CREATE TABLE new_table LIKE old_table;
This query creates a new table named new_table that has the same structure and indices as the old_table.
Insert Data into the New Table:
INSERT INTO new_table SELECT * FROM old_table;
This query inserts all data from the old_table into the newly created new_table.
This two-step process enables you to fully duplicate a MySQL table, including its data, structure, and indices.
For scenarios where you only need to copy the structure and data without the indices, you can use the following simplified query:
CREATE TABLE new_table AS SELECT * FROM old_table;
The above is the detailed content of How Can I Duplicate a MySQL Table Including Structure, Data, and Indices?. For more information, please follow other related articles on the PHP Chinese website!