将主键添加到具有现有聚集索引的表

发布于 2024-09-13 03:41:41 字数 402 浏览 2 评论 0原文

我必须使用数据库来进行报告 DB 相当大: 416 055 104 行 不过,每一行都很轻,只有布尔值和 int id。

每行由 3 列标识,但令我惊讶的是,它上面没有主键。 只有具有唯一约束的聚集索引。

所以知道了这一点,我有两个问题。

  1. 这有什么充分的理由吗?
  2. 有什么办法可以把它变成主键吗?

关于问题 2

创建新主键还会创建一个与之关联的非聚集索引(已经存在一个聚集索引)。
这不是我正在寻找的东西。我想保留相同的索引,但也将其设为主键。

  • 是否可以?
  • 这会比再次创建整个索引更快吗? (我希望如此)
  • 会有什么后果? (锁?崩溃?数据损坏?)

I have to work with a database to do reporting
The DB is quite big : 416 055 104 rows
Each row is very light though, just booleans and int ids.

Each row is identify by 3 columns, but at my surprise, there is no Primary Key on it.
Only a Clustered Index with a unique constraint.

So Knowing that, I have 2 question.

  1. Could there be ANY good reason for that?
  2. Is there any way I can turn this into a primary key.

Regarding question 2

Creating a new primary key also creates a non-clustered index to associate with (there is already an existing clustered one).
This is not what I am looking for. I want to keep that same index, but also make it a primary key.

  • Is it possible?
  • Would that be faster that creating the whole index again? (I hope so)
  • What could be the consequences? (locks? crash? corrupted data?)

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

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

发布评论

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

评论(3

海夕 2024-09-20 03:41:41

不可空列上的 PRIMARY KEY 和 UNIQUE 约束之间几乎没有区别。因此,如果相关列不可为空,那么我建议您什么也不做。将候选键设为主键的主要原因是,如果您有某些软件(例如数据建模工具或其他开发工具)期望使用 PRIMARY KEY 约束来标识该键。

There is little or no difference between a PRIMARY KEY and a UNIQUE constraint on non-nullable columns. So if the columns in question are non-nullable then I suggest you do nothing. The main reason to make a candidate key into a primary key is if you have some software (such as a data modelling tool or other development tool) that expects the key to be identified with a PRIMARY KEY constraint.

予囚 2024-09-20 03:41:41

好问题。

如果您在不可为空的列上已经有唯一索引,那么您就有了一个候选键。我不知道将其作为“官方”主键有什么特别的好处。事实上我有一种感觉,不把它做成PK会带来更大的灵活性。

Good question.

If you already have a unique index on non nullable columns then you have a candidate key. I'm not aware of any particular benefit of making this an "official" primary key. In fact I have a feeling that not making it a PK will give greater flexibility.

怪我鬧 2024-09-20 03:41:41
  1. 唯一索引可以允许为空
    价值观。主键不能。

  2. 我相信您不能将现有索引“标记”为主键。您必须删除它并重新创建。为了避免出现问题,我建议在执行此操作之前在表上放置一个 TABLOCKX, HOLDLOCK

  1. A unique index can allow null
    values. A primary key can't.

  2. I believe you can't "mark" an existing index as the primary key. You'd have to drop it and recreate. To avoid stuff, I'd say it'd be good to place a TABLOCKX, HOLDLOCK on the table before doing that.

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