Why can't I insert 10 digits when my column is INT(10)

后端 未结 6 1732
孤独总比滥情好
孤独总比滥情好 2021-01-17 05:24

I have this column that\'s INT(10) ZEROFILL NOT NULL DEFAULT \'0000000000\',

But when I insert something like 9100000010, I get 42949

相关标签:
6条回答
  • 2021-01-17 05:57

    I belive you get int max value. INT(10) is ment for number padding not for limiting number of characters.

    MySQL will try to pad these values with spaces/zeroes when returning them.

    0 讨论(0)
  • 2021-01-17 06:08

    Int has maximum value range :

    INT 4   -2147483648 2147483647
            0   4294967295 (unsigned )
    

    hence, you are getting the maximum value due to the overflow. use bigint instead, which has 8 bytes and you shall be fine.

    http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html#integer-types

    BIGINT  8   -9223372036854775808    9223372036854775807
              0                 18446744073709551615
    
    0 讨论(0)
  • 2021-01-17 06:10

    you reached the maximum for int. The int maximum value range for int in mysql is 4294967295 (unsigned). So you have to use BIGINT. You can get more informations about this here: Mysql Numeric types

    Hope this helps

    Kind Regards

    0 讨论(0)
  • 2021-01-17 06:11

    It should have allowed 10 digits there, right?

    NO, size of INT and integer types in mySQL is to set the display width when field has ZEROFILL specified.

    for example if the value you inserted is 123456 then when you query it, it will displays as 0000123456 , it is just the display width and doesn't affect the value.

    so the value range is determined by the datatype itself, not the display width. to store greater values, you should change the datatype to a suitable one, like BIGINT.

    from mySQL reference:

    INT : A normal-size integer. The signed range is -2147483648 to 2147483647. The unsigned range is 0 to 4294967295.

    0 讨论(0)
  • 2021-01-17 06:15

    Largest value possible in INT field in MySQL is 2147483647, your value 9100000010 is greater than that. That is why it is overflowing to 4294967295 which is Max for Unsigned Integer.

    11.1.4.1. Integer Types (Exact Value)

    INT    4    -2147483648 2147483647
    

    Use BIGINT if you want to store larger number, which ranges from:

    BIGINT  8   -9223372036854775808    9223372036854775807
    
    0 讨论(0)
  • 2021-01-17 06:18

    In MySQL, INT(10) does not mean that values are limited to 10-character values. It only means that MySQL will try to pad these values with spaces/zeroes when returning them.

    The numeric range of any signed INT including INT(10), INT(5) or any other INT(n) is: -2,147,483,648 ... 2,147,483,647, which is 10 digits at most.

    0 讨论(0)
提交回复
热议问题