MySQL replication table details and example code

MySQL replication table details and example code

MySQL replication table detailed explanation

If we need to completely copy the MySQL data table, including the table structure, indexes, default values, etc. This cannot be achieved using only the CREATE TABLE ... SELECT command.

This section will introduce how to completely copy a MySQL data table. The steps are as follows:

  1. Use the SHOW CREATE TABLE command to obtain the create table (CREATE TABLE) statement, which contains the structure and index of the original table.
  2. Copy the SQL statement displayed in the following command, modify the data table name, and execute the SQL statement. The above commands will completely copy the data table structure.
  3. If you want to copy the contents of a table, you can use the INSERT INTO ... SELECT statement to do so.

Examples

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

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 completing the second step, you will create a new clone table clone_tbl in the database. If you want to copy the data in a table you can use the INSERT INTO... SELECT statement to do so.

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 executing the above steps, you will completely copy the table, including the table structure and table data.

Thank you for reading, I hope it can help you, thank you for your support of this site!

You may also be interested in:
  • Three ways to copy MySQL tables (summary)
  • Three implementation methods of Mysql copy table and grant analysis
  • Mysql method to copy a column of data in one table to a column in another table
  • Copy fields between different tables in MySQL
  • How to use worm replication in Mysql data table
  • How to copy MySQL query results to a new table (update, insert)
  • mysql copy table structure and data example code
  • Tutorial on copying data from a table to a new table in MySQL
  • Tutorial on table replication and large data table backup in MySQL
  • Mysql method of copying table structure and table data
  • How to copy MySQL table

<<:  The difference and use of json.stringify() and json.parse()

>>:  Basic operation tutorial of files and permissions in centos

Recommend

JavaScript message box example

Three types of message boxes can be created in Ja...

Control the vertical center of the text in the HTML text box through CSS

When the height attribute of Text is defined, the ...

About the problem of running git programs in jenkins deployed by docker

1. First, an error message is reported when assoc...

How to build an ELK log system based on Docker

Background requirements: As the business grows la...

JS implements WeChat's "shit bombing" function

Hello everyone, I am Qiufeng. Recently, WeChat ha...

How to create a database in navicat 8 for mysql

When developing a website, you often need to use ...

Summary of a CSS code that makes the entire site gray

In order to express the deep condolences of peopl...

Detailed steps to upgrade mysql8.0.11 to mysql8.0.17 under win2008

Upgrade background: In order to solve the vulnera...

Detailed steps to install Nginx on Linux

1. Nginx installation steps 1.1 Official website ...

Vue.js implements simple timer function

This article example shares the specific code of ...

Detailed explanation of publicPath usage in Webpack

Table of contents output output.path output.publi...

How to use squid to build a proxy server for http and https

When we introduced nginx, we also used nginx to s...