时间:2022-01-31 09:29:50 | 栏目:Mysql | 点击:次
基于school数据库中的student, course, sc表,创建满足下述要求的触发器:
数据库navicat
cmd界面运行
DELIMITER ; DROP TRIGGER IF EXISTS `tri_update_grade`; DELIMITER ;; CREATE TRIGGER `tri_update_grade` BEFORE UPDATE ON `sc` FOR EACH ROW begin if new.grade > 100 then delete from sc; elseif new.grade < 0 then delete from sc; end if; end ;;
DELIMITER ; DROP TRIGGER IF EXISTS `tri_insert_grade`; DELIMITER ;; CREATE TRIGGER `tri_insert_grade` BEFORE INSERT ON `sc` FOR EACH ROW begin if new.grade > 100 then delete from sc; elseif new.grade < 0 then delete from sc; end if; end ;;
DELIMITER ; DROP TRIGGER IF EXISTS `tri_insert_credit`; DELIMITER ;; CREATE TRIGGER `tri_insert_credit` BEFORE INSERT ON `sc` FOR EACH ROW begin SELECT sum(Ccredit) INTO @sumcred from Student join SC on SC.Sno=Student.Sno join Course on Course.Cno=SC.Cno WHERE sc.Sno=new.Sno; if (@sumcred>30) then delete from sc; end if; end ;;
DELIMITER ; DROP TRIGGER IF EXISTS `tri_grade`; DELIMITER ;; CREATE TRIGGER `tri_grade` BEFORE UPDATE ON `sc` FOR EACH ROW begin select grade INTO @gra from sc where sno=new.sno and sno=new.sno; if (@gra >= 60) then set new.grade=20; else delete from sc; end if; end ;;
DELIMITER ; DROP TRIGGER IF EXISTS `tri_student`; DELIMITER ;; CREATE TRIGGER `tri_student` BEFORE INSERT ON `student` FOR EACH ROW begin select avg(Sage) INTO @avgage from student; SET @old_age = new.Sage; if @old_age IS NULL then set new.Sage=@avgage; end if; end ;;
DELIMITER ; DROP TRIGGER IF EXISTS `tri_comment`; DELIMITER ;; CREATE TRIGGER `tri_comment` BEFORE UPDATE ON `student` FOR EACH ROW begin select avg(Grade) INTO @avggra from sc where sno=new.sno; if @avggra > 85 then update sc set new.Savg=@avggra,new.Scomment="you"; else if @avggra > 60 and @avggra <= 85 then update sc set new.Savg=@avggra,new.Scomment="you"; else update sc set new.Savg=@avggra,new.Scomment='cha'; end if; end if; end ;; DELIMITER ;