开发者

How many characters does an integer (rendered as a string) consume in MySQL?

I have been told that the maximum value for an integer in mysql is approximately 4 billion but this seems to use 10 cha开发者_运维知识库racters. However when I specify the number of characters for integer I use 11 characters. What happens to the extra character?


It's 11 characters. The extra digit is for the - in negative numbers. Don't believe me, you can count here.

Edit

I wrote that answer before lunch hour, and it has bothered me the whole time that I didn't include anything on most significant bit, because it really is very relevant in this case. So, I'm including Wikipedia's treatment because I don't feel like re-writing the wheel.


An integer is saved as a 32 bit number. That means, it can store 2^32 = 4,294,967,296 different values. Now, if you chose to use a singed integer (which you do by default), you will be able to represent numbers in the interval of [-2147483648, 2147483647] in that 32 bits.

If you now convert the lowest possible number to a string, you will notice it has 11 chars including the minus.

Generally, you should try to work with numbers as numbers instead of strings. Although both might look similar, they is a major difference between a string containing only numeric chars and an actual numeric value.


It is actually 11 characters. Just in case if you exceed the max limit not just for integer but for any fields the values will be truncated.


It depends on mode which you use in mysql ans transactional/non-transactional tables. By default mysql will truncate number to the closest. If strict mode so you query will fail. See more info http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜