替换现有 SQL DB 表中的 PK

发布于 2024-09-28 19:07:09 字数 391 浏览 2 评论 0原文

现在我有一个数据库,其中 PK 是 int IDENTITY。最近,去年,我的任务是将这些添加到复制拓扑中。除了 IDENTITY 字段之外,这种方法的效果非常好。

我想探索如何使用 uniqeidentifier(GUID) 更改或替换它们。
插入新的 PK 列是否可行?
我将其增加到一个big int 会更好吗?

我还应该考虑什么吗?


详细说明为什么我要这样做:

据我了解,当复制遇到 IDENTITY 列时,它会为每个订阅者留出一个 Identity Range(例如 1-1000(默认)),以确保该列具有唯一的 INT。订阅者越多,问题就会变得越大。这导致我们不断收到身份范围检查约束错误。

谢谢

Right now I have a DB where the PK's are int IDENTITY. I recently, in the last year, was tasked with adding these to a Replication Topology. This has worked out quite well with the exception of the IDENTITY fields.

I want to explore my options for changing or replacing them with a uniqeidentifier(GUID).
Is it feasible to insert a NEW PK column?
Would I be better of just increasing it to a big int?

Anything else I should consider?


To elaborate on WHY I want to do do this:

It is my understanding that when Replication encounters an IDENTITY Column it sets aside an Identity Range, say 1-1000(default), for each subscriber to ensure an Unique INT for that column. The more subscribers you have the bigger issue it can become. This leads to the Identity Range Check Constraint errors we keep getting.

Thanks

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

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

发布评论

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

评论(1

你与清晨阳光 2024-10-05 19:07:09

如果您确实必须删除 INT IDENTITY 列,那么您必须执行以下步骤(或多或少):

  • 在表中创建新的 GUID 列,并
  • 用标识所有外键关系的 值填充它引用该表并记下这些(例如,将其 CREATE 脚本存储在磁盘或其他东西上)
  • 将新的 GUID 引用字段添加到所有引用表
  • 根据您已经拥有的 INT 引用字段填充这些值
  • 将所有 FK 引用删除表
  • 删除表上的 INT IDENTITY PK
  • 使新的 GUID 列成为您的 PK
  • 从所有引用表中删除旧的 INT 引用列
  • 使用新的 GUID 引用列重新创建所有外键引用

我希望这应该能解决问题。

If you really must remove the INT IDENTITY column, then you'd have to do these steps (more or less):

  • create the new GUID column in your table and fill it with values
  • identify all foreign key relationships referencing that table and making a note of those (e.g. store their CREATE scripts on disk or something)
  • add a new GUID reference field to all the referencing tables
  • fill those values based on the INT reference field you already have
  • drop all the FK references to your table
  • drop the INT IDENTITY PK on your table
  • make the new GUID column your PK
  • drop the old INT reference columns from all referencing tables
  • re-create all foreign key references using the new GUID reference column

I hope that should do the trick.

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