SQL Server级联不会删除父级的子级,其父级也是另一父级的子级

时间:2013-12-02 08:24:05

标签: c# asp.net sql sql-server cascade

Microsoft SQL Server 2012& ASP.NET

我需要一些帮助,我正在努力解决这个问题,我在数据库中有3个表:

NumberOne > NumberTwo > NumberThree

的Numberone:

  • id(int)

NumberTwo:

  • id(int)
  • numberone_id

NumberThree

  • id(int)
  • numbertwo_id

我有一个从id(第一个)到numberone_id(第二个)的外键和一个从id(第二个)到numbertwo_id的外键(数字)三)。这些密钥的Cascade规则为Delete。现在,当我以编程方式删除第一个表时,第二个和第三个也被删除。但是当我以编程方式删除第二个表时,我想保留第一个表,第三个应该被删除但是却没有。

为什么会这样,我该如何解决这个问题?提前谢谢。

修改

根据要求:

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]

1 个答案:

答案 0 :(得分:2)

我无法重现这一点。这是一个脚本(以及我将其放在答案中的原因,即使它是一个评论)。

你能创建一个演示问题的脚本吗?

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

结果:

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)