Types in MySQL: BigInt(20) vs Int(20)

前端 未结 6 1350
野性不改
野性不改 2020-11-22 16:15

I was wondering what the difference between BigInt, MediumInt, and Int are... it would seem obvious that they would allow for larger n

6条回答
  •  心在旅途
    2020-11-22 16:32

    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

提交回复
热议问题