要在MySQL中克隆,请使用LIKE子句。让我们首先创建一个表-
mysql> create table DemoTable1 -> ( -> StudentId int, -> StudentName varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1 values(100,'John'); mysql> insert into DemoTable1 values(101,'Sam'); mysql> insert into DemoTable1 values(102,'Carol');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable1;
输出结果
这将产生以下输出-
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 100 | John | | 101 | Sam | | 102 | Carol | +-----------+-------------+ 3 rows in set (0.00 sec)
以下是在MySQL中克隆表结构的查询-
mysql> create table DemoTable2 LIKE DemoTable1;
让我们检查新创建的表的结构-
mysql> show create table DemoTable2;
输出结果
这将产生以下输出-
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | 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)