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

后端 未结 8 2039
既然无缘
既然无缘 2020-12-01 03:55

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

INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`ID`, `viale`, `num_civico`,  
`data_apertura`, `data         


        
相关标签:
8条回答
  • 2020-12-01 04:06

    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;
    
    0 讨论(0)
  • 2020-12-01 04:07

    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.

    0 讨论(0)
  • 2020-12-01 04:13

    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.

    0 讨论(0)
  • 2020-12-01 04:24

    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

    0 讨论(0)
  • 2020-12-01 04:25

    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!

    0 讨论(0)
  • 2020-12-01 04:28

    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';
    
    0 讨论(0)
提交回复
热议问题