Several methods to prevent Mysql from inserting duplicate data

insert image description here

1 Introduction

When operating the database at ordinary times, sometimes you need to insert some data into the database. At this time, you need to use the insert statement of the database, but when inserting data into the database, you cannot insert blindly, because blind insertion may cause data duplication , a waste of database resources, so in general there will be a so-called primary key in the database, requiring that the selected primary key field or combination cannot be repeated, this can avoid the generation of some dirty data, but at the same time it brings some unfriendly operations, For example, when inserting a piece of data, the value of the primary key field already exists. At this time, if you directly use the insert into statement to insert, an error will be reported, indicating that the value of the field already exists, but sometimes we don’t want to see it Statement error, so this brings some bad operating experience, in order to avoid this kind of error, the best way is toAvoid duplicate primary keys when inserting. If it is unavoidable. Using the following grammars can also achieve the effect of not reporting errors.

grammar Remarks (primary key exists)
insert ignore into If the database already exists, this operation will not be performed. If it does not exist in the database, insert this data into the database.
insert into not exists If the database already exists, this operation will not be performed. If it does not exist in the database, insert this data into the database.
insert into on duplicate key update If the database already exists, firstrenew, and perform this action. If it does not exist in the database, insert this data into the database.
replace into If it already exists in the database, directlydelete, and perform this action. If it does not exist in the database, insert this data into the database.

2. Case

test database

-- auto-generated definition
create table if not exists  t_admin
(
    id        int auto_increment
        primary key,
    adminName varchar(50) null,
    passWord  varchar(50) null
);
INSERT INTO t_admin (id, adminName, passWord) VALUES (1, 'admin', 'admin');
INSERT INTO t_admin (id, adminName, passWord) VALUES (2, 'xiaoqian', '123456');

2.1 、insert ignore into

explain:

​ There is a primary key, if the inserted dataprimary key valueIf the database already exists, this operation will not be performed. If it does not exist in the database, insert this data into the database.

insert ignore into t_admin value (2,'itbestboy','123456');

insert image description here

It can be seen that the insertion is not successful at this time, because the primary key value already exists, and the insertion operation is ignored, but no error is reported, which improves the friendliness. If you follow the usual operation, you will definitely report an error.

report error

insert image description here

2.2、 insert into not exists

explain:

There is a primary key, if the inserted dataprimary key valueIf the database already exists, this operation will not be performed. If it does not exist in the database, insert this data into the database.

insert into t_admin select 2,'itbestboy','123456' from t_admin where not exists(select id from t_admin where id=2);

insert image description here

It can be seen that the insertion is not successful at this time, because the primary key value already exists, the insertion operation is ignored, but no error is reported, which improves the friendliness.

2.3、insert into on duplicate key update

explain:

There is a primary key, if the inserted dataprimary key valueIf the database already exists, firstrenew, and perform this action. If it does not exist in the database, the data will be inserted into the database this time, and this operation is similar to an update statement.

-- 主键值在数据库中已经存在,此时会先执行update 语句,然后在插入,
insert  into t_admin value (2,'itbestboy','123456') on duplicate key update adminName='itbestboy', passWord='123456';
-- 注;当update语句后跟的值与value中的值不一致时,会按着update语句后的值进行入库操作。

insert image description here

If the primary key value does not exist in the database, it will be inserted directly, and the update statement will fail

 insert  into t_admin value (3,'itbestboy','123456') on duplicate key update adminName='tboy', passWord='1456';

insert image description here

2.4、replace into

There is a primary key, if the inserted dataprimary key valueIf the database already exists, firstdelete, and perform this action. If it does not exist in the database, the data will be inserted into the database this time, and this operation is similar to an update statement.

-- 主键值在数据库中已经存在,此时会先删除这条记录,然后重新插入数据库中
replace into t_admin (id, adminName, passWord) VALUES (3, 'boy', '123456');

insert image description here
insert image description here

If the primary key value does not exist in the database, it will be inserted directly, and the delete statement will fail

 insert  into t_admin value (3,'itbestboy','123456') on duplicate key update adminName='tboy', passWord='1456';

insert image description here
insert image description here

3. Summary

Although the above method can avoid inserting and reporting errors when the primary key value is repeated, sometimes we need to see the corresponding error reporting information, which is more conducive to debugging, so how to use it and when to use it depends on the situation.
insert image description here

Guess you like

Origin blog.csdn.net/qq_40520912/article/details/128622896