Copy Table in a MySQL Database
In this section, you will learn to copy one table to another in a same MySQL database.
Description
of Code:
This program helps you in copying one table to another in a same MySQL database. To copy any table, firstly, you need to establish the connection with MySQL database (jdbc4). This database has both the tables (Copyemployee and employee table). That is the one which is to be copied to the other table and the second is the table to be copied. For copying a table, we have applied the ?INSERT INTO Copyemployee SELECT * FROM employee? SQL statement. Whenever a table is copied, it displays ?Numbers of row(s) affected?. If none of the rows is copied then it shows a message ?Don?t add any row!?.
[Note: Both tables have the same field and its data type.]
Syntax
for copy tables:
INSERT INTO <new_table_name> SELECT * FROM <old_table_name>
Here is code of program:
import java.sql.*;
|
Table Name: employee
empId | empName | empSal |
1 | Vinod | 50000 |
2 | Sushil | 80000 |
Table Name: Copyemployee
empId | empName | empSal |
8 | AmarDeep | 10000 |
7 | Noor | 50000 |
Output of this program:
C:\vinod>javac CopyOneTableToAnother.java C:\vinod> C:\vinod>java CopyOneTableToAnother Copy data from one table to another in a database! 2 row(s)affected. C:\vinod> |
After copy table: Copyemployee
empId | empName | empSal |
8 | AmarDeep | 10000 |
7 | Noor | 50000 |
1 | Vinod | 50000 |
2 | Sushil | 80000 |