Home > Database > Mysql Tutorial > body text

Detailed explanation of MySQL replication table functions and example codes

零下一度
Release: 2017-05-16 10:48:58
Original
1374 people have browsed it

MySQL copy table

If we need to completely copy the MySQL data table, including the table structure, indexes, default values, etc. If you just use the CREATE TABLE ... SELECT command, it cannot be achieved.

This chapter will introduce how to completely 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 original data. Table structure, indexes, etc.

Copy the SQL statement displayed by the following command, modify the data table name, and execute the SQL statement. The data table structure will be completely copied through the above command.

If you want to copy the contents of the table, you can use the INSERT INTO ... SELECT statement to achieve this.

Example

Try the following example to copy the table tutorials_tbl.

Step 1:

Get the complete structure of the data table.

mysql> SHOW CREATE TABLE tutorials_tbl \G;
*************************** 1. row ***************************
       Table: tutorials_tbl
Create 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=MyISAM
1 row in set (0.00 sec)
ERROR:
No query specified
Copy after login

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)
Copy after login

Step 3:

After performing the second step, you will create a new clone table clone_tbl in the database. If you want to copy the data in the 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
Copy after login

After performing the above steps, you will completely copy the table, including table structure and table data.

【Related Recommendations】

1. Special Recommendation:"php Programmer Toolbox" V0.1 version Download

2. Free mysql online video tutorial

3. Those things about database design

The above is the detailed content of Detailed explanation of MySQL replication table functions and example codes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!