Detailed description of MySQL copy table and instance code, detailed description of mysql instance code

Source: Internet
Author: User

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:

  1. Use the show create table command to obtain the create table statement, which contains the structure and index of the original data TABLE.
  2. 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.
  3. 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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.