MySQL Replication table and table data to the new table method

Source: Internet
Author: User

Copy the table in MySQL and copy the Old_table table to the New_table table.

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

The code is as follows Copy Code
CREATE TABLE new_table like Old_table2

. By a SELECT query to copy, New_table table will lose primary key, index and other information.

Reference

The code is as follows Copy Code

CREATE TABLE new_table as
(
SELECT *
From old_table
)

3. Full Copy Table

The code is as follows Copy Code

CREATE TABLE new_table like old_table;
INSERT into new_table SELECT * from old_table;

4. Copy only fields

The code is as follows Copy Code

CREATE TABLE new_table as
(
SELECT field1, field2 from old_table
)

5. Partial copies

The code is as follows Copy Code

CREATE TABLE new_table as
(
SELECT * from old_table WHERE field1 = ' Mangguo '
)

Data replication

Copy data from old table to new table (assuming two table structures are different)

INSERT into new Table (Field 1, Field 2,.......) SELECT field 1, Field 2,...... From old table

Copy data from old table to new table (assuming two table structure)

INSERT into new Table select * from old table

CREATE Table New Table

SELECT * from old table where 1=2 is: let where condition not set

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.