十、 触发器练习
创建触发器,当Student表中的数据发生变化时(插入、更新、删除),自动修改Dept_age表中的数据(提示,需要两个触发器)
(1)当在Student表中插入或修改学生信息时,在Dept_age更新或插入数据
CREATE TRIGGER trg_newage ON Student
AFTER INSERT, UPDATE
AS BEGIN
UPDATE Dept_age
SET Avg_age = ( SELECT AVG(Sage) FROM Student
WHERE Student. Sdept = Dept_age. Sdept)
WHERE Sdept IN ( SELECT Sdept FROM inserted );
INSERT INTO Dept_age (Sdept, Avg_age)
SELECT Sdept, AVG(Sage) FROM Student
WHERE Sdept IN ( SELECT Sdept FROM inserted ) AND
Sdept NOT IN (SELECT Sdept FROM Dept_age)
GROUP BY Sdept;
END;
2019-12-21 21:12:15
64KB
选课系统
1