SQL Server 双向级联?
我正在尝试在 SQL Server Management Studio 2008 中组合一个数据库,该数据库将管理数据,而无需以编程方式处理数据,但目前我遇到了一些问题。
我有一个名为 person 的主表,该表保存有关人员的核心数据,并且该表链接到名为 customer 的第二个表,如果他们希望拥有交易权限,则将其包含在该表中。一旦交易发生,客户 ID 就会被放入交易表中。
|Person| |Customer| |Transaction|
|------| |--------| |-----------|
| ID |-\ | ID |-\ | ID |
| | \| person | \| Customer |
| | | | | |
在这两个链接中,ID 是 PK,次要项目(人员/客户)是 FK。 我在这种安排中遇到的问题在于,如果一个人进行了交易,则必须保留该人,因此他们的客户 ID 出现在交易表中。
我不是 100% 确定要采取什么方法,个人-客户之间的“删除规则 - 级联”与客户 - 交易之间的“删除规则 - 无操作”会产生预期的效果吗?
在这个问题上任何想法方法或更好的方法将非常感激。
I'm trying to put together a database in SQL Server Management Studio 2008 which will manage the data without having to programatically process the data but I'm having some issues at the moment.
I have a main table called person, this table holds core data about a person and this is linked to a second table called customer in which they are included if they wish to have transaction permissions. Once a transaction has taken place the customer ID is placed into the transaction table.
|Person| |Customer| |Transaction|
|------| |--------| |-----------|
| ID |-\ | ID |-\ | ID |
| | \| person | \| Customer |
| | | | | |
Of the two links, the ID's are PK's and the secondary items (person/customer) are FK's.
The problem I am having with this arrangement lies in the requirement that a person has to be kept if they have made a transaction and thus their customer ID appears in the transaction table.
I am not 100% sure on what approach to take, would a 'Delete Rule - Cascade' between person-customer with a 'Delete Rule - No Action' between Customer - Transaction create the desired effect?
Any thoughts approach or a better approach would be very much appreciated on this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为,是的,
Customer.Person
外键中的子句和Transaction.Customer
外键中的子句将达到预期的效果。然后,任何删除人员的尝试都会删除相关的客户行,除非有相关的交易。这将引发错误并且 DELETE 将被回滚。
I think, yes, an
clause in
Customer.Person
foreign key, and anclause in
Transaction.Customer
foreign key would have the desired effect.Then any attemp to delete a Person, will delete the related Customer row, unless there are related Transcations. That will raise an error and the DELETE would be rolled back.
您可以在人员表上设置一个删除触发器,如果要删除的人员在事务表中有任何记录,该触发器就会停止删除。
You could just have a delete trigger on the person table that stops the delete if the to-be-deleted person has any records in the Transaction table.