Home > Database > Mysql Tutorial > How to Fix MySQL Error 1062: Duplicate Entry for Key \'PRIMARY\'?

How to Fix MySQL Error 1062: Duplicate Entry for Key \'PRIMARY\'?

Patricia Arquette
Release: 2024-10-31 21:03:01
Original
528 people have browsed it

How to Fix MySQL Error 1062: Duplicate Entry for Key 'PRIMARY'?

MySQL Error 1062: Resolving "Duplicate Entry for Key 'PRIMARY'"

An "Error Code: 1062. Duplicate entry '1' for key 'PRIMARY'" occurs when attempting to insert duplicate values into a column marked as a primary key. This ensures data integrity by preventing rows with identical primary key values.

In the provided example, the UFFICIO-INFORMAZIONI table has an ID column defined as the primary key. When attempting to insert a new record with an ID of 1, the error occurs because that value already exists in the table.

Solution:

The primary key constraint requires unique values in the specified column. To resolve the error, you can make the ID column auto-increment by replacing the table definition with the following:

CREATE TABLE IF NOT EXISTS `PROGETTO`.`UFFICIO-INFORMAZIONI` (
  `ID` INT(11) NOT NULL AUTO_INCREMENT,
  `viale` VARCHAR(45) NULL,
  ...
Copy after login

When inserting records, you can now omit the ID column, allowing the database to automatically generate unique values:

INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`viale`, `num_civico`, ...)
VALUES ('Viale Cogel ', '120', ...)
Copy after login

The above is the detailed content of How to Fix MySQL Error 1062: Duplicate Entry for Key \'PRIMARY\'?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template