.NET 中数据库表的唯一 ID

发布于 2024-10-11 06:54:22 字数 399 浏览 8 评论 0原文

有人可以列出广泛使用的方法来生成要在数据库表中使用的唯一 ID(PK 或其他)

表字段应该始终为字符串(为了灵活性)或 GUID(紧密耦合)还是始终仅使用 IDENTITY?

如果它是字符串,那么我当然可以选择随时更改生成器逻辑。

对于 IDENTITY,我感到受到限制,因为我必须依赖数据库。但是,对于像发票#这样的字段来说,向外部发布是一个不错的选择吗?

对于小数据(例如 10000 条记录),我必须选择什么选项才能获得最小长度的唯一但随机的字母数字?

我面临很多情况,我需要这样的唯一数字用于各种目的,如果我无法想出一个好的选择,我必须回退到好的旧的和快速的.NET Guid.newGuid.ToString()。

但我需要听听有经验的人的意见,一般准则和需要注意的事项是什么?

Can someone list widely used methods for generating unique IDs to be used in DB tables (PK or otherwise)

Should the table field be string always (for flexibility) or GUID (tight coupling) or simply use IDENTITY always ?

If it is string then, of course i have option to change generator logic anytime.

With IDENTITY i feel constrained because i have to depend on database. But is it good option for fields to publish outside like an Invoice #.

For small size of data say 10000 records, what option i have to get smallest length unique but random alpha-numeric ?

I face many situations where i need such unique number for various purposes, if i am unable to think through a good option, i have to fallback to good old and quick .NET Guid.newGuid.ToString().

But i need to hear from experienced folks, What are the general guidelines and things to watch out for ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七颜 2024-10-18 06:54:22

您可以简单地使用下面的内容来创建一个唯一的ID,

Guid.NewGuid();

我个人更喜欢将表字段设置为唯一标识符。

You can simply use the below in order to create a unique id

Guid.NewGuid();

personally i prefer to set the table field to unique identifier.

老旧海报 2024-10-18 06:54:22

始终使用尽可能小的 ID。

如果您不需要在业务层中生成 ID(例如,许多客户端节省了往返时间),请在表中使用 IDENTITY 列,否则使用 GUID,但不要将聚集索引放在 GUID 列上,除非它也是 < a href="https://stackoverflow.com/questions/1155454/performance-value-of-comb-guids">COMB GUID。

Always use the smallest ID possible.

If you do not need IDs generated in the business tier (e.g. many clients saving on round trip), use an IDENTITY column in your table, else use a GUID, but do not place your clustered index on the GUID column unless it's also a COMB GUID.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文