MySQL replication table-insert into SELECT

Source: Internet
Author: User

The base table is:

Mysql>Select* fromStaff ;+----+----------+-------+| ID | name | Slary |+----+----------+-------+|3| Haofugui |10000||4| guoming |3500||5| Haotian |2900|+----+----------+-------+3Rowsinch Set(0.00sec) MySQL>describe staff;+-------+----------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+----------------+| ID |int( One)  | NO | PRI | NULL | auto_increment | | name |Char( -) |     YES | |                NULL | || Slary |int( One)  |     YES | |                NULL | |+-------+----------+------+-----+---------+----------------+3Rowsinch Set(0.00Sec

The INSERT INTO SELECT statement copies data from a table, and then inserts the data into an existing table (the target table), and any existing rows in the target table are unaffected.

Statement 1: Insert into Table2 (field1,field2,...) Select value1,value2,... from Table1 where Condition;

Statement 2: Insert into Table2 select * from Table1;

Description: 1) The target table Table2 must exist, MySQL will not be created automatically;

2) Statement 1 is generally used for two table structure of the same situation, and statement 2 is used in the case of inconsistent table structure;

3) The field1,field2 involved in statement 1 must also exist;

4) If Table2 has a primary key and does not have automatic growth, then field1, field2 ... Must include the primary key.

Example:

Mysql>CREATE table staff_bak_1 like staff; Query OK,0Rows Affected (0.05sec) MySQL>describe Staff_bak_1;+-------+----------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+----------------+| ID |int( One)  | NO | PRI | NULL | auto_increment | | name |Char( -) |     YES | |                NULL | || Slary |int( One)  |     YES | |                NULL | |+-------+----------+------+-----+---------+----------------+3Rowsinch Set(0.01sec) MySQL>Select* fromStaff_bak_1; EmptySet(0.00sec) MySQL>INSERT INTO Staff_bak_1 select * fromStaff  ; Query OK,3Rows Affected (0.00sec) Records:3Duplicates:0Warnings:0MySQL>Select* fromStaff_bak_1;+----+----------+-------+| ID | name | Slary |+----+----------+-------+|3| Haofugui |10000||4| guoming |3500||5| Haotian |2900|+----+----------+-------+3Rowsinch Set(0.00Sec

Mysql>CREATE TABLE Staff_bak_2(IDintauto_increment primary KEY,NameChar( -)); Query OK,0Rows Affected (0.05sec) MySQL>describe staff_bak_2;+-------+----------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+----------------+| ID |int( One)  | NO | PRI | NULL | auto_increment | | name |Char( -) |     YES | |                NULL | |+-------+----------+------+-----+---------+----------------+2Rowsinch Set(0.01sec) MySQL>INSERT into Staff_bak_2 (name) select name from the staff where id!=5; Query OK,2Rows Affected (0.00sec) Records:2Duplicates:0Warnings:0MySQL>Select* fromstaff_bak_2;+----+----------+| ID | Name |+----+----------+|1| Haofugui | |2| guoming |+----+----------+2Rowsinch Set(0.00Sec

MySQL replication table-insert into SELECT

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.