Primary key repeat in MySQL database 3 Solution

Source: Internet
Author: User

The following article describes the actual processing scheme for repeated primary keys in the MySQL database. We all know that when the data is inserted into the data table, when the unique key (unique key and primary key) is repeated, a duplicate key error occurs.

There are three solutions to this problem: the following data structure is an example

 
 
  1. MySQL> use test;  
  2. MySQL> create table `user` (`userid` int(11) DEFAULT NULL, `username` varchar(255) NOT NULL DEFAULT '');  

Add the userid column primary key

 
 
  1. MySQL> alter table `user` add primary key `userid` (`userid`); 

Insert data

 
 
  1. MySQL> insert into `user` values (1, 'eric'), (2, 'jesus');  

Now I want to insert or edit a record with userid 1, but I don't know if it already exists.

Actual solution 1 when the primary key in the MySQL database is repeated. Delete the key before inserting it.

 
 
  1. MySQL> delete from user where userid = 1;  
  2. MySQL> insert into user values (1, 'xxxxx') ;  

The actual solution 2 when the primary key in the MySQL database is repeated. Use replace

 
 
  1. MySQL> replace into user values (1, 'newvalue');  

In this case, the logic is like this. MySQL first checks whether the record exists. If yes, it first deletes the record and then inserts the record independently. so you can see that affected rows is two after this statement is executed (the premise is that there is only one record of userid 1 in your data table)

Actual solution 3 when the primary key in the MySQL database is repeated

 
 
  1. insert into ... on duplicate key update  
  2. MySQL> insert into user1 values (1, 'newvalueagain') on duplicate key update user1.username = VALUES(username);  

The affected rows of this statement is also 2.

Of course, another way to handle this problem is to use php directly,

Select first. If no result is found, insert; otherwise, update.

You can also update it first and find that affected rows is 0, then insert.

But obviously neither of these methods directly handed over the work to MySQL for high processing efficiency

Related Article

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.