在sql server中重置主键 [英] Reset Primary Key in sql server
本文介绍了在sql server中重置主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用c#和sql server 2005.
我有一个名为Transaction ID的列。它取决于4个表,所以我需要使用外键给4个表关系。
现在的问题是,500到达后必须重置交易ID。由于主键,它反映了错误消息...
解决方案
比使用单独的非主键字段。您不必遵循技术层面的业务概念。你也可以使用复杂的主键(你的特殊运行号码+日期) - 但我会按照其他方法。
PS:如果每天有超过500笔交易怎么办?
请按照以下步骤操作:
1.保留此交易ID列,因为它在另外4个表中引用。
2.添加一个新的列Temp_Transaction_ID,它是一个int,而不是null而不是自动增量列。
3.每次插入之前使用max(事务ID)获得最高的Temp_Transaction_ID并增加Temp_Transaction_ID。
4.一旦达到500,将其更新为1.
Hi,
I am using c# and sql server 2005.
i am having column with the name of Transaction ID. it depends 4 tables so i need to give relationship with 4 tables using Foreign Key.
Now problem is, transaction ID must be reset after 500 reached. due to primary key it reflects error message...
解决方案
Than use a separate, non-primary key field for that. You don't have to follow the business concepts on technical level. And you can also use complex primary key (your special running number + date) - but I would follow the other approach.
PS: And what if there are more than 500 transactions a day?
Please follow below steps:
1. Leave this Transaction ID column as it is, since it is referenced in 4 more tables.
2. Add a mew column Temp_Transaction_ID which is an int, and not null and not auto increment column.
3. Before every insert get the highest Temp_Transaction_ID using max(Transaction ID) and increment Temp_Transaction_ID.
4. Once it reaches 500, update it to 1.
这篇关于在sql server中重置主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文