The method of judging data duplication in MySQL

Source: Internet
Author: User

The practical application of my work. When collecting data, it is not necessary to repeat it after collection. At the same time, multiple instances are required to operate simultaneously to ensure the continuity of data acquisition. So summed up, made the following small experiment. The core code is as follows:

Table structure: only 3 fields

Id,name,password

SQL code

CREATE TABLE ' TT ' (  
  ' Id ' int (one) not null auto_increment,  
  ' name ' varchar (255) DEFAULT null,  
  ' password ' varcha R (255) default NULL,  
  PRIMARY KEY (' Id ')  
) engine=innodb default Charset=utf8;

SQL statement

1) Recommended

SQL code

Insert ignore into TT (Name,password) VALUES (' PHL ', ' 123 ')

2 is not recommended, because select will lock the Select table when insert

SQL code

INSERT INTO TT (name,password) Select ' PHL ', ' 123 ' to dual where NOT EXISTS (SELECT * from TT where name= ' PHL ' and password = ' 123 ')

The meaning of this SQL statement is that if the inserted data name= ' PHL ', password= ' 123 ' does not exist, the insertion is performed;

Add:

Method 1, there is no filter. It is because there is no joint master building of name and password;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.