
為了建立一個像舊表一樣的新表及其資料、觸發器和索引,我們需要執行以下兩個查詢
1 2 | CREATE TABLE new_table LIKE old_table;
INSERT new_table SELECT * from old_table;
|
登入後複製
範例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | mysql> Create table employee(ID INT PRIMARY KEY NOT NULL AUTO_INCREMENT, NAME VARCHAR(20));
Query OK, 0 rows affected (0.21 sec)
mysql> Describe employee;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| NAME | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.07 sec)
mysql> Insert into employee(name) values('Gaurav'),('Raman');
Query OK, 2 rows affected (0.07 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> Select * from employee;
+----+--------+
| ID | NAME |
+----+--------+
| 1 | Gaurav |
| 2 | Raman |
+----+--------+
2 rows in set (0.00 sec)
|
登入後複製
下面的查詢將建立與表employee 具有相似結構的表employee1。可以透過執行 DESCRIBE 查詢來檢查它。
1 2 3 4 5 6 7 8 9 10 11 | mysql> create table employee1 like employee;
Query OK, 0 rows affected (0.19 sec)
mysql> describe employee1;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| NAME | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.14 sec)
|
登入後複製
現在下面的查詢將在employee1中插入與employee中相同的值,可以如下檢查
1 2 3 4 5 6 7 8 9 10 11 12 | mysql> INSERT INTO employee1 select * from employee;
Query OK, 2 rows affected (0.09 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from employee1;
+----+--------+
| ID | NAME |
+----+--------+
| 1 | Gaurav |
| 2 | Raman |
+----+--------+
2 rows in set (0.00 sec)
|
登入後複製
透過這種方式,我們也可以克隆表及其資料、觸發器和索引。
以上是如何複製/複製表及其資料、觸發器和索引?的詳細內容。更多資訊請關注PHP中文網其他相關文章!