SQL表数据结构,这是错误的吗?使用级联删除
以下是我遇到问题的 3 个表格:
表:机会 - 保存各种机会(工作)描述
表:Opportunities_Applicants - 保存各种申请人申请机会。 1 个申请人只能申请 1 个机会,但是 1 个机会可以有多个申请人
表:Opportunities_Category - 保存类别名称和类型。 1 个类别可以与许多机会相关。
我试图在删除机会类别时执行级联删除,它将删除相应的机会和这些机会的申请人。
这种结构是否合适或者我应该以不同的方式设置数据库?我的表关系应该如何设置,以便在删除机会类别时级联删除起作用?
我应该使用级联删除吗?
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
)
Here are the 3 tables I am having problems with:
Table: Opportunities - Holds various opportunity(job) descriptions
Table: Opportunities_Applicants - Holds various applicants applying for opportunities. 1 Applicant can only apply for 1 opportunity, however 1 opportunity can have many applicants
Table: Opportunities_Category - Holds category name and type. 1 Category can relate to many Opportunities.
I am trying to perform a CASCADING Delete when a Opportunity Category is deleted, it will delete corresponding Opportunities and Applicants for those Opportunities.
Is this structure appropriate or should I setup database differently? How should my table relationships be setup in order for the CASCADING Delete to work when a Opportunity Category is deleted?
Should I even be using 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
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,我的表设置正确:
昨天,我一直在尝试执行以下操作:从其中 CategoryID = @CategoryID 的机会中删除。这只是删除 Opportunities 和 Opportunities_Applicants 中的记录。
今天,我更改为: DELETE FROM Opportunities_Categoies WHERE CategoryID = @CategoryID 并且所有 3 个表都正在删除其相应的记录!
It turns out that my tables are setup properly:
Yesterday, i had been trying to do: DELETE FROM Opportunities WHERE CategoryID = @CategoryID. This was only deleting the records from Opportunities and Opportunities_Applicants.
Today, i changed to: DELETE FROM Opportunities_Categoies WHERE CategoryID = @CategoryID and all 3 tables are deleting their corresponding records!
ALTER TABLE [dbo].[机会] 带检查添加约束 [FK_OpportunitiesCategory_Opportunities] FOREIGN KEY([CategoryID])
参考文献 [dbo].[Opportunities_Category] ([CategoryID])
删除级联时
祝你
好运...
ALTER TABLE [dbo].[Opportunities] WITH CHECK ADD CONSTRAINT [FK_OpportunitiesCategory_Opportunities] FOREIGN KEY([CategoryID])
REFERENCES [dbo].[Opportunities_Category] ([CategoryID])
ON DELETE CASCADE
GO
Good Luck...