Home > Database > Mysql Tutorial > body text

How to clone/copy a table and its data, triggers and indexes?

王林
Release: 2023-08-26 11:29:02
forward
594 people have browsed it

How to clone/copy a table and its data, triggers and indexes?

In order to create a new table with its data, triggers and indexes just like the old table, we need to run the following two queries

CREATE TABLE new_table LIKE old_table;
INSERT new_table SELECT * from old_table;
Copy after login

Example

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)
Copy after login

The following query will create table employee1 with a similar structure to table employee. You can check this by running a DESCRIBE query.

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)
Copy after login

Now the following query will insert the same value in employee1 as in employee, which can be checked as follows

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)
Copy after login

In this way, we can also clone the table and its data, trigger controller and index.

The above is the detailed content of How to clone/copy a table and its data, triggers and indexes?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!