Take all records from one MySQL table and insert it to another?


For this, you can use the concept of CREATE TABLE AS SELECT statement. Let us first create a table −

mysql> create table DemoTable1518
   -> (
   -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> EmployeeName varchar(20)
   -> )AUTO_INCREMENT=101;
Query OK, 0 rows affected (0.69 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1518(EmployeeName) values('John Doe');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1518(EmployeeName) values('John Smith');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1518(EmployeeName) values('David Miller');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1518;

This will produce the following output −

+------------+--------------+
| EmployeeId | EmployeeName |
+------------+--------------+
|        101 | John Doe     |
|        102 | John Smith   |
|        103 | David Miller |
+------------+--------------+
3 rows in set (0.00 sec)

Following is the query to take all records from one MySQL table and insert it to another −

mysql> create table DemoTable1519 as select * from DemoTable1518;
Query OK, 3 rows affected (0.62 sec)
Records: 3  Duplicates: 0  Warnings: 0

Display all records from the table using select statement −

mysql> select * from DemoTable1519;

This will produce the following output −

+------------+--------------+
| EmployeeId | EmployeeName |
+------------+--------------+
|        101 | John Doe     |
|        102 | John Smith   |
|        103 | David Miller |
+------------+--------------+
3 rows in set (0.00 sec)

Updated on: 11-Dec-2019

110 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements