使用十六进制作为数据库 ID 可以吗?
我刚刚收到一个对我来说看起来有点奇怪的数据库模式。该数据库位于 Soap Web 服务后面,我注意到所有表 ID 都是十六进制格式的字符串,
例如:0x1D283F
我必须将数据复制到 MySQL 数据库中。我从来没有使用过十六进制作为 ID,所以我不知道这是一个好主意/坏主意,还是无关紧要。我猜自动增量在这里不起作用,这让我认为这将是一个坏主意。
我可以将它们转换为整数,或者保持原样,但这有什么影响。
I've just been handed a database schema that looks a little odd to me. The database sits behind a Soap web service and I've noticed that all the table ID's are strings in hex format
eg: 0x1D283F
I'm going to have to duplicate the data into a MySQL database. I've never used Hex's as IDs, so I've no idea wether it's a good idea / bad idea or doesn't matter either way. I'm guessing Auto Increment wouldn't work here, which makes me think this is going to be a bad idea.
I could convert them to integers, or leave them as they are, but what are the implications.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,数字是“十六进制”还是“十进制”只是外部表示的问题,它不会改变存储格式。但是,使用数字字符串作为数据库索引显然效率低下,并且可能会阻止某些数据库功能和优化。
因此,将十六进制字符串转换为普通整数并将它们用作数据库索引就可以了。
First of all, whether a number is 'hexadecimal' or 'decimal' is just a matter of outer representation, it doesn't change the storage format. Using strings of numbers as database indices is obviously inefficient and may prevent certain db features and optimizations, though.
Therefore, convert the hex strings to plain integers and use them as database indices and you'll be fine.
从广义上讲,您希望主键毫无意义,并且(显然)是唯一的。十六进制只是表示数字的一种方式 - 在底层,它仍然是一个整数。因此,我会在存储到数据库之前转换为 int 。
更大的问题是 - 你确定它们是独一无二的吗?如果没有,你就不能使用它们作为你的主键......
Broadly speaking, you want your primary keys to be meaningless, and (obviously) unique. Hex is just one way of representing a number - underneath, it's still an integer. So, I'd convert to int before storing in the DB.
Bigger question is - are you sure they're unique? If not, you can't use them as your primary key....
这是一个坏主意。使用整数键选择会更快。在某些情况下,使用字符串键可能有优势,但使用存储为字符串的数值作为键是愚蠢的。
It's a bad idea. SELECTs will be faster with integer keys. There might be advantages to using string keys in some instances, but using numeric values stored as strings as keys is just silly.