是否有必要在 MySQL 中使用unsigned 整数列来存储 IP(v4) 地址(已使用 INET_ATON
转换)?
我会自发地认为这真的无关紧要,因为无论该字段如何定义,相同的数据都将用于通过 INET_NTOA
将其转换回来。
所以唯一的区别是,在一种情况下,您在数据库中看到一个负数,而使用无符号时,您会看到一个(完全)不同的正数,而两种情况下的实际数据相同,只是表示形式因字段定义而异, 对吗?
最佳答案
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.
为什么不直接使用 unsigned 来保证兼容性呢?在您的场景中使用 unsigned 有什么缺点会让您对此产生疑问?
关于php - 使用 INET_ATON 存储 IP - 有符号或无符号整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23995360/