Understanding Numeric Column Transformation in MySQL
MySQL often transforms long integers to unexpected values when inserted into shorter columns. Instead of truncating, MySQL adjusts the stored value. This behavior is attributed to the integer overflow mechanism.
For example, consider a column named some_number with a length of 10. When a number exceeding this length (e.g., 715988985123857) is inserted, it is transformed into 2147483647.
The Overflow Mechanism
According to the MySQL documentation, integer overflow occurs when the result of a calculation exceeds the maximum allowed value for the data type. In this case, the maximum value for a 10-bit integer is 2147483647.
Formula for Calculated Result
The transformation follows a specific formula:
Result = Number % (2^Bits) - 1
where:
In our example, Number is 715988985123857 and Bits is 10, resulting in:
2147483647
BigInt vs. Int
To avoid integer overflow, use a bigint data type for larger integers. BigInt can store values up to 2^63-1, preventing truncation or transformation issues.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3