Detailed description of MySQL copy table and instance code, detailed description of mysql instance code
MySQL copy table details
If we need to completely copy the MySQL DATA table, including the table structure, index, and default value, etc. If you only use the create table... SELECT command, it cannot be implemented.
This section describes how to copy a MySQL DATA table. The steps are as follows:
- Use the show create table command to obtain the create table statement, which contains the structure and index of the original data TABLE.
- Copy the SQL statement displayed by the following command, modify the name of the data table, and execute the SQL statement to completely copy the data table structure.
- If you want to copy the table content, you can use the insert into... SELECT statement.
Instance
Try the following example to repeat tabulation tutorials_tbl.
Step 1:
Obtain the complete structure of a data table.
mysql> SHOW CREATE TABLE tutorials_tbl \G;*************************** 1. row *************************** Table: tutorials_tblCreate Table: CREATE TABLE `tutorials_tbl` ( `tutorial_id` int(11) NOT NULL auto_increment, `tutorial_title` varchar(100) NOT NULL default '', `tutorial_author` varchar(40) NOT NULL default '', `submission_date` date default NULL, PRIMARY KEY (`tutorial_id`), UNIQUE KEY `AUTHOR_INDEX` (`tutorial_author`)) TYPE=MyISAM1 row in set (0.00 sec)ERROR:No query specified
Step 2:
Modify the data table name of the SQL statement and execute the SQL statement.
mysql> CREATE TABLE `clone_tbl` ( -> `tutorial_id` int(11) NOT NULL auto_increment, -> `tutorial_title` varchar(100) NOT NULL default '', -> `tutorial_author` varchar(40) NOT NULL default '', -> `submission_date` date default NULL, -> PRIMARY KEY (`tutorial_id`), -> UNIQUE KEY `AUTHOR_INDEX` (`tutorial_author`)-> ) TYPE=MyISAM;Query OK, 0 rows affected (1.80 sec)
Step 3:
After the second step is completed, you will create a clone table named clone_tbl in the database. If you want to copy data from a data table, you can use the insert into... SELECT statement.
mysql> INSERT INTO clone_tbl (tutorial_id, -> tutorial_title, -> tutorial_author, -> submission_date) -> SELECT tutorial_id,tutorial_title, -> tutorial_author,submission_date -> FROM tutorials_tbl;Query OK, 3 rows affected (0.07 sec)Records: 3 Duplicates: 0 Warnings: 0
After performing the preceding steps, you can copy the complete table, including the table structure and table data.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!