简体   繁体   中英

SQL Server cascade does not delete the child of a parent where the parent is also a child of another parent

Microsoft SQL Server 2012 & ASP.NET

I need some help with the following that I am struggling with, I have 3 tables in the database:

NumberOne > NumberTwo > NumberThree

NumberOne:

  • id (int)

NumberTwo:

  • id (int)
  • numberone_id

NumberThree

  • id (int)
  • numbertwo_id

I have a foreign key from id (number one) to numberone_id (number two) and a foreign key from id (number two) to numbertwo_id (number three). These keys have Cascade as their Delete rule. Now, when I programmatically delete the number one table, number two and number three are also deleted. But when I programmatically delete number two table, and I want to keep table one, number three should be deleted but it doesn't.

Why is this and how can I fix this? Thanks in advance.

EDIT:

As requested:

CREATE TABLE [dbo].[REACTIONFILES](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [reactions_id] [int] NOT NULL,
    [reactionsFile] [varbinary](max) NOT NULL,
    [reactionsName] [varchar](250) NOT NULL,
 CONSTRAINT [PK_REACTIONFILES] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


CREATE TABLE [dbo].[REACTIONS](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [tickets_id] [int] NOT NULL,
    [contents] [varchar](150) NOT NULL,
    [users_id] [int] NOT NULL,
    [reactionStartdate] [date] NOT NULL,
 CONSTRAINT [PK_REACTIONS] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


CREATE TABLE [dbo].[TICKETS](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [subject] [varchar](50) NOT NULL,
    [categories_id] [int] NOT NULL,
    [priorities_id] [int] NOT NULL,
    [statuses_id] [int] NOT NULL,
    [users_id] [int] NOT NULL,
    [usergroups_id] [int] NOT NULL,
    [isPublic] [bit] NOT NULL,
    [devStartdate] [date] NULL,
    [devEnddate] [date] NULL,
    [devTime] [varchar](50) NULL,
    [commentIntern] [varchar](max) NULL,
    [commentExtern] [varchar](max) NULL,
    [releaseVersion] [varchar](150) NULL,
    [ticketStartdate] [date] NOT NULL,
 CONSTRAINT [PK_TICKETS] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

I'm unable to reproduce this. Here's a script (and the reason I've placed this in an answer even though it's a comment).

Can you please create a script that demonstrates the problem?

create table one(id int not null primary key)
go
insert into one(id) values (1),(2),(3)
go
create table two(id int not null primary key, one_id int not null references one(id) on delete cascade)
go
insert into two(id,one_id) values (1,1),(2,2),(3,3)
go
create table three(id int not null primary key, two_id int not null references two(id) on delete cascade)
go
insert into three(id,two_id) values (1,1),(2,2),(3,3)
go
delete from one where id = 3
delete from two where id = 2
select * from two
select * from three
select * from one

Results:

id          one_id
----------- -----------
1           1

(1 row(s) affected)

id          two_id
----------- -----------
1           1

(1 row(s) affected)

id
-----------
1
2

(2 row(s) affected)

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.

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