MySQL copy tables in several ways

Source: Internet
Author: User

1. Copy the table structure into the new table. (Data in the table is not copied)

CREATE table new table like old table;
Or
CREATE Table New Table SELECT * from old table WHERE 1=2;

  

2. Copy the data into the new table. (The new table will not have a primary key, index)

CREATE table New Table as   (       SELECT * from       old table);

  

3. Really copy a table. You can use the following statement.

CREATE table new table like old table;   INSERT into new table SELECT * from old table;  
Or
CREATE Table New Table SELECT * from old table;

  

4. Operate a different database.

CREATE table new table like library 1. old table;   CREATE Table Library 2. New table like library 1. old table;  

  

5. Copy some of the fields in a table.

CREATE table New Table   as (       SELECT field 1, Field 2,... From the old table);

We can also copy part of the data

CREATE Table New Table as

SELECT * from old table WHERE left (username,1) = ' s '
);
Copy the data from the old table to the new table
(assuming two table structures)
INSERT into new table SELECT * from old table;

(assuming two table structures are different)
INSERT into new table
(
Field 1, Field 2,.......


from the old table;

  

6. Rename the field of the newly created table.

CREATE table New Table as   (       SELECT ID, username as uname, password as pass from old table);

  

7. Copy table 1 structure to table 2

SELECT * into table 2 from table 1 WHERE 1=2;

Copy all table 1 contents to table 2
SELECT * into table 2 from table 1;

  

8. Create a table to define the field information in the table at the same time.

CREATE Table New Table (    ID INTEGER not NULL auto_increment PRIMARY KEY) as (    SELECT * from old table   

9. Display the creation command for the old table

Show create table old table;

  

MySQL copy tables in several ways

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.