Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Code: 1062. Duplicate entry '1' for key 'PRIMARY'

I have a problem on this error message, when i try this:

INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`ID`, `viale`, `num_civico`,  
`data_apertura`, `data_chiusura`, `orario_apertura`, `orario_chiusura`, 
`telefono`, `mail`, `web`, `Nome-paese`, `Comune`) 
VALUES (1, 'Viale Cogel ', '120', '2012-05-21', '2012-09-30', '08:00', '23:30',
'461801243', '[email protected]', 'Bolzanoturismo.it', 'Bolzano', 'BZ')

Error Code: 1062. Duplicate entry '1' for key 'PRIMARY'

I haven't auto_increment data, PLEASE HELP me!

This is the table related, UFFICIO-INFORMAZIONI

CREATE  TABLE IF NOT EXISTS `PROGETTO`.`UFFICIO-INFORMAZIONI` (
  `ID` INT(11) NOT NULL ,
  `viale` VARCHAR(45) NULL ,
  `num_civico` VARCHAR(5) NULL ,
  `data_apertura` DATE NULL ,
  `data_chiusura` DATE NULL ,
  `orario_apertura` TIME NULL ,
  `orario_chiusura` TIME NULL ,
  `telefono` VARCHAR(15) NULL ,
  `mail` VARCHAR(100) NULL ,
  `web` VARCHAR(100) NULL ,
  `Nome-paese` VARCHAR(45) NOT NULL ,
  `Comune` CHAR(2) NOT NULL ,
  PRIMARY KEY (`ID`) ,
  INDEX `Nome_paese` (`Nome-paese` ASC) ,
  INDEX `Comune` (`Comune` ASC) ,
  CONSTRAINT `Nome_paese`
    FOREIGN KEY (`Nome-paese` )
    REFERENCES `PROGETTO`.`PAESE` (`Nome-paese` )
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `Comune`
    FOREIGN KEY (`Comune` )
    REFERENCES `PROGETTO`.`PAESE` (`Comune` )
    ON DELETE NO ACTION
    ON UPDATE CASCADE)
ENGINE = InnoDB

INSERT INTO

INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`ID`, `viale`, `num_civico`, `data_apertura`, `data_chiusura`, `orario_apertura`, `orario_chiusura`, `telefono`, `mail`, `web`, `Nome-paese`, `Comune`) VALUES (1, 'Viale Cogel ', '120', '2012-05-21', '2012-09-30', '08:00', '23:30', '461801243', '[email protected]', 'Bolzanoturismo.it', 'Bolzano', 'BZ');
    INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`ID`, `viale`, `num_civico`, `data_apertura`, `data_chiusura`, `orario_apertura`, `orario_chiusura`, `telefono`, `mail`, `web`, `Nome-paese`, `Comune`) VALUES (2, 'Via Olmo', '45', '2012-05-01', '2012-09-30', '08:00', '23:30', '393495169301', '[email protected]', 'Lechinformation.it', 'Lech', 'BZ');
    INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`ID`, `viale`, `num_civico`, `data_apertura`, `data_chiusura`, `orario_apertura`, `orario_chiusura`, `telefono`, `mail`, `web`, `Nome-paese`, `Comune`) VALUES (3, 'Via Quercia', '37', '2012-05-11', '2012-09-30', '08:00', '23:30', '393381679321', '[email protected]', 'Trentoinformaiozni.it', 'Trento', 'TN');
    INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`ID`, `viale`, `num_civico`, `data_apertura`, `data_chiusura`, `orario_apertura`, `orario_chiusura`, `telefono`, `mail`, `web`, `Nome-paese`, `Comune`) VALUES (4, 'Via Atene', '76', '2012-06-01', '2012-09-15', '08:00', '23:30', '39349361345', '[email protected]', 'SanMartino.it', 'San Martino di Castrozza', 'TN');
    INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`ID`, `viale`, `num_civico`, `data_apertura`, `data_chiusura`, `orario_apertura`, `orario_chiusura`, `telefono`, `mail`, `web`, `Nome-paese`, `Comune`) VALUES (5, 'Via Salice', '45', '2012-05-01', '2012-09-20', '08:00', '23:30', NULL, '[email protected]', 'Pejoturismo.it', 'Pejo', 'TN');
    INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`ID`, `viale`, `num_civico`, `data_apertura`, `data_chiusura`, `orario_apertura`, `orario_chiusura`, `telefono`, `mail`, `web`, `Nome-paese`, `Comune`) VALUES (6, 'Piazza Sempreverde', '34', '2012-05-15', '2012-09-15', '08:00', '23:30', '392516789', '[email protected]', 'Ortisei.it', 'Ortisei', 'BZ');
like image 901
FrancescoN Avatar asked Jan 31 '13 14:01

FrancescoN


People also ask

How do I fix error 1062?

1062 - Duplicate Entry To solve this, Set the primary key column as AUTO_INCREMENT . And when you are trying to insert a new row, ignore the primary key column or insert NULL value to primary key.

What is duplicate entry for key primary?

When creating a primary key or unique constraint after loading the data, you can get a “Duplicate entry for key 'PRIMARY'” error. If the data in the source database is valid and there are no any duplicates you should check which collation is used in your MySQL database.

What is the meaning of error 1062?

If you have received this warning on your PC, it means that there was a malfunction in your system operation. Error code "Error 1062" is one of the issues that users may get as a result of incorrect or failed installation or uninstallation of software that may have left invalid entries in system elements.

How do I ignore duplicate entries in MySQL?

Use the INSERT IGNORE command rather than the INSERT command. If a record doesn't duplicate an existing record, then MySQL inserts it as usual. If the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error.


2 Answers

The main reason why the error has been generated is because there is already an existing value of 1 for the column ID in which you define it as PRIMARY KEY (values are unique) in the table you are inserting.

Why not set the column ID as AUTO_INCREMENT?

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

and when you are inserting record, you can now skip the column ID

INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`viale`, `num_civico`, ...) 
VALUES ('Viale Cogel ', '120', ...)
like image 55
John Woo Avatar answered Oct 20 '22 22:10

John Woo


If you are using PHPMyAdmin You can be solved this issue by doing this:

CAUTION: Don't use this solution if you want to maintain existing records in your table.

Step 1: Select database export method to custom:

enter image description here

Step 2: Please make sure to check truncate table before insert in data creation options:

enter image description here

Now you are able to import this database successfully.

like image 22
Indrasinh Bihola Avatar answered Oct 20 '22 21:10

Indrasinh Bihola