I was working on a project recently and fiddling around with sql, when I accidentally forgot to put the () brackets in int(n), however it didn't result in an error and the table was created. This 'intn' datatype also behaves a lot different that int(n), int1/int2/int3/int4 all accept integers less than 128/32768/8388608/2147483648 which is 2^(8n-1), n being the number after int. On the other hand no matter what integer I give inside int(n) it always accepts up to 2147483647 while it should accept only n digits. Can someone pls explain the weird behavior of both these datatypes in layman's terms (I have just started learning SQL). Can you also tell me the correct syntax that I should use while using integer datatype.
CodePudding user response:
These datatypes are aliases (see Using Data Types from Other Database Engines):
datatype | is alias of |
---|---|
INT1 | TINYINT |
INT2 | SMALLINT |
INT3 | MEDIUMINT |
INT4 | INT |
INT8 | BIGINT |
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=2c5545953ef496acf7893c28c198c3cb
Another variants (for example, INT5
) will cause syntax error.