Search code examples
mysqlsqlindexingsql-insertinnodb

MySQL Insert Failing with Duplicate Entry on Index


I am trying to do an insert statement. I have an unique index setup that combines phone_number and niche.

INSERT INTO CV_YP.table (phone_number,website,niche) VALUES (1231231234, 'website.com', 'fitness')

The return value says I have a duplicate entry but the phone number is different then the one I just tried to insert.

ERROR 1062 (23000) at line 1: Duplicate entry '2147483647-fitness' for key 'idx_table_phone_number_niche'

I can't figure out why MySQL thinks my insert is related to the index duplication it returns.


Solution

  • i think if you run this query

    SELECT * FROM CV_YP.table WHERE phone_number = '2147483647' AND niche = 'fitness';
    

    you will find more than 1 row, so I think its a violation of the unique index, so when the engine tries to check for uniqueness on the new data this error fire

    update

    I think your phone number isn't 1231231234 right? and it's a number greater than 2147483647 and you use phone_number data type as int

    If that true I think you need to change the data type of the phone_number column because the max value of int is 2147483647 so when you insert the new number the engine sees it as the max value which is 2147483647 and try to insert it.