Sei sulla pagina 1di 7

MySQL Clone Tables

What is a Clone Table


A Clone table is an exact copy of an
existing table which is otherwise
insufficient to be created using a CREATE
TABLE . SELECT statement
A clone table can be regarded as a
duplicate table which is the exact copy of
an original existing table with the same
indexes, default vales and so on
Steps to create a Clone Table
Use SHOW CREATE TABLE to get a CREATE
TABLE statement that specifies the source
table's structure, indexes and all
Modify the statement to change the table
name to that of the clone table and execute
the statement and hence, we will have exact
clone table
Optionally, if you need the table contents
copied as well, issue an INSERT INTO ...
SELECT statement, too
Example : Creating A Clone Table
Step 1: Get complete structure about table
mysql> show create table actor\G
***** 1. row *****
Table: actor
Create Table: CREATE TABLE `actor` ( `actor_id`
smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(45) NOT NULL, `last_name`
varchar(45) NOT NULL, `last_update` timestamp NOT
NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP, PRIMARY KEY (`actor_id`), KEY
`idx_actor_last_name` (`last_name`) ) ENGINE=InnoDB
AUTO_INCREMENT=201 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
Example : Creating A Clone Table
Step 2:Rename this table and create another table.
mysql> CREATE TABLE `actor_copy` ( `actor_id` smallint(5) unsigned NOT
NULL AUTO_INCREMENT, `first_name` varchar(45) NOT NULL, `last_name`
varchar(45) NOT NULL, `last_update` timestamp NOT NULL DEFAULT
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY
(`actor_id`), KEY `idx_actor_last_name` (`last_name`) ) ENGINE=InnoDB
AUTO_INCREMENT=201 DEFAULT CHARSET=utf8
mysql> show create table actor_copy\G
******** 1. row ******
Table: actor_copy
Create Table: CREATE TABLE `actor_copy` ( `actor_id` smallint(5) unsigned
NOT NULL DEFAULT '0', `first_name` varchar(45) CHARACTER SET utf8 NOT
NULL, `last_name` varchar(45) CHARACTER SET utf8 NOT NULL,
`last_update` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' )
ENGINE=InnoDB AUTO_INCREMENT=201 DEFAULT CHARSET=utf8 1 row in
set (0.00 sec)
Example : Creating A Clone Table
Step 3: After executing step 2, we will create a clone
table in our database If we want to copy data from
old table then we can do it by using INSERT INTO...
SELECT statement
mysql> INSERT INTO actor_copy (actor_id,
first_name, last_name, last_update) SELECT
actor_id, first_name, last_name, last_update FROM
actor;
Query OK, 3 rows affected (0.07 sec) Records: 3
Duplicates: 0 Warnings: 0
MySQL query to copy the structure of a
table to create another table

CREATE TABLE `new_table_name` LIKE `old_table_name`;


INSERT INTO `new_table_name` SELECT * FROM
`old_table_name`;
Suppose we want to create the table and insert all the data
from the old table but without bringing the keys from the
old table. So we will need to set the keys for the new table
CREATE TABLE `new_table_name` SELECT * FROM
`old_table_name`;

Potrebbero piacerti anche