Depending on which SQL mode you are using, MySQL does one of two things when the value of an AUTO_INCREMENT
numeric column grows out of range. You get an error in either case, but for different reasons.
In strict mode MySQL rejects the out of range value, throws an invalid value error, and the INSERT
fails. In the default non-strict mode MySQL reduces the value to the highest allowed for the data type, and performs the INSERT
. But the INSERT
fails because the AUTO_INCREMENT
attribute has caused all of the possible values to be used already, and you get this error (unsigned SMALLINT
example):
MySQL said:
#1062 - Duplicate entry '65535' for key 1
For the BIGINT
example here, replace the "65535" with 18 quintillion, although it's not likely that this error has ever occurred on a production database.
But with TINYINT
s and SMALLINT
s it can happen very easily if you underestimate the possible key values (number of INSERT
s) over the lifetime of an application. Imagine you are making changes to your code and testing that your data is being inserted correctly. Suddenly your application quits working with the error above. You roll back the changes to known good code, but the error won't go away...very frustrating.