[Solved] How to solve MySQL error code: 1062 duplicate entry?

Error Message:



Error Code: 1062. Duplicate entry ‘%s’ for key %d


Example:


Error Code: 1062. Duplicate entry ‘1’ for key ‘PRIMARY’

Possible Reason:


Case 1: Duplicate value.


The data you are trying to insert is already present in the column primary key. The primary key column is unique and it will not accept the duplicate entry.

Case 2: Unique data field.


You are trying to add a column to an existing table which contains data and set it as unique.

Case 3: Data type –upper limit.


The auto_increment field reached its maximum range.

MySQL NUMERICAL DATA TYPE - STORAGE & RANGE

Solution:


Case 1: Duplicate value.


Set the primary key column as AUTO_INCREMENT.

ALTER TABLE table_name ADD column_name INT NOT NULL AUTO_INCREMENT PRIMARY KEY;

Now, when you are trying to insert values, ignore the primary key column. Also you can insert NULL value to primary key column to generate sequence number. If no value specified MySQL will assign sequence number automatically.





Case 2: Unique data field.


Create the new column without the assigning it as unique field, then insert the data and now set it as unique field now. It will work now!!!

Case 3: Data type-upper limit.


When the data type reached its upper limit, for example, if you were assigned your primary key column as TINYINT, once the last record is with the id 127, when you insert a new record the id should be 128. But 128 is out of range for TINYINT so MySQL reduce it inside the valid range and tries to insert it with the id 127, therefore it produces the duplicate key error.

In order to solve this, you can alter the index field, setting it into signed / unsigned INT/ BIGINT depending on the requirement, so that the maximum range will increase. You can do that by using the following command:

ALTER TABLE table_name MODIFY column_name INT UNSIGNED NOT NULL AUTO_INCREMENT;

You can use the following function to retrieve the most recently automatically generated AUTO_INCREMENT value:

mysql> SELECT LAST_INSERT_ID();

Final workaround:




After applying all the above mentioned solutions and still if you are facing this error code: 1062 Duplicate entry error, you can try the following workaround.

Step 1: Backup database:

You can backup your database by using following command:
mysqldump database_name > database_name.sql

Step 2: Drop and recreate database: 

Drop the database using the following command:
DROP DATABASE database_name;
Create the database using the following command:
CREATE DATABASE database_name;

Step 3: Import database:

You can import your database by using following command:
mysql database_name < database_name.sql;
After applying this workaround, the duplicate entry error will be solved. I hope this post will help you to understand and solve the MySQL Error code: 1062. Duplicate entry error. If you still facing this issue, you can contact me through the contact me page. I can help you to solve this issue.





20 comments:

  1. The article has genuinely peaks my interest. I’m planning to marker your website and maintain checking for brand new details. I actually idolized reading your journal, valuable data.

    ReplyDelete
  2. we have sell some merchandise of rotate custom boxes.it's far absolutely useful and completely low fee allure visits this web page thanks and appeal component this say thinking of your links. Facial Treatment

    ReplyDelete
  3. I never comment on blogs but your article is so best that I never stop myself to say something about it. You’re amazing Man, I like it Database Errors... Keep it up

    ReplyDelete
  4. Awesome and interesting article. Great things you've always shared with us. Thanks. Just continue composing this kind of post. entreprise informatique

    ReplyDelete
  5. That is the place IT specialists come in and it is actually why it tends to be so natural to discover lucrative chances in the event that you have the best possible measure of innovation based information.
    information technology

    ReplyDelete
  6. Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. skytechosting.com

    ReplyDelete
  7. Thanks for your insight for your fantastic posting. I’m glad I have taken the time to see this. ternopil national medical university

    ReplyDelete
  8. I recommend that you ask for a free trial session. cursos de ti

    ReplyDelete
  9. This is awesome post. I really like it. I will also want to know APK Game Killer is good or not?

    ReplyDelete
  10. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great work water softeners

    ReplyDelete
  11. Your writers are your resources in providing quality articles to your clients. Obviously, a badly written article reflects the type of service that you offer. Helphub

    ReplyDelete
  12. I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites blog site list and will be checking back soon. Please check out my site as well and let me know what you think. fue hair transplant in lucknow

    ReplyDelete
  13. I was reading your article and wondered if you had considered creating an ebook on this subject. Your writing would sell it fast. You have a lot of writing talent. techno tech blog

    ReplyDelete
  14. Now, the question would be how someone will analyze data in the aforementioned manner and give some useful results. 360DigiTMG data science training in hyderabad

    ReplyDelete
  15. This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, data sciecne course in hyderabad

    ReplyDelete
  16. This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thank you For Sharing
    https://socialprachar.com/data-science/

    ReplyDelete