

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Copy all rows of a table to another table in MySQL?
To copy all rows of a table to another table, use the below syntax −
insert into yourTableName2(yourColumnName1,...N) select yourColumnName1,..N from yourTableName1;
Let us first create a table −
mysql> create table DemoTable1(FirstName varchar(100)); Query OK, 0 rows affected (1.11 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values('John'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable1 values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1 values('Bob'); Query OK, 1 row affected (0.40 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | John | | Chris | | Bob | +-----------+ 3 rows in set (0.00 sec)
Following is the query to create second table −
mysql> create table DemoTable2 (EmployeeName varchar(100)); Query OK, 0 rows affected (0.78 sec)
Following is the query to copy all rows of a table to another table −
mysql> insert into DemoTable2(EmployeeName) select FirstName from DemoTable1; Query OK, 3 rows affected (0.52 sec) Records: 3 Duplicates: 0 Warnings: 0
Let us now check the records of the 2nd table, wherein we have set the records of 1st table −
mysql> select *from DemoTable2;
This will produce the following output −
+--------------+ | EmployeeName | +--------------+ | John | | Chris | | Bob | +--------------+ 3 rows in set (0.00 sec)
- Related Questions & Answers
- How to copy rows from one table to another in MySQL?
- Copy a few columns from a table to another in MySQL
- How to copy a table from one MySQL database to another?
- Simplest way to copy data from one table to another new table in MySQL?
- MySQL statement to copy data from one table and insert into another table
- Move rows from one table to another in MySQL?
- MySQL command to copy table?
- MySQL select query to select rows from a table that are not in another table?
- Create a table in MySQL that matches another table?
- Updating a MySQL table with values from another table?
- How can I loop through all rows of a table in MySQL?
- How can we delete all rows from a MySQL table?
- How to copy a table in MySQL using Python?
- 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
Advertisements