MySQL Copying Tables
If we need to fully copy a MySQL data table, including its structure, indexes, default values, etc.
This cannot be achieved by using only the CREATE TABLE ... SELECT command.
This chapter will introduce how to fully copy a MySQL data table; the steps are as follows:
- Use SHOW CREATE TABLE Use the command to get the create table (CREATE TABLE) statement, which contains the structure, indexes, etc. of the original data table.
- Copy the SQL statement shown by the following command, modify the data table name, and execute the SQL statement; through the above commands, the data table structure will be fully copied.
- If you want to copy the table's content, you can use the ** INSERT INTO ... SELECT ** statement.
Example
Try the following example to copy the table runoob_tbl.
Step 1:
Get the complete structure of the data table.
mysql> SHOW CREATE TABLE runoob_tbl \G;
*************************** 1. row ***************************
Table: runoob_tbl
Create Table: CREATE TABLE `runoob_tbl` (
`runoob_id` int(11) NOT NULL auto_increment,
`runoob_title` varchar(100) NOT NULL default '',
`runoob_author` varchar(40) NOT NULL default '',
`submission_date` date default NULL,
PRIMARY KEY (`runoob_id`),
UNIQUE KEY `AUTHOR_INDEX` (`runoob_author`)
) ENGINE=InnoDB
1 row in set (0.00 sec)
ERROR:
No query specified
Step 2:
Modify the data table name in the SQL statement and execute the SQL statement.
mysql> CREATE TABLE `clone_tbl` (
-> `runoob_id` int(11) NOT NULL auto_increment,
-> `runoob_title` varchar(100) NOT NULL default '',
-> `runoob_author` varchar(40) NOT NULL default '',
-> `submission_date` date default NULL,
-> PRIMARY KEY (`runoob_id`),
-> UNIQUE KEY `AUTHOR_INDEX` (`runoob_author`)
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (1.80 sec)
Step 3:
After completing step 2, you will create a new clone table clone_tbl in the database. If you want to copy the data table's data, you can use the ** INSERT INTO... SELECT ** statement.
mysql> INSERT INTO clone_tbl (runoob_id,
-> runoob_title,
-> runoob_author,
-> submission_date)
-> SELECT runoob_id,runoob_title,
-> runoob_author,submission_date
-> FROM runoob_tbl;
Query OK, 3 rows affected (0.07 sec)
Records: 3 Duplicates: 0 Warnings: 0
After executing the above steps, the table's content will be fully copied, including the table structure and data.
Using the mysqldump Command
The mysqldump command can be used to back up and restore MySQL databases.
If you only want to copy one table, you can use mysqldump to export the table's structure and data, then import it into a new database or a new table.
The following is a simple example.
Back up table data:
mysqldump -u username -p dbname old_table > old_table_dump.sql
This will export the structure and data of the table named old_table to an SQL file named old_table_dump.sql.
You need to provide the MySQL username and password, and replace username, dbname, and old_table with actual values.
Restore to a new database:
mysql -u username -p new_dbname < old_table_dump.sql
This will create a table named old_table in the new database (new_dbname) and import the previously exported structure and data into the new table.