- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
- Related Articles
- MySQL statement to copy data from one table and insert into another table
- Easiest way to copy values of one column to a new table in MySQL?
- How to copy rows from one table to another in MySQL?
- How to copy a table from one MySQL database to another?
- Insert data from one table to another in MySQL?
- Update data in one table from data in another table in MySQL?
- MySQL query to copy records from one table to another with different columns
- Copy column values from one table into another matching IDs in MySQL
- Copy all rows of a table to another table in MySQL?
- Copy a few columns from a table to another in MySQL
- Move rows from one table to another in MySQL?
- Insert from one table with different structure to another in MySQL?
- How to copy data from one field to another on every row in MySQL?
- MySQL query to insert data from another table merged with constants?
- How to SELECT fields from one table and INSERT to another in MySQL?

Advertisements