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

Nginx Layer 4 Load Balancing Configuration Guide

1. Introduction to Layer 4 Load Balancing What is...

Detailed explanation of nginx-naxsi whitelist rules

Whitelist rule syntax: BasicRule wl:ID [negative]...

MySQL 8.0.11 Installation Guide for Mac

MAC installs mysql8.0, the specific contents are ...

Example of configuring multiple SSL certificates for a single Nginx IP address

By default, Nginx supports only one SSL certifica...

Vue custom v-has instruction to implement button permission judgment

Application Scenario Taking the background manage...

Sample code for a simple seamless scrolling carousel implemented with native Js

There are many loopholes in the simple seamless s...

Front-end state management (Part 2)

Table of contents 1. Redux 1.1. Store (librarian)...

Detailed steps for installing and configuring MySQL 8.0 on CentOS 7.4 64-bit

Step 1: Get the MySQL YUM source Go to the MySQL ...

Basic usage and pitfalls of JavaScript array sort() method

Preface In daily code development, there are many...

Detailed explanation of the loop form item example in Vue

Sometimes we may encounter such a requirement, th...

IE6 space bug fix method

Look at the code: Copy code The code is as follows...

Detailed tutorial on installing MySQL 8.0.19 in zip version on win10

Table of contents 1. After downloading, unzip it ...