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.