35

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', 'informazioni@bolzano.it', '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', 'informazioni@bolzano.it', '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', 'informazioni@lech.it', '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', 'info@trento.it', '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', 'info@sanmartinodicastrozza.it', '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, 'info@pejo.it', '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', 'info@ortisei.it', 'Ortisei.it', 'Ortisei', 'BZ');
FrancescoN
  • 1,766
  • 7
  • 28
  • 40
  • So, there's already a record in the table having `ID=1`. Since `ID` is the `PRIMARY KEY`, there cannot be multiple records with the same value. What would you like this operation to do? Ignore the new insertion? Update the existing record? Replace the existing record? – eggyal Jan 31 '13 at 14:43
  • Are you sure the errors refer to the primary field of this table? Sould it refer to other ID from other tables? – Alepac Jan 31 '13 at 14:50
  • 0 14:55:20 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', 'informazioni@bolzano.it', 'Bolzanoturismo.it', 'Bolzano', 'BZ') Error Code: 1062. Duplicate entry '1' for key 'PRIMARY' – FrancescoN Jan 31 '13 at 14:55
  • Probably you have an issue with foreign keys, I'm trying to replicate your problem with [sql fiddle] (http://sqlfiddle.com/#!2/c6fb9) but without success. Could you give as the complete db schema? – Alepac Jan 31 '13 at 15:16
  • http://dl.dropbox.com/u/60796322/ultimateProgetto.sql – FrancescoN Jan 31 '13 at 15:28

8 Answers8

32

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', ...)
John Woo
  • 238,432
  • 61
  • 456
  • 464
  • 2
    i've just added ALL my INSERT INTO ; there are no duplicated PK.. that's because i can't understand what happened – FrancescoN Jan 31 '13 at 14:46
  • You might have run the inserts more than once. – Adder Jan 31 '13 at 14:56
  • I've droped the schema and re-created for the 2 time... i always got the same error, as shown above. EDIT:: the auto_increment would be an option, but it would work also without it... – FrancescoN Jan 31 '13 at 15:01
  • Are there maybe inserts in the script you use to recreate the table? – Adder Jan 31 '13 at 15:20
  • In the script, there is first the CREATE TABLE, then INSERT INTO. These are shown above, and there is not other query/record for that table (UFFICIO-INFORMAZIONI) – FrancescoN Jan 31 '13 at 15:30
  • EHM... SOLVED: while i was editing the mysql file i've done a cut&paste over another INSERT INTO.... so yes obviously there were at least 2 ID with the same number. Anyway know i know of this auto_increment way, thanks you! – FrancescoN Jan 31 '13 at 22:58
  • https://www.youtube.com/watch?v=UrYLYV7WSHM In this video "Cust_ID" contains "wilson44" two times... – Lealo Aug 17 '17 at 16:33
20

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.

Indrasinh Bihola
  • 1,948
  • 3
  • 20
  • 25
  • This is one of good and time saving solution when using phpmyadmin or terminal to import database from another. – mapmalith May 30 '18 at 07:06
  • When trying to create new indexes on a huge table, I've resorted to renaming, creating a new table from the old, adding my indexes and then inserting from the old table. Was getting duplicate primary key errors even on the empty new table. Truncating it before copying data fixed this. – OK sure Nov 26 '18 at 19:58
7

If you are trying to populate a table from a SQL dump, make sure that the table listed in the "INSERT INTO" statements of the dump is the same one you are trying to populate. Opening "MyTable" and importing with a SQL dump will throw exactly that kind of error if the dump is trying to put entries into "MyOtherTable", which may already have entries.

CodeOwl
  • 624
  • 8
  • 21
6

If you have a new database and you make a fresh clean import, the problem may come from inserting data that contains a '0' incrementation and this would transform to '1' with AUTO_INCREMENT and cause this error.

My solution was to use in the sql import file.

SET SESSION sql_mode='NO_AUTO_VALUE_ON_ZERO';
Paul Roub
  • 35,100
  • 27
  • 72
  • 83
Andrada
  • 91
  • 1
  • 5
2

The problem is related with your file - you are trying to create a DB using a copy - at the top of your file you will find something like this:

CREATE DATABASE IF NOT EXISTS *THE_NAME_OF_YOUR_DB* DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci; USE *THE_NAME_OF_YOUR_DB*;

and I'm sure that you already have a DB with this name - IN THE SAME SERVER - please check. Just change the name OR ERASE THIS LINE!

bito_
  • 149
  • 1
  • 10
2

When I get this kind of error I had to update the data type by a notch. For Example, if I have it as "tiny int" change it to "small int" ~ Nita

Nita
  • 185
  • 3
  • 8
  • 20
2

Also check your triggers.

Encountered this with a history table trigger which tried to insert the main table id into the history table id instead of the correct hist-table.source_id column.

The update statement did not touch the id column at all so took some time to find:

UPDATE source_table SET status = 0;

The trigger tried to do something similar to this:

FOR EACH ROW
BEGIN
    INSERT INTO `history_table` (`action`,`id`,`status`,`time_created`)
    VALUES('update', NEW.id, NEW.status, NEW.time_created);
END;

Was corrected to something like this:

FOR EACH ROW
BEGIN
    INSERT INTO `history_table` (`action`,`source_id`,`status`,`time_created`)
    VALUES('update', NEW.id, NEW.status, NEW.time_created);
END;
sastorsl
  • 1,583
  • 11
  • 16
1

I just encountered the same issue but here it seemed to come from the fact that I declared the ID-column to be UNsigned and that in combination with an ID-value of '0' (zero) caused the import to fail...

So by changing the value of every ID (PK-column) that I'd declared '0' and every corresponding FK to the new value, my issue was solved.