+ -
当前位置:首页 → 问答吧 → 请帮助,这个触发器应该怎么搞呀!

请帮助,这个触发器应该怎么搞呀!

时间:2011-11-26

来源:互联网

创建一个触发器,实现如下的完整性约束:
student表:
学号 姓名 ... 总学分
010001 王林 ... 7
010002 王明 ... 3
010003 王玉民 ... 4
SC表:
学号 课程号 成绩 学分
010001 106 80 3
010001 206 90 4
010002 106 70 3
当向SC表中插入一行数据时,自动将学分累加到总学分中。
例如上面的010001的总学分是SC表中两个学分之和,这个触发器应该怎么设置呀

作者: seeyou339621438   发布时间: 2011-11-26

自已写啊

作者: longai123   发布时间: 2011-11-26

create table Student(id varchar(15),name nvarchar(10),xuefeng int)
insert into Student values('010001','王林',0)
insert into Student values('010002','小明',0)
create table Score(id varchar(15),课程 varchar(40),成绩 int,学分 int)
insert into Score values('010001','106',80,3)
insert into Score values('010001','206',90,4)
insert into Score values('010002','106',90,3)
create trigger score_insert on score
for insert
as
declare @s_no char(10)
declare @xuefeng varchar(12)
select @s_no=student.id from student,inserted
where student.id=inserted.id
select @xuefeng=score.学分 from score where score.id=@s_no
update student set student.xuefeng=student.xuefeng+@xuefeng where student.id=@s_no

作者: longai123   发布时间: 2011-11-26

测试成功。有图有真相
 

作者: longai123   发布时间: 2011-11-26