What's the difference in int(11) and int(11) UNSIGNED?

后端 未结 8 768
鱼传尺愫
鱼传尺愫 2020-12-13 08:30

What\'s the difference in int(11) and int(11) UNSIGNED ?

8条回答
  •  -上瘾入骨i
    2020-12-13 09:11

    I think you may want to know the difference between int and int(10).

    Let's give an example for int(10) one with zerofill keyword, one not, the table likes that:

    create table tb_test_int_type(
        int_10 int(10),
        int_10_with_zf int(10) zerofill,
        unit int unsigned
    );
    

    Let's insert some data:

    insert into tb_test_int_type(int_10, int_10_with_zf, unit)
    values (123456, 123456,3147483647), (123456, 4294967291,3147483647) 
    ;
    

    Then

    select * from tb_test_int_type; 
    
    # int_10, int_10_with_zf, unit
    '123456', '0000123456', '3147483647'
    '123456', '4294967291', '3147483647'
    

    We can see that

    • with keyword zerofill, num less than 10 will fill 0, but without zerofill it won't

    • Secondly with keyword zerofill, int_10_with_zf becomes unsigned int type, if you insert a minus you will get error Out of range value for column...... But you can insert minus to int_10. Also if you insert 4294967291 to int_10 you will get error Out of range value for column.....

    Conclusion:

    1. int(X) without keyword zerofill, is equal to int range -2147483648~2147483647

    2. int(X) with keyword zerofill, the field is equal to unsigned int range 0~4294967295, if num's length is less than X it will fill 0 to the left

提交回复
热议问题