MySQL clone table (copy table structure and table data)

Source: Internet
Author: User
Tags table name

There may be a situation where you need an identical replica table create TABLE ... The select is not appropriate because the copy must contain the same index, default, and so on.

You can handle this situation by following the steps below.

Use show create table to get the structure, index, and so on of the specified source table in a CREATE TABLE statement.

Modify the statement to change the clone table of the table name and execute the statement. In this way there will be an exact clone table.

Alternatively, if you need to make a copy of the table's content, use INSERT INTO ... Select statements can also be handled.

Instance:
Try the following example to create a clone table Tutorials_tbl

Step 1:
About the table get the complete structure

The code is as follows Copy Code

Mysql> show CREATE TABLE tutorials_tbl G;
1. Row ***************************
Table:tutorials_tbl
Create table:create Table ' tutorials_tbl ' (
' tutorial_id ' int (one) not NULL auto_increment,
' tutorial_title ' varchar not NULL default ',
' Tutorial_author ' varchar not NULL default ',
' submission_date ' date default NULL,
PRIMARY KEY (' tutorial_id '),
UNIQUE KEY ' Author_index ' (' Tutorial_author ')
) Type=innodb
1 row in Set (0.00 sec)

ERROR:
No query specified

Step 2:
Rename this table and create another table

The code is as follows Copy Code
mysql> CREATE TABLE ' clone_tbl ' (
-> ' tutorial_id ' int (one) not NULL auto_increment,
-> ' tutorial_title ' varchar (m) Not NULL default ',
-> ' tutorial_author ' varchar () not NULL default ',
-> ' submission_date ' date default NULL,
-> PRIMARY KEY (' tutorial_id '),
-> UNIQUE KEY ' author_index ' (' Tutorial_author ')
->) Type=innodb;
Query OK, 0 rows affected (1.80 sec)

Step 3:
In the table of the clone database that executes step 2. If you want to copy data from an old table, you can use INSERT INTO ... SELECT statement.

The code is as follows Copy Code
Mysql> INSERT into Clone_tbl (tutorial_id,
-> Tutorial_title,
-> Tutorial_author,
-> submission_date)
-> SELECT Tutorial_id,tutorial_title,
-> Tutorial_author,submission_date,
-> from Tutorials_tbl;
Query OK, 3 rows affected (0.07 sec)
Records:3 duplicates:0 warnings:0

Finally, there will be an exact clone table.

Related Article

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.