To clone in MySQL, use LIKE clause. Let us first create a table −
mysql> create table DemoTable1 -> ( -> StudentId int, -> StudentName varchar(20) -> ); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(100,'John'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1 values(101,'Sam'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1 values(102,'Carol'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
Output
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 100 | John | | 101 | Sam | | 102 | Carol | +-----------+-------------+ 3 rows in set (0.00 sec)
Following is the query to clone a table structure in MySQL −
mysql> create table DemoTable2 LIKE DemoTable1; Query OK, 0 rows affected (0.53 sec)
Let us check the structure of newly created table −
mysql> show create table DemoTable2;
Output
This will produce the following output −
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | DemoTable2 | CREATE TABLE `demotable2` (`StudentId` int(11) DEFAULT NULL, `StudentName` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 OLLATE=utf8_unicode_ci | +--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.03 sec)