主键(两列)标识增量

发布于 2024-12-17 09:24:52 字数 229 浏览 4 评论 0原文

我的主键中有两列(id,type),id 是身份,type 是外键。 我想为 id 列设置种子,如下所示:

 id    type
10000   1
10001   1
10000   2
10001   2
10002   1
10002   2
10000   3

我可以通过代码(或 dml)执行此操作,但想知道在 ddl 或 SqlServer 表属性中是否可能?

I have two columns in my primary key (id, type), id is identity and type is foreign key.
I want to set seed for id column like following:

 id    type
10000   1
10001   1
10000   2
10001   2
10002   1
10002   2
10000   3

I could do this from code (or dml), but wonder is it possible in ddl or SqlServer table properties?

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

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

发布评论

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

评论(2

弥枳 2024-12-24 09:24:53

每行 id 列递增 1。没有办法让它重复。为什么不能只让标识符列作为表的 pk?您可能必须求助于使用触发器来执行此操作。

An id column increments by 1 for each row. There is no way to have it repeat. Any reason why you can't just have the identifier column be the pk for the table? You may have to resort to using a trigger to do this.

死开点丶别碍眼 2024-12-24 09:24:53

Microsoft SQL Server 不允许您通过 TSQL 轻松添加或更改现有列上的标识。要更改原始种子值并重新设定任何现有行的种子,您必须删除标识列并重新创建它并指定新的种子值。当表包含数据时,标识号将添加到具有指定种子和增量值的现有行中。

Microsoft SQL Server does not allow you to add or alter an Identity on an existing column via TSQL very easily. To change the original seed value and reseed any existing rows, you must drop the identity column and recreate it specifying the new seed value. When the table contains data, the identity numbers are added to the existing rows with the specified seed and increment values.

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