Skip to content
Advertisement

mysql Duplicate entry error for primary key with similar values

This is how i created my table, i have composite key columns.

now, im trying to insert this two values that are the same except for the last character ‘t’:

value1 = testbtc:testusd

value2 = testbtc:testusdt

output error:

it just makes no sense, they are not duplicated, is something missing on my table?

Advertisement

Answer

I suspect that the version of MySql you are using is 5.5.x, in which case when you try to insert a value for a VARCHAR column that is longer than its defined length, in your case longer than 15, the value is truncated to the max length.

So, because the length of 'testbtc:testusdt' is 16, it is truncated to 'testbtc:testusd' (the first 15 chars) and the result is the dupicate.

See a simplified demo.

The best you can do is increase the size of the column:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement