Search code examples
phpmysqlipunsignedsigned

Storing IP with INET_ATON - signed or unsigned integer?


Is it necessary to use an unsigned integer column to store an IP(v4) address (that has been converted with INET_ATON) in MySQL?

I spontaneous would think that it really does NOT matter, since no matter how the field is defined, the same data will be used to convert it back with INET_NTOA. So the only difference would be, that in one case you see a negative number in your db while with unsigned you would see a (totally) different positive number, while the actual data is the same in both cases, just representation differs through field definition, is that correct?


Solution

  • From the docs on INET_ATON():

    To store values generated by INET_ATON(), use an INT UNSIGNED column rather than INT, which is signed. If you use a signed column, values corresponding to IP addresses for which the first octet is greater than 127 cannot be stored correctly. See Section 11.2.6, “Out-of-Range and Overflow Handling.

    Why not just use an unsigned to guarantee compatibility? What are the downsides to using unsigned in your scenario that would make you question this?