数据库实验
1.在学生表S上创建一个触发器,要求在向S中插入数据或修改数据时,学生的年龄不得小于18岁,若小于18岁则将年龄改为18岁。
CREATE TRIGGER insert_or_update
on S
after insert ,update
as
BEGIN
if (select Age from inserted)<18
update S
set Age=18
where Sno=(select Sno from inserted)
END
2.在课程表C中创建一个触发器,要求在删除一条课程记录时,先检查该课程是否有被选修记录,若有则提示出错,并回滚Rollback。
CREATE TRIGGER C_check
on C
after delete
as
BEGIN
if (select count(*) from SC where Cno=(select Cno from inserted))>0
rollback transaction
END
3.在学生表S上创建一个触发器,要求在向S中插入一条记录时,同时将该条记录插入到表S1中。
CREATE TRIGGER _insert
on S
after insert
as
BEGIN
insert s1
values((select Sno from inserted),(select Sname from inserted),(select Sex from inserted),(select Age from inserted),(select Depno from inserted))
END