In the MySQL database, these 4 ways can avoid repeated insertion of data!

 

The most common way is to set a primary key or a unique index for a field. When inserting duplicate data, an error is thrown and the program terminates, but this will cause trouble for subsequent processing, so special processing is required for the insert statement, and try to avoid or ignore it Abnormal, let me briefly introduce it, and interested friends can try it:

Here, for the convenience of demonstration, I created a new user test table, which mainly has four fields: id, username, sex, and address. The primary key is id (self-incrementing), and a unique index is set for the username field:

01 insert ignore into

That is, when inserting data, if the data exists, ignore this insertion. The prerequisite is that the inserted data field is set with a primary key or a unique index. The test SQL statement is as follows. When inserting this piece of data, the MySQL database will first retrieve the existing data (also is the idx_username index), if it exists, ignore this insertion, if it does not exist, insert the data normally:

02 on duplicate key update

That is, when inserting data, if the data exists, the update operation is performed. The prerequisite is the same as above, and the inserted data field is also set with a primary key or a unique index. The test SQL statement is as follows. When inserting this record, the MySQL database will first retrieve the existing data ( idx_username index), if it exists, perform an update operation, if it does not exist, insert it directly:

03 replace into

That is, when inserting data, if the data exists, delete it and insert it again. The prerequisite is the same as above. The inserted data field needs to be set with a primary key or a unique index. The test SQL statement is as follows. When inserting this record, the MySQL database will first retrieve the existing data (idx_username index), if it exists, delete the old data first, and then insert it, if it does not exist, insert it directly:

04 insert if not exists

That is, insert into ... select ... where not exist ..., this method is suitable for inserting data fields that do not have a primary key or a unique index. When inserting a piece of data, first determine whether the data exists in the MySQL database. If it does not exist, Then insert it normally, if it exists, ignore it: At present, let’s share these 4 ways for MySQL to handle duplicate data. The first 3 ways are suitable for fields with primary keys or unique indexes. The last way does not have this limitation, as long as you are familiar with it Just look at the process of use, and you can quickly master it. There are also relevant materials and tutorials on the Internet. The introduction is very detailed. If you are interested, you can search it.

 

Guess you like

Origin blog.csdn.net/qq_35956444/article/details/109185532