SQL Server 2005 级联删除 [英] SQL Server 2005 Cascading Delete

查看:31
本文介绍了SQL Server 2005 级联删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是 100% 确定级联删除是如何工作的.

I'm not 100% sure how cascading deletes work.

为了简单起见,我有看起来像这样的表格

I have for simplicity tables that look like this

用户用户 ID

扩展用户用户 ID

评论用户 ID

帖子用户 ID

我基本上有大量的表,它们引用了 User 的 User_ID.我想在一个表上设置级联删除,以便我可以删除 User 对象并确保删除所有引用 User 的表.

I basically have a ton of tables which reference the User_ID from User. I'd like to set a cascading delete on one table so that I can delete the User object and ensure that all tables that reference User are deleted.

但是,我的理解是我需要在每个引用 User 的表上设置删除操作.那就是我需要在每个子表上设置级联删除".我的理解正确吗?

However, my understanding is that I need to set the delete action on every table that references User. that is I need to set the "cascade delete" on every child table. Is my understanding correct?

SQL Server 级联

更新:看起来我必须为每个关系设置它.我应该在哪里将这些关系视为存储"?可能我的观念不对.

Update: It looks like I have to set it for every relationship. Where should I think of these relationships as being "stored"? Maybe my conception is not right.

看起来我可以使用父表中的管理工作室为每个关系设置所有参照完整性规则.

It looks like I can set all the referential integrity rules for each relationship using the management studio from the parent table.

推荐答案

对于每个关系,您可以指定要采取的操作.

For each relationship, you can specify what action to take.

管理此问题的最简单方法可能是使用 SQL Server Management Studio.设计您的父表,并找到所有 PK-FK 关系.

Easiest way to manage this likely would be to use SQL Server Management Studio. Design your parent table, and find all the PK-FK relationships.

对于每个,选择发生删除事件时要采用的路径:

For each, choose which path to take when a Delete event occurs:

  • 无操作 - 这会在发生时导致 FK 错误
  • 级联 - 删除子记录
  • Set null - FK 列值将为 null.当子表中不允许空值时,这显然会引发错误.
  • 设置默认值 - 如果子表上的 FK 列具有默认值,则它将是子列中的新值.

这篇关于SQL Server 2005 级联删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆