在MySQL中创建与另一个表匹配的表?
要在MySQL中创建与另一个表匹配的表,请使用带有LIKE运算符的CREATETABLE命令。语法如下-
create table yourNewTableName like yourOldTableName;
上面的语法创建表的结构。
如果要所有记录,请使用INSERTINTO…...SELECT*FROM命令。语法如下-
insert into yourNewTableName select *from yourOldTableName.
我有一张旧桌子和一些数据-
mysql> create table WholeWordMatchDemo −> ( −> Words varchar(200) −> );
首先,我们将创建一个表结构。查询如下-
mysql> create table NewTableDuplicate Like WholeWordMatchDemo;
现在,您可以借助show命令检查表结构是否已创建。查询如下-
mysql> show create table NewTableDuplicate;
以下是输出-
+-------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | NewTableDuplicate | CREATE TABLE `newtableduplicate` (`Words` varchar(200) DEFAULT NULL) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci | +-------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
复制新表中名称为“NewTableDuplicate”的所有记录。查询如下,将所有数据复制到新表中-
mysql> insert into NewTableDuplicate select *from WholeWordMatchDemo; Records: 3 Duplicates: 0 Warnings: 0
现在,您可以借助SELECT语句检查新表中是否存在所有记录。查询如下-
mysql> select *from NewTableDuplicate;
以下是输出-
+----------------------+ | Words | +----------------------+ | My Name is John | | Carol | | My Name is Johnson | +----------------------+ 3 rows in set (0.00 sec)
检查旧表是否具有相同的记录-
mysql> select *from WholeWordMatchDemo;
以下是输出-
+----------------------+ | Words | +----------------------+ | My Name is John | | Carol | | My Name is Johnson | +----------------------+ 3 rows in set (0.00 sec)