Wu Yuxiong - natural born MySQL study notes: MySQL replication table

If complete copy MySQL data table, including the structure, index, table of default values and the like. If you only use the CREATE TABLE ... SELECT command, can not be achieved. 
How complete copy MySQL data tables, the following steps: 
using SHOW CREATE TABLE command to get the data table is created (CREATE TABLE) statement that contains the original data table structure, indexes and so on. 
Copy the following command to display the SQL statement, modify the table name, and execute SQL statements, the above command will complete copy data table structure. 
If you want to copy the contents of the table, you can use INSERT INTO ... SELECT statement to achieve.
The following instances replicate table runoob_tbl. 
Step a: 
for a complete data table structure. 
MySQL > SHOW the CREATE TABLE runoob_tbl \ G;
 *************************** 1. Row *********** **************** 
       the Table: runoob_tbl 
the Create the Table: runoob_tbl` the CREATE TABLE `( 
  ` runoob_id` int ( . 11 ) the NOT NULL AUTO_INCREMENT, 
  `runoob_title` VARCHAR ( 100) the NOT NULL default ' ' , 
  `runoob_author` VARCHAR ( 40) the NOT NULL default ' ' , 
  ` submission_date` DATE default NULL, 
  a PRIMARY KEY ( `runoob_id`), 
  UNIQUE KEY AUTHOR_INDEX`` ( `runoob_author`) 
) ENGINE = the InnoDB
 
step two:
Modify data table name SQL statements and execute SQL statements. 
MySQL > the CREATE TABLE `clone_tbl` (
   ->` runoob_id` int (. 11 ) the NOT NULL AUTO_INCREMENT,
   -> `runoob_title` VARCHAR (100) the NOT NULL default '' ,
   ->` runoob_author` VARCHAR (40) the NOT NULL default '' ,
   -> `submission_date` DATE default NULL,
   -> a PRIMARY KEY (` runoob_id`),
   -> UNIQUE KEY `AUTHOR_INDEX` (runoob_author``)
 ->) = ENGINE the InnoDB; 

step three: 
after performing the second step, create a new clone clone_tbl table in the database. If you want to copy data table data can be used INSERT INTO ... SELECT statement to achieve. 
MySQL > INSERT INTO clone_tbl (runoob_id,
     ->                        runoob_title,
     ->                         runoob_author,
     ->                         submission_date)
     -> the SELECT runoob_id, runoob_title,
     ->         runoob_author, submission_date
     -> the FROM runoob_tbl; 

After performing the above steps, a complete copy of the table, comprising a table structure and table data.

 

Guess you like

Origin www.cnblogs.com/tszr/p/12114531.html