SQL 2008中如何建立一个关联三个表的触发器

yp2800 2011-11-11 08:52:15
--创建学生介绍自己(IntroduceYourself)表
CREATE TABLE IntroduceYourself(
cStudentsId char(10) NOT NULL,
vSelfAssessment varchar(100) NOT NULL,
vSelfSpecialty varchar(100) NULL,
vHobbiesInterest varchar(100) NULL
)

ALTER TABLE IntroduceYourself
ADD CONSTRAINT fkIntroduceYourself FOREIGN KEY (cStudentsId) REFERENCES Students(cStudentsId)

--创建学生教育经历(Education)表
CREATE TABLE Education(
cStudentsId char(10) NOT NULL,
vEducationUndergo varchar(100) NOT NULL,
vTrainUndergo varchar(100) NULL
)
ALTER TABLE Education
ADD CONSTRAINT fkEducation FOREIGN KEY (cStudentsId) REFERENCES Students(cStudentsId)

--创建学生求职信息(ApplyJobInformation)表
CREATE TABLE ApplyJobInformation(
vExpectWorkPlace varchar(30) NULL,
vIndustry varchar(70) NOT NULL,
cPosition char(50) NOT NULL,
dApplyTime datetime NOT NULL,
cStudentsId char(10) NOT NULL
)

ALTER TABLE ApplyJobInformation
ADD CONSTRAINT fkApplyJobInformation FOREIGN KEY (cStudentsId) REFERENCES Students(cStudentsId)
alter table dbo.ApplyJobInformation add msalary money null
我想在删除学生求职信息(ApplyJobInformation)表中一条cStudentsId = 2010121015的一行记录数据,
同时也要删除学生教育经历(Education)表中一条cStudentsId = 2010121015的一行记录数据,
并且还要删除学生介绍自己(IntroduceYourself)表中一条cStudentsId = 2010121015的一行记录数据
该怎么做到呢,希望各位高手指点一二,不尽感激
...全文
475 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yp2800 2011-11-11
  • 打赏
  • 举报
回复
谢谢指点,问题解决了,非常感谢
中国风 2011-11-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yp2800 的回复:]

谢谢,不过能不能用触发器达到这样效果呢,又该怎么写呢
[/Quote]
可以用,这是级联做的事
不建议用触发器

这样用
go

create trigger tr_IntroduceYourself_delete on IntroduceYourself
for delete
as
delete Education where cStudentsId in(select cStudentsId from deleted)
delete ApplyJobInformation where cStudentsId in(select cStudentsId from deleted)
yp2800 2011-11-11
  • 打赏
  • 举报
回复
谢谢,不过能不能用触发器达到这样效果呢,又该怎么写呢
中国风 2011-11-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yp2800 的回复:]

能不能写一下级联,这个我不会,谢谢指点
[/Quote]

在1楼已经给你改了,你细心点啊,兄弟
yp2800 2011-11-11
  • 打赏
  • 举报
回复
能不能写一下级联,这个我不会,谢谢指点
中国风 2011-11-11
  • 打赏
  • 举报
回复
用级联删除就行了,在外健上加这选项on delete cascade
CREATE TABLE IntroduceYourself(
cStudentsId char(10) NOT NULL,
vSelfAssessment varchar(100) NOT NULL,
vSelfSpecialty varchar(100) NULL,
vHobbiesInterest varchar(100) NULL
)

ALTER TABLE IntroduceYourself
ADD CONSTRAINT fkIntroduceYourself FOREIGN KEY (cStudentsId) REFERENCES Students(cStudentsId)

--创建学生教育经历(Education)表
CREATE TABLE Education(
cStudentsId char(10) NOT NULL,
vEducationUndergo varchar(100) NOT NULL,
vTrainUndergo varchar(100) NULL
)
ALTER TABLE Education
ADD CONSTRAINT fkEducation FOREIGN KEY (cStudentsId) REFERENCES Students(cStudentsId) on delete cascade

--创建学生求职信息(ApplyJobInformation)表
CREATE TABLE ApplyJobInformation(
vExpectWorkPlace varchar(30) NULL,
vIndustry varchar(70) NOT NULL,
cPosition char(50) NOT NULL,
dApplyTime datetime NOT NULL,
cStudentsId char(10) NOT NULL
)
ALTER TABLE ApplyJobInformation
ADD CONSTRAINT fkApplyJobInformation FOREIGN KEY (cStudentsId) REFERENCES Students(cStudentsId) on delete cascade

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧