用于维护修改历史记录的 SQL 触发器

发布于 2024-12-08 15:10:44 字数 533 浏览 0 评论 0 原文

我有三个表:

Table1: Customers (CustomerId, Name, CustomerAddress)
Table2: AccountManagers(ManagerId, Name)
Table3: CustomerAccountManagers (CustomerId, ManagerId, Remarks)
Table4: CustomerHistory(CustomerAddress, ManagerId, Date)

CustomerHistory 表用于存储对“CustomerAddress”或“ManagerId”所做的任何更改,例如 CustomerAddress 从“Address1”更新为“Address2”,或者 CustomerAccountManager 从“Manager1”更改为“Manager2”。

我需要通过 SQL 触发器将更改存储在 CustomerHistory 表中。问题是我应该在哪张桌子上放置触发器?请注意,更改是同时对“客户”表和“客户”表进行的。 “客户帐户经理”。

谢谢

I have three tables:

Table1: Customers (CustomerId, Name, CustomerAddress)
Table2: AccountManagers(ManagerId, Name)
Table3: CustomerAccountManagers (CustomerId, ManagerId, Remarks)
Table4: CustomerHistory(CustomerAddress, ManagerId, Date)

The CustomerHistory table is used to store any changes made to "CustomerAddress" OR "ManagerId" for example CustomerAddress is updated from "Address1" to "Address2" OR The CustomerAccountManager changes from "Manager1" to "Manager2".

I need to store the changes in CustomerHistory table through SQL Trigger. The issue is that the on which table should i have my trigger? Please note that the changes are made at the same time to both the tables "Customers" & "CustomerAccountManagers".

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

嗼ふ静 2024-12-15 15:10:44

首先,CustomerHistory 表可能还应该包含CustomerId,以便可以将历史记录追溯到正确的客户。

您需要两个触发器:一个在 CustomerAccountManagers 上,另一个在 Customers 上。如果您可以保证它们的执行顺序,那就没问题:第一个触发器将插入,第二个触发器将更新历史记录。

如果您不能保证顺序,事情就会变得复杂,因为每个触发器都必须:1)尝试插入新记录,如果失败2)更新现有记录。您必须保护自己免受另一个触发器的间歇性插入的影响,这可能意味着在具有可序列化隔离级别的事务中运行(锁定整个表)。这很容易出现死锁,因此最好使用两个历史表,正如其他人已经建议的那样。

First of all, the CustomerHistory table should probably contain CustomerId as well, so that a history record can be tracked back to the proper customer.

You'll need two triggers: one on CustomerAccountManagers and one on Customers. If you can guarantee the order in which they are executed it's fine: the first trigger will insert, the second update the history record.

If you cannot guarantee the order, things get complicated, as each trigger would have to: 1) try to insert a new record, and failing that 2) update the existing. You'd have to protect yourself from the intermittent insertion by another trigger and this likely means running within a transaction with serializable isolation level (locking the whole table). This is deadlock-prone, so it's really better to use two history tables, as others already suggested.

和影子一齐双人舞 2024-12-15 15:10:44

对于每个普通表,您应该有不同的历史表。
然后,您可以在每个普通表中放置一个触发器。

最后,如果需要,您可以创建一个视图 CustomerHistory 连接不同的历史表。

You should have different historic tables for each normal table you have.
Then you can place a trigger in each normal table.

Finally, if you need, you can create a view CustomerHistory joining the different historic tables.

放飞的风筝 2024-12-15 15:10:44

您需要将触发器添加到表中,其中数据更改将“触发”执行某些操作的需要。

因此,Customers 表上的触发器用于跟踪 CustomerAddress 更改,而 CustomerAccountManagers 上的触发器则在 ManagerId 更改时触发。

You need to add your trigger to the table where data changes will "trigger" a need to do something.

So a trigger on the Customers table to track CustomerAddress changes and a trigger on CustomerAccountManagers when the ManagerId is changed.

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