MySQL:存储MAC地址的最佳方式?
在 MySQL 数据库中存储 MAC 地址的最佳字段类型是什么?另外,它应该使用特定的分隔符(冒号或破折号)存储还是应该不使用分隔符存储?
What's the best field type to use to store MAC addresses in a MySQL database? Also, should it be stored with a certain separator (colon or dash) or should it be stored without a separator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 bigint unsigned (8 bytes) 那么你可以:
并且
use bigint unsigned (8 bytes) then you can:
and
看看这里。一般来说,CHAR(12) 很好,但细节取决于您的需要。或者只使用 PostgreSQL,它有一个内置的 macaddr 类型。
Take a look here. Generally, CHAR(12) is good but details depend on your needs. Or just use PostgreSQL, which has a built-in macaddr type.
鉴于 MySQL 不支持用户定义的扩展,它似乎也不支持任意扩展或插件(仅适用于存储引擎),您最好的选择是将其存储为 CHAR(17) 作为标准表示法中的 ASCII 字符串(例如,带冒号分隔符),或一个小 BLOB 并直接存储字节。然而,字符串表示法对于大多数应用程序来说会更加友好。
您可能希望将其与验证其是否为 MAC 地址的触发器配对,因为这确实是在不支持自定义类型的情况下强制数据有效性的唯一方法。
Given that MySQL does not support user defined extensions, nor does it seem to support arbitrary extensions or plugins (just for storage engines), your best bet is to store it as a CHAR(17) as an ASCII string in standard notation (e.g., with colon separators), or a small BLOB and store the bytes directly. However, the string notation is going to be more friendly for most applications.
You may want to pair it with a trigger that validates that it is a MAC address, since that is really the only way to enforce data validity without support for custom types.