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
. This organization has branches which are in branch table called tblBranch
, and these branches have multiple applications in a table tblApplication
. These applications in turn are used by multiple users stored in 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?
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:
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. You can have these ON DELETE CASCADE
settings over multiple foreign key constraints down to the tblUsers
table.
You need to add ON DELETE CASCADE
to the constraints on tblBranch, tblApplication, and 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.
If you delete a branch, all related records form tblApplication and tblUsers will be deleted.
If you delete an Application, all related records form tblUsers will be deleted.
If you delete a user, nothing else will be deleted.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.