主键(两列)标识增量
我的主键中有两列(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每行 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.
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.