sql server中ON DELETE CASCADE的删除触发器问题

发布于 2024-12-20 16:06:18 字数 584 浏览 2 评论 0原文

我在 Sql 服务器中有两个表

Table A: Id UNIQUEIDENTIFIER NOT NULL
Table B: Id UNIQUEIDENTIFIER NOT NULL
         AId UNIQUEIDENTIFIER NOT NULL,ON DELETE CASCADE (This is Id of Table A)

(注意 B 的 AId 列使用 ON DELETE CASCADE 选项引用 A)。 B 也有一个删除触发器,就像

CREATETRIGGER B_Delete_Trigger
ON [dbo].[B]
FOR DELETE
AS
SELECT * FROM DELETED AS B INNER JOIN A ON B.AId=A.Id

现在当我对表 B 执行删除操作时,上面的触发器返回行,但是当打开时 表 A 没有返回任何内容。

据我所知,SQL Server 首先从子表(本例中为 B)删除记录,然后从父表(本例中为 A)删除记录,因此上面的触发器应该在这两种情况下返回一些记录。

请告诉我如何访问父母记录,否则我的观察是错误的。

I have two Tables in Sql server

Table A: Id UNIQUEIDENTIFIER NOT NULL
Table B: Id UNIQUEIDENTIFIER NOT NULL
         AId UNIQUEIDENTIFIER NOT NULL,ON DELETE CASCADE (This is Id of Table A)

(Notice B has column AId that references A with ON DELETE CASCADE option).
B also has a delete trigger like

CREATETRIGGER B_Delete_Trigger
ON [dbo].[B]
FOR DELETE
AS
SELECT * FROM DELETED AS B INNER JOIN A ON B.AId=A.Id

Now when I perform delete operation on Table B, above trigger returns rows, but when on
Table A nothing is returned.

As much I know SQL Server first delete records from Child Table(B in this case) and then from parent(A in this case), so above trigger should return some record in both cases.

Please, tell me how to access parents records, Or my observation is wrong.

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

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

发布评论

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

评论(4

一刻暧昧 2024-12-27 16:06:19

需要注意以下事项:

  • 如果是父子关系表,请务必尝试在父表上创建触发器以执行ON DELETE CASCADE
  • 子表不能与父表进行CASCADE 。
  • ON DELETE CASCADE 表示:当您从父表中删除项目时,所有已删除引用的项目都将从子表中删除。

希望你能接受我,谢谢你的时间。

Below items should be noted:

  • In case of Parent-Child relational table, always try to create trigger on parent table to perform ON DELETE CASCADE
  • Child table can't CASCADE with Parent.
  • ON DELETE CASCADE means: when you delete item from Parent table, all items will be deleted from child table with deleted reference.

Hope you've got me, thanks for your time.

梦一生花开无言 2024-12-27 16:06:18

实际上当你在表A上调用这个触发器时,执行顺序是

  1. 从子表B中删除记录。从
  2. 父表A中删除记录。
  3. 触发器调用

并且SELECT * FROM DELETED仅包含A表中的数据。

Actually when you call this trigger on table A. The execution sequence is

  1. DELETE Record from child table B.
  2. DELETE Record from Parent table A.
  3. Trigger call

And SELECT * FROM DELETED contains only data from A table.

同尘 2024-12-27 16:06:18

“FOR”触发器相当于“AFTER”触发器。触发器在删除操作完成后触发。因此,当B执行删除触发器时,表A中的记录已经消失了。据我所知,没有简单的方法可以访问表A中的记录。

A "FOR" trigger is equivalent to an "AFTER" trigger. The trigger fires after the delete operation has completed. Therefore, by the time the delete trigger executes for B, the record in table A is already gone. As far as I know, there is no simple way to get access to the record in table A.

风和你 2024-12-27 16:06:18

删除级联不应该在父表上吗?

这样做的目的是,一旦父表被删除,当然前提是还有其他表引用父表(通过 FK 约束),那么所有子记录也将被删除。看来你搞错了??

MSDN:

http://msdn.microsoft.com/ en-us/library/aa933119(v=sql.80).aspx

Shouldn't the on delete cascade be on the parent table?

The purpose of this is that once the parent is deleted, providing there are other tables referencing the parent of course (via FK constraints), then all the child records will be deleted too. It looks like you have it the wrong way round??

MSDN:

http://msdn.microsoft.com/en-us/library/aa933119(v=sql.80).aspx

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