如何克隆MySQL表,索引和数据?

要克隆MySQL表,索引和数据,我们可以使用“ LIKE”。我有一个数据库“ business”,并且已经创建了许多表。在这里,我们可以检查所有表格。

mysql> USE business;
Database changed

让我们显示所有表格-

mysql> SHOW tables;
+------------------------+
| Tables_in_business     |
+------------------------+
| addcolumntable         |
| autoincrement          |
| autoincrementtable     |
| bookindexes            |
| chardemo               |
| columnvaluenulldemo    |
| dateadddemo            |
| deletedemo             |
| deleterecord           |
| demo                   |
| demo1                  |
| demoascii              |
| demoauto               |
| demobcrypt             |
| demoemptyandnull       |
| demoint                |
| demoonreplace          |
| demoschema             |
| distinctdemo           |
| duplicatebookindexes   |
| duplicatefound         |
| employeetable          |
| existsrowdemo          |
| findandreplacedemo     |
| foreigntable           |
| foreigntabledemo       |
| groupdemo              |
| groupdemo1             |
| incasesensdemo         |
| int1demo               | 
| intdemo                |
| latandlangdemo         |
| limitoffsetdemo        |
| milliseconddemo        |
| modifycolumnnamedemo   |
| modifydatatype         |
| moneydemo              |
| moviecollection        |
| mytable                |
| nonasciidemo           |
| nthrecorddemo          |
| nulldemo               |
| nullwithselect         |
| pasthistory            |
| presenthistory         |
| primarytable           |
| primarytable1          |
| primarytabledemo       |
| sequencedemo           |
| smallintdemo           |
| spacecolumn            |
| student                |
| tblstudent             |
| tbluni                 |
| textdemo               |
| texturl                |
| trailingandleadingdemo |
| unsigneddemo           |
| varchardemo            |
| varchardemo1           |
| varchardemo2           |
| varcharurl             |
| whereconditon          |
+------------------------+
63 rows in set (0.25 sec)

现在,我正在使用上述数据库中的表STUDENT。查询如下-

mysql> DESC student;

以下是输出

+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | YES  | MUL | NULL    |       |
| Name  | varchar(100) | YES  | MUL | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.20 sec)

现在让我们检查学生表的记录:

SELECT * from student

这是输出:

+------+------+
| id   | Name |
+------+------+
| 1    | John |
| 2    | Bob  |
+------+------+
2 rows in set (0.00 sec)

这是查询;我们可以使用LIKE复制索引和数据。查询如下-

mysql> CREATE table cloneStudent like student;

mysql> insert cloneStudent SELECT *from student;
Records: 2 Duplicates: 0 Warnings: 0

因此,我已经成功创建了克隆以及数据。现在,我们可以证明已经借助DESC命令创建了索引和数据的克隆,如下所示:

mysql> DESC cloneStudent;

以下是输出

+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | YES  | MUL | NULL    |       |
| Name  | varchar(100) | YES  | MUL | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

让我们检查表是否被克隆。

mysql> SELECT * from cloneStudent;

以下是输出:

+------+------+
| id   | Name |
+------+------+
| 1    | John |
| 2    | Bob  |
+------+------+
2 rows in set (0.00 sec)