[Switch] repeating insert ignored, mysql insertion operation skips, cover inserted cover, mysql update repeated

Demand BACKGROUND:
In general, when data is inserted, there is the case of dirty data, the primary key is repeated, then directly insert into being given, and the following sql are no longer performed, and if you can determine the following data can overwrite the previous data, directly replace into the line. Normally I use this more, see the following article in the remaining cases.

1.insert ignore into

When inserting data, such as when an error occurs, such as the duplicate data will not return an error, it returns only as a warning. So be sure to use ignore the statement itself is no problem, otherwise it will be ignored. E.g:

INSERT IGNORE INTO books (name) VALUES ('MySQL Manual')
INSERT IGNORE INTO books (name) VALUES ('MySQL Manual'),('NGINX Manual'),('REDIS Manual')

2.on duplicate key update
when the primary unique or repeated, the update statement is executed, as described later update is unnecessary statements, such as id = id, the same as a function, but the error can not be ignored. For example, in order to achieve data into duplicate name not given:

INSERT INTO books (name) VALUES ('MySQL Manual') ON duplicate KEY UPDATE id = id

3.insert ... select ... where not exist
according to the conditions of the select determines whether the insertion may be determined not only by the primary and UNIQUE, but also through other conditions. E.g:

INSERT INTO books (name) SELECT 'MySQL Manual' FROM dual WHERE NOT EXISTS (SELECT id FROM books WHERE id = 1)

4.replace into
if the same primary or unique record exists, the first deleted. Then insert a new record. Note that if the original record exists (a1, b1, c1), the new record as (a1, b2), then replace the c field is null

REPLACE INTO books SELECT 1, 'MySQL Manual' FROM books

Original Address: https: //www.cnblogs.com/sweet521/p/5730804.html

 

Guess you like

Origin www.cnblogs.com/zhangzhijian/p/10938960.html