SQL Server 如何让两列自动递增?
我的 SQL Server 数据库表中有两列,我希望在添加新字段时自动增量。但是,Managment Studio 不允许我将两列设置为 IDENTITY。还有其他方法可以做到这一点吗?
I have two columns in a table of a SQL server DB that I would like to autoincrement when new fields are added. However, Managment Studio wont allow me to set two columns to IDENTITY. Is there some other way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将第二个字段设置为基于第一个字段的计算字段。
或者创建一个 INSERT 触发器,然后以编程方式生成第二个值。
You could make the second field a calculated field based on the first.
Or make an INSERT trigger than programatically generates the value for the second.
如果您希望第二列基本上成为第一列的镜像:
如果您希望它对其应用一些数学公式以实现某种偏移:
If you wanted the 2nd column to basically be a mirror of the first:
If you wanted it to apply some math formula to it to achieve some kind of offset:
每个表只能有一个自增字段。但是,您可以有一个基于自动增量字段的计算字段。或者,您可以有一个 int 字段,您可以在其中通过前端代码或触发器管理序列。您还可以在 SQL Server 中使用序列。
There can only be one auto increment field per table. But, you could have a calculated field based on the auto increment field. Or, you could have an int field where you manage the sequence by front end code or by a trigger. And also you could use a sequence in SQL Server.