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

Several ways to store images in MySQL database

Usually the pictures uploaded by users need to be...

Summary of JavaScript JSON.stringify() usage

Table of contents 1. Usage 1. Basic usage 2. The ...

This article takes you into the world of js data types and data structures

Table of contents 1. What is dynamic typing? 2. D...

CSS3 text animation effects

Effect html <div class="sp-container"...

Zen Coding Easy and fast HTML writing

Zen Coding It is a text editor plugin. In a text ...

A complete guide to some uncommon but useful CSS attribute operations

1. Custom text selection ::selection { background...

A detailed discussion of components in Vue

Table of contents 1. Component Registration 2. Us...

6 Ways to Elegantly Handle Objects in JavaScript

Table of contents Preface 1. Object.freeze() 2. O...

JavaScript to achieve simple provincial and municipal linkage

This article shares the specific code for JavaScr...

How to modify the port mapping of a running Docker container

Preface When docker run creates and runs a contai...

Solve the MySQL login 1045 problem under centos

Since the entire application needs to be deployed...