Home > Database > Mysql Tutorial > How Can I Duplicate a MySQL Table Including Data and Indices?

How Can I Duplicate a MySQL Table Including Data and Indices?

Barbara Streisand
Release: 2024-12-01 07:50:14
Original
119 people have browsed it

How Can I Duplicate a MySQL Table Including Data and Indices?

Duplicating a MySQL Table with Indices and Data

Replicating MySQL tables often requires preserving data, structure, and indices. Copying these components separately can be tedious. Fortunately, there's a straightforward method to achieve this.

To duplicate a table completely, execute the following steps:

  1. Create the new table with the same structure and indices:
CREATE TABLE new_table LIKE old_table;
Copy after login
  1. Insert data into the new table:
INSERT INTO new_table SELECT * FROM old_table;
Copy after login

This method effectively copies all data, structure, and indices while avoiding extra table recreation and data duplication.

If copying only data and structure is desired, the following query can be used:

CREATE TABLE new_table AS SELECT * FROM old_table;
Copy after login

This query creates a new table with the same data and structure as the original table, but it does not copy any indices.

The above is the detailed content of How Can I Duplicate a MySQL Table Including Data and Indices?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template