How to copy data from a MySQL table to a new table

Source: Internet
Author: User
In mysql, if we want to perform full table copy on the data, in addition to importing and exporting data, we can also directly use commands to perform operations, which can help us with some special operations, such: the table structure can be copied when data is sharded.

In mysql, if we want to perform full table copy on the data, in addition to importing and exporting data, we can also directly use commands to perform operations, which can help us with some special operations, such: the table structure can be copied when data is sharded.

Copy the table in MySQL and copy the old_table table to the new_table table.

1. Do not copy table data, just copy structure.

The Code is as follows:
Create table new_table LIKE old_table2

. Copy the data using the SELECT query. The new_table table will lose the primary key, index, and other information.

Reference

The Code is as follows:

Create table new_table
(
SELECT *
FROM old_table
)

3. Completely copy the table

The Code is as follows:

Create table new_table LIKE old_table;
Insert into new_table SELECT * FROM old_table;

4. Copy fields only

The Code is as follows:

Create table new_table
(
SELECT field1, field2 FROM old_table
)

5. Partial copy

The Code is as follows:

Create table new_table
(
SELECT * FROM old_table WHERE field1 = 'mangguo'
)

Data Replication

Copy the data from the old table to the new table (assuming the two tables have different structures)

Insert into new table (Field 1, Field 2 ,.......) SELECT Field 1, Field 2 ,...... FROM old table

Copy the data from the old table to the new table (assuming the two tables have the same structure)

Insert into new table SELECT * FROM old table

Create table new TABLE

SELECT * FROM old table WHERE 1 = 2: Make the WHERE condition not true

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.