数据库习题存档

(26)创建一个触发器,当某个学生选修、退选、改选某门课程后,在YTC表中,这门课的学生人数对应发生变化;

CREATE TRIGGER T1
AFTER UPDATE ON sc
FOR EACH ROW
	INSERT into ytc
	SELECT sc.cno,t_task.Term,t_task.year,count(sc.Sno)
 	WHERE sc.cno=t_task.Cno
 	GROUP BY sc.Cno
DROP TRIGGER T1;

(27) 创建一个触发器,能实现,SC插入某条记录时,根据系统时间自动填写年度和学期信息;每学年学期以当年9.1日和3.1日为学期第一天。

CREATE TRIGGER T2
AFTER INSERT ON sc 
FOR EACH ROW
BEGIN
	DECLARE thisterm char;
	DECLARE beginyear char;
	set beginyear=YEAR(CURRENT_DATE);
	IF MONTH(CURRENT_DATE)>=3 AND MONTH(CURRENT_DATE)<=9 THEN
   set thisterm="2";
ELSE
	set thisterm="1";
END IF;

	INSERT INTO sc(year,term)
	VALUES(beginyear+"-"+beginyear+1,thisterm);
	END;

DROP TRIGGER t2;

你可能感兴趣的:(数据库)