SQL Server 双向级联?

发布于 2024-12-18 19:22:19 字数 628 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

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

发布评论

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

评论(2

寻梦旅人 2024-12-25 19:22:19

我认为,是的,

ON DELETE CASCADE

Customer.Person 外键中的子句和

ON DELETE NO ACTION

Transaction.Customer 外键中的子句将达到预期的效果。

然后,任何删除人员的尝试都会删除相关的客户行,除非有相关的交易。这将引发错误并且 DELETE 将被回滚。

I think, yes, an

ON DELETE CASCADE

clause in Customer.Person foreign key, and an

ON DELETE NO ACTION

clause 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.

甜心小果奶 2024-12-25 19:22:19

您可以在人员表上设置一个删除触发器,如果​​要删除的人员在事务表中有任何记录,该触发器就会停止删除。

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.

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