MySQL: Best way to store MAC addresses?

这一生的挚爱 提交于 2019-11-29 11:49:29

问题


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?


回答1:


use bigint unsigned (8 bytes) then you can:

select hex(mac_addr) from log;

and

insert into log (mac_addr) values (x'000CF15698AD');



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/4514547/mysql-best-way-to-store-mac-addresses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!