SQL触发器问题(数据库 高分急求解答)

2025-01-01 05:17:59
推荐回答(5个)
回答1:

1.
create or replace trigger t_test
after insert on s
for each row
begin
insert into sc(sno) values (:NEW.sno) ;
end t_test;

2.
create or replace trigger t_test_1
after delete on s
for each row
begin
delete from sc where sno=:old.sno;
end t_test;

声明一点:增加一个学生,根本就没有课程表里的东西,所以我往SC表里插的时候也就只插入了一个学号,成绩那东西本来就只在SC表中出现,你再自己改吧,
这两个触发器在我本机已经能运行了,结果正确

回答2:

这个挺难的。。。

回答3:

你用什么数据库??不用数据库语法是不同的

回答4:

可惜我只会MSSQL:
(下面的存储过程希望能对你有用,可以在MS的数据库上运行)
注意理解执行删除,更新和插入时数据库是要创建临时表Deleted和Inserted的,当然不同的DBMS(数据库)有所不同,但原理都是一样的。
只有理解了,其实这题不难!遇到问题先尝试自己查帮助或者上网搜索,实在还不知道怎么做再发帖提问会更好。

create trigger TR_Student_DELETE
on student for delete
AS
Begin
delete SC from SC ,Deleted d
where SC.Sno=d.Sno
End
go

create trigger TR_COURSE_DELETE
on course for delete
AS
Begin
delete SC from SC ,Deleted d
where SC.Cno=d.Cno
End
go

Create Trigger TR_Student_UPDATE
On student
for Update
As
if Update(Sno)
begin
Update SC
Set Sno=i.Sno
From SC , Deleted d ,Inserted i
Where SC.Sno=d.Sno
end
go

Create Trigger TR_COURSE_UPDATE
On course
for Update
As
if Update(Cno)
begin
Update SC
Set Cno=i.Cno
From SC , Deleted d ,Inserted i
Where SC.Cno=d.Cno
go

Update course
Set course.Cpno=i.Cno
From course , Deleted d ,Inserted i
Where course.Cpno=d.Cno
end
go

回答5:

1.
create trigger addSC
on S
for insert
as
insert into SC(SNO) select SNO from inserted
go

2.
create trigger delSC
on S
for delete
as
delete from SC where SNO in (select SNO from deleted)
go