Syntax error Simplest way to copy data from one table to another new table in MySQL?

Simplest way to copy data from one table to another new table in MySQL?



To copy data from one table to another table, firstly we will create a table.

Creating first table −

mysql> CREATE table FirstTable
-> (
-> id int,
-> name varchar(100)
-> );
Query OK, 0 rows affected (0.61 sec)

After creating a table, we will insert records.

mysql> INSERT into FirstTable values(1,'john');
Query OK, 1 row affected (0.20 sec)

mysql> INSERT into FirstTable values(2,'Smith');
Query OK, 1 row affected (0.21 sec)

We can display all the records with the help of SELECT statement −

mysql> SELECT * From FirstTable;

The following is the output

+------+-------+
| id   | name  |
+------+-------+
| 1    | john  |
| 2    | Smith |
+------+-------+
2 rows in set (0.00 sec)

Now, I am creating second table and I will copy the first table data into the second table. Creating second table −

mysql> CREATE table SecondTable
-> (
-> id int,
-> name varchar(100)
-> );
Query OK, 0 rows affected (0.69 sec)

Here is the query to make a copy −

mysql> INSERT INTO SecondTable (id,name)
-> select id,name from FirstTable;
Query OK, 2 rows affected (0.17 sec)
Records: 2 Duplicates: 0 Warnings: 0

To check that the record is copied or not, we can check the secondTable with the help of SELECT statement −

mysql> SELECT * from SecondTable;

The following is the output

+------+-------+
| id   | name  |
+------+-------+
| 1    | john  |
| 2    | Smith |
+------+-------+
2 rows in set (0.00 sec)
Updated on: 2020-06-25T08:14:27+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements