Home > Database > Mysql Tutorial > How to Duplicate a MySQL Table with Data and Indices?

How to Duplicate a MySQL Table with Data and Indices?

Susan Sarandon
Release: 2024-11-30 07:29:10
Original
264 people have browsed it

How to Duplicate a MySQL Table with Data and Indices?

Duplicating MySQL Table: Data, Indices, and Structure

Question:

How can a MySQL table, including its indices and data, be replicated or duplicated to create a new table?

Answer:

To create an exact duplicate of a MySQL table, including its data, structure, and indices, follow these steps:

  1. Create a new table with the same structure and indices as the original:

    • CREATE TABLE new_table LIKE old_table;
  2. Insert data from the old table into the new table:

    • INSERT INTO new_table SELECT * FROM old_table;

Additional Options:

  • To copy only the structure and data without the indices, use the following query:

    • CREATE TABLE new_table AS SELECT * FROM old_table;
  • To copy the indices and triggers along with the data, execute the following queries:

    • CREATE TABLE new_table LIKE old_table;
    • INSERT INTO new_table SELECT * FROM old_table;

The above is the detailed content of How to Duplicate a MySQL Table with 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