Three ways to prevent MySQL from inserting duplicate data

Three ways to prevent MySQL from inserting duplicate data

Create a new table

CREATE TABLE `person` (
 `id` int NOT NULL COMMENT 'Primary key',
 `name` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'Name',
 `age` int NULL DEFAULT NULL COMMENT 'Age',
 `address` varchar(512) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'Address',
 PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;

Add three pieces of data as follows:

We can avoid it according to the insertion method:

1. insert ignore

insert ignore will automatically ignore the data that already exists in the database (based on the primary key or unique index). If there is no data, insert the data; if there is data, skip inserting this data.

--Insert SQL as follows:
insert ignore into person (id,name,age,address) values(3,'那谁',23,'赣苏省'),(4,'我天',25,'浙江省');

If you check the database again, you will find that only data with id 4 is inserted. Since data with id 3 already exists in the database, it is ignored.

2. replace into

replace into first tries to insert data into the table. 1. If it is found that this row of data already exists in the table (determined by the primary key or unique index), delete this row of data first, and then insert the new data. 2. Otherwise, insert the new data directly.

--Insert SQL as follows:
replace into person (id,name,age,address) values(3,'那谁',23,'赣苏省'),(4,'我天',25,'浙江省');

First, we restore the data in the table, and then after the insert operation, we find that the data with id 3 has changed and the data with id 4 has been added.

3. insert on duplicate key update

insert on duplicate key update If on duplicate key update + field update is specified at the end of the insert into statement, the information will be updated according to the description of the subsequent field update when duplicate data appears (determined by the primary key or unique index).

--Insert SQL as follows:
insert into person (id,name,age,address) values(3,'那谁',23,'赣苏省') on duplicate key update name='那谁', age=23, address='赣苏省';

First, we restored the data in the table, and then when performing the insert operation, we found that the data with id 3 had changed, so we performed an update operation.

We can choose the method according to our business needs.

The above are the details of three methods to prevent MySQL from inserting data repeatedly. For more information on preventing MySQL from inserting data repeatedly, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Three ways to avoid duplicate insertion of data in MySql
  • MySQL uses UNIQUE to implement non-duplicate data insertion
  • How to prevent MySQL from inserting duplicate records
  • Several ways to avoid duplicate insertion of records in MySql
  • 4 ways to avoid duplicate insertion of data in Mysql

<<:  JavaScript to achieve text expansion and collapse effect

>>:  Common ways to optimize Docker image size

Recommend

Common usage of hook in react

Table of contents 1. What is a hook? 2. Why does ...

Implementation of CSS linear gradient concave rectangle transition effect

This article discusses the difficulties and ideas...

Multiple ways to change the SELECT options in an HTML drop-down box

After the form is submitted, the returned HTML pag...

Windows Server 2008 Tutorial on Monitoring Server Performance

Next, we will learn how to monitor server perform...

CentOS 7.6 installation of MySQL 5.7 GA version tutorial diagram

Table of contents Environment Preparation Environ...

The difference between clientWidth, offsetWidth, scrollWidth in JavaScript

1. Concept They are all attributes of Element, in...

Detailed explanation of formatting numbers in MySQL

Recently, due to work needs, I need to format num...

How to find the my.ini configuration file in MySQL 5.6 under Windows

Make a note so you can come back and check it lat...

A pitfall and solution of using fileReader

Table of contents A pitfall about fileReader File...

MySQL 5.7.31 64-bit free installation version tutorial diagram

1. Download Download address: https://dev.mysql.c...

The best solution for resetting the root password of MySQL 8.0.23

This method was edited on February 7, 2021. The v...

Native JS to achieve book flipping effects

This article shares with you a book flipping effe...