Insert Into,replace into, insert ignore of MySQL database insertion statement

Source: Internet
Author: User
Tags mysql insert

Only recently found that the MySQL INSERT statement has so many uses, here to share.

① about INSERT INTO:

INSERT INTO table_name values ();

INSERT INTO table_name (column) values ();

INSERT INTO TABLE_NAME VALUES (select (column) from table_name2);

The only thing to be aware of here is:

If a primary key conflict occurs (that is, the inserted primary key already exists in the table), the system errors.

②replace into:

Replace into is similar to the INSERT INTO function, except that replace into first attempts to insert data into the table, 1. If you find that this row of data is already in the table (judging by a primary key or a unique index), the row data is first deleted and then the new data is inserted. 2. Otherwise, insert the new data directly.
Note that the table in which the data is inserted must have a primary key or a unique index! Otherwise, replace into will insert the data directly, which will result in duplicate data appearing in the table.

③insert Ignore into

The main difference between insert ignore into and insert into is that when a primary key conflict occurs, the system does not make an error and skips the insertion of the entry directly.

The feeling is not very interesting ...

Let's do an experiment.

CREATE TABLE Test (

' id ' int (one) not null auto_increment comment ' primary key ',

' Name ' varchar (a) NOT null comment ' name ',

Primary KEY (' ID ')

) Engine=innodb DEFAULT CHARSET UTF8 comment= ' Test table ';


Then we insert a few data in.

INSERT into Test (name) VALUES (' vein ');

INSERT into Test (name) VALUES (' vein1 ');

INSERT into Test (name) VALUES (' vein2 ');

When the following statement is executed:

INSERT into Test (Id,name) VALUES (1, ' vein11 ');

The system will error, saying the primary key conflict.

If you execute the following statement:

Insert ignore into Test (Id,name) VALUES (1, ' vein11 ');

System will not error, directly ignored.

Replace into Test (id,name) VALUES (1, ' vein11 ');

The system was inserted successfully, and the modified table is recorded as

1, Vein11.


Edited by Veinqueen



Insert Into,replace into, insert ignore of MySQL database insertion statement

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.