简体   繁体   English

如果删除了主表实体,则使用FOREIGN KEY约束删除所有记录

[英]Delete All the records Using FOREIGN KEY Constraints if Main Table Entity is deleted

How to delete records from multiple tables if main table's entity is deleted as constraints is applied on all 如果主表的实体被删除,因为在所有表上都施加了约束,那么如何从多个表中删除记录

Scenario 情境

Let me give you a scenario: there is this main table called Organization or TblOrganization . 让我给你一个场景:这个主表叫做OrganizationTblOrganization This organization has branches which are in branch table called tblBranch , and these branches have multiple applications in a table tblApplication . 该组织的分支位于名为tblBranch分支表中,并且这些分支在表tblApplication具有多个应用程序。 These applications in turn are used by multiple users stored in tblUsers . 这些应用程序又被tblUsers存储的多个用户使用。

What I want is: when I delete an organization, all the branches, applications and users related to it must be deleted also. 我想要的是:删除组织时,还必须删除与其相关的所有分支机构,应用程序和用户。

How I can apply that on a button click in asp.net web forms? 我如何将其应用在按钮上,单击asp.net Web表单?

Right now this is my delete function which is very simple 现在这是我的删除功能,非常简单

Public void Delete(int? id)
{
    var str = ”DELETE FROM tblOrganization WHERE organizationId=”+ id ;
}

And my tables look like this: 我的表如下所示:

CREATE TABLE tblOrganization
(
    OrganizationId int,
    OrganizationName varchar(255)
); 


CREATE TABLE tblApplication
(
     ApplicationId int,
     OrganizationId int,
     ApplicationName varchar(255)
);

CREATE TABLE tblBranches
(
    BranchId int, 
    OrganizationId int,
    BranchName varchar (255)
);

CREATE TABLE tblUsers
(
    userId int,
    OrganizationId int,
    UserName varchar(255)
);

Constraints are applied on all table like this 这样,约束将应用于所有表

ALTER TABLE [dbo].[tblBranchs] WITH CHECK 
    ADD CONSTRAINT [FK_tblBranchs_tblOrganization] 
    FOREIGN KEY([OrganizationId])
    REFERENCES [dbo].[tblOrganization] ([OrganizationId])

You cannot "apply" this in ASP.NET - this is a database concern and you need to change your foreign key constraints to include ON DELETE CASCADE - something like this: 您不能在ASP.NET中“应用”此操作-这是数据库问题 ,您需要更改外键约束以包括ON DELETE CASCADE诸如此类:

ALTER TABLE [dbo].[tblBranchs] WITH CHECK 
    ADD CONSTRAINT [FK_tblBranchs_tblOrganization] 
    FOREIGN KEY([OrganizationId])
    REFERENCES [dbo].[tblOrganization] ([OrganizationId])
        ON DELETE CASCADE

This means: if a row from tblOrganization is deleted, that delete will be cascaded down into the tblBranchs table, too, deleting all rows that have been referencing that orgnization. 这意味着:如果删除了tblOrganization的一行, tblOrganization该删除操作也将向下级联tblBranchs表中,同时删除所有已引用该组织的行。 You can have these ON DELETE CASCADE settings over multiple foreign key constraints down to the tblUsers table. 您可以在多个外键约束(直到tblUsers表)上具有这些ON DELETE CASCADE设置。

You need to add ON DELETE CASCADE to the constraints on tblBranch, tblApplication, and tblUsers: 您需要将ON DELETE CASCADE添加到tblBranch,tblApplication和tblUsers的约束中:

ALTER TABLE [dbo].[tblBranchs] WITH CHECK 
    ADD CONSTRAINT [FK_tblBranchs_tblOrganization] 
    FOREIGN KEY([OrganizationId])
    REFERENCES [dbo].[tblOrganization] ([OrganizationId])
    ON DELETE CASCADE

ALTER TABLE [dbo].[tblApplication] WITH CHECK 
    ADD CONSTRAINT [FK_tblApplication_tblBranchs] 
    FOREIGN KEY([BranchId])
    REFERENCES [dbo].[tblBranchs] ([BranchId])
    ON DELETE CASCADE

ALTER TABLE [dbo].[tblUsers] WITH CHECK 
    ADD CONSTRAINT [FK_tblUsers_tblApplication] 
    FOREIGN KEY([ApplicationId])
    REFERENCES [dbo].[tblApplication] ([ApplicationId])
    ON DELETE CASCADE

Now if you delete an organization, the database engine will delete all related child records automatically: 现在,如果您删除组织,数据库引擎将自动删除所有相关的子记录:

  • If you delete an organization, all related records form tblBranch, tblApplication, and tblUsers will be deleted. 如果删除组织,则所有与tblBranch,tblApplication和tblUsers有关的记录都将被删除。

  • If you delete a branch, all related records form tblApplication and tblUsers will be deleted. 如果删除分支,则将删除所有来自tblApplication和tblUsers的相关记录。

  • If you delete an Application, all related records form tblUsers will be deleted. 如果删除应用程序,则将从tblUsers中删除的所有相关记录。

  • If you delete a user, nothing else will be deleted. 如果删除用户,则不会删除任何其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM