触发器实验
来源:
2022-07-18 20:06:00
导读 -------1、CREATE TRIGGER tr_insertON 学生表FOR INSERTAS DECLARE @Class_ID int, @Stu_id int select top 1 @Class_
-------1、CREATE TRIGGER tr_insert
ON 学生表
FOR INSERT
AS
DECLARE @Class_ID int,
@Stu_id int
select top 1 @Class_ID=课号 from (select 课号,count(*) as total from 选课表 group by 课号) t1 order by t1.total desc
select @Stu_id=学号 from inserted
insert into 选课表 values(@Stu_id, @Class_ID, 0)
GO
--------2、
CREATE TRIGGER tr_delete
ON 学生表
FOR DELETE
AS
DECLARE @Stu_id int
select @Stu_id=学号 from deleted
if exists (select 1 from 选课表 where 学号=@Stu_id)
begin
RAISERROR ('拒绝删除学号为%d的记录,因该生有选课记录',@Stu_id)
ROLLBACK TRANSACTION
end
/*要求在删除学生信息同时,将相关表中的信息全部删除(因不知道你的表,你自己添加根据@Stu_id(学号)来在下面添加额外对应表的删除吧*/
GO
免责声明:本文由用户上传,如有侵权请联系删除!