1

以下是我遇到问题的 3 个表:

  • 表:机会 - 拥有各种机会(工作)描述

  • 表:Opportunities_Applicants - 持有各种申请机会的申请人。1个申请人只能申请1个机会,但是1个机会可以有多个申请人

  • 表:Opportunities_Category - 保存类别名称和类型。1 类别可能与许多机会相关。

我正在尝试在删除机会类别时执行级联删除,它将删除相应的机会和这些机会的申请人。

这种结构合适还是我应该以不同的方式设置数据库?应该如何设置我的表关系,以便在删除商机类别时使 CASCADING Delete 起作用?

我什至应该使用 CASCADING Delete 吗?

create table Opportunities_Category
(   
CategoryID          int identity(1,1)       not null
    constraint PK_CategoryID primary key clustered,         
[Name]              varchar(150)            not null,
[Type]              varchar(100)            not null --Pay, Volunteer, Volunteer Yearly
)

create table Opportunities
(
OpportunityID       int identity(1,1)   not null
    constraint PK_OpportunityID primary key clustered,
CategoryID          int                     not null
    constraint FK_CategoryID foreign key references Opportunities_Category(CategoryID) ON DELETE CASCADE,       
Title               varchar(300)            not null,
PostingDate         datetime                not null,
ClosingDate         datetime                not null,
Duration            varchar(150)            not null, --Part Time, Full Time, Seasonal, Contract
Compensation        varchar(150)            not null, --Hourly, Volunteer, Salary
[Description]       varchar(5000)           not null,
Qualifications      varchar(5000)           not null,
Show                int                     not null
)

create table Opportunities_Applicant
(
ApplicantID             int identity(1,1)   not null
    constraint PK_ApplicantID primary key clustered,
OpportunityID           int                 not null
    constraint FK_OpportunityID foreign key references Opportunities(OpportunityID) ON DELETE CASCADE,
[First]                 varchar(150)        not null,
[Last]                  varchar(150)        not null,
Phone                   varchar(20)         not null,
Cell                    varchar(20)         not null,
EMail                   varchar(200)        not null,
CoverLetterResume       varchar(300)        null,
[Timestamp]             datetime            not null    
)   
4

2 回答 2

1

事实证明,我的表设置正确:

昨天,我一直在尝试做:从机会中删除 CategoryID = @CategoryID。这只是从 Opportunities 和 Opportunities_Applicants 中删除记录。

今天,我改为:DELETE FROM Opportunities_Categoies WHERE CategoryID = @CategoryID 并且所有 3 个表都在删除它们对应的记录!

于 2011-04-20T16:35:49.910 回答
0

ALTER TABLE [dbo].[Opportunities] WITH CHECK ADD CONSTRAINT [FK_OpportunitiesCategory_Opportunities] FOREIGN KEY([CategoryID]) REFERENCES [dbo].[Opportunities_Category] ​​([CategoryID]) ON DELETE CASCADE GO

祝你好运...

于 2011-04-19T22:13:41.753 回答