Guid.NewGuid()。GetHash code()为DBNewGuid、Guid、GetHash、DB

2023-09-03 16:55:59 作者:锥心话

这将是可靠的使用作为ID数据存储(SQL Server)的?

我会用一个GUID,但我preFER的数值。

解决方案   

这将是可靠的使用作为ID数据存储(SQL Server)的?

没有。 GUID是128位,但散列codeS是32位。因此,有必要的碰撞。这可能是不可能的,你曾经遇到的一个,但你不能保证永远不会遇到的。

您想要的可靠性什么是保证你永远不会遇到冲突。如果你坚持要用 Guid.NewGuid()。GetHash code()那么你需要添加逻辑来检测碰撞。的GUID确实有优势(和缺点),但没有更多的信息,我会建议使用自动增量 INT 列。尤其是当你说你想要一个数字专栏中,我会倾向于使用身份

漫画 什么是字符串匹配算法

Would this be reliable for using as an ID for data storage(SQL Server)?

I would use a guid but I prefer a numeric value.

解决方案

Would this be reliable for using as an ID for data storage(SQL Server)?

No. GUIDs are 128-bit but hashcodes are 32-bit. Therefore, there are necessarily collisions. It may be unlikely that you ever encounter one, but you are not guaranteed to never encounter one.

What you want for reliability is a guarantee that you never encounter a collision. If you insist on using Guid.NewGuid().GetHashCode() then you need to add logic to detect collisions. GUIDs do have advantages (and disadvantages) but without additional information I would suggest using an auto-incrementing int column. Especially as you say you want a numeric column I would lean towards using an IDENTITY.