delimiter $$
create PROCEDURE count_procedure()
BEGIN
select count(*)
from students
where year(NOW())-year(sbirthday)>25;
END $$
delimiter ;
call count_procedure();
delimiter $$
create PROCEDURE name_procedure(in xh char(12))
BEGIN
SELECT sname,year(NOW())-year(sbirthday),sdept
from students
where sno=xh;
END $$
delimiter ;
call name_procedure('2015001');
delimiter $$
CREATE PROCEDURE stu_procedure(in xh char(12))
BEGIN
select count(*)
from score
WHERE sno=xh and grade<60;
END $$
delimiter ;
call stu_procedure('2015005');
delimiter $$
CREATE PROCEDURE stu_info_procedure
(
in type char(1),
out info char(14))
BEGIN
IF type='1' THEN
SELECT count(*) into info from students where sex='男';
ELSEIF type='2' THEN
SELECT count(*) into info from students where sex='女';
ELSE
SET info='Error Input!';
END IF;
END $$
delimiter ;
CALL stu_info_procedure('3',@msg);
SELECT @msg;
delimiter $$
CREATE PROCEDURE grade_style
(
in xh char(12),
in kch char(6))
BEGIN
declare cj decimal(5,2);
SELECT grade into cj from score where sno=xh and cno=kch;if cj>=90 and cj<=100 THEN
update score set style='优秀' where sno=xh and cno=kch;
elseif cj>=80 and cj<90 THEN
update score set style='良好' where sno=xh and cno=kch;
elseif cj>=70 and cj<80 THEN
update score set style='中等' where sno=xh and cno=kch;
elseif cj>=60 and cj<70 THEN
update score set style='及格' where sno=xh and cno=kch;
ELSE
update score set style='不及格' where sno=xh and cno=kch;
end IF;
END $$
delimiter ;
call grade_style('2015006','c05');[如果不定义cj将grade赋值给cj,则要每次比较都执行一遍select语句:
if(select grade from score where sno=xh and cno=kch)>=90 and (select grade from score where sno=xh and cno=kch)<=100 THEN...]
delimiter $$
CREATE PROCEDURE scholarship
(
in xh char(12),
out msg varchar(20))
BEGIN
ifexists(select * from score where sno=xh and grade<60) THEN
SET msg='该生无参评奖学金资格';
ELSEIF (select avg(grade) from score where sno=xh)<75 THEN
SET msg='该生无参评奖学金资格';
ELSE
SET msg='该生可参评奖学金';
END IF;
END $$
delimiter ;
call scholarship('2015006',@str);
select @str;
delimiter $$
CREATE PROCEDURE xuanke
(
in xh char(12),
in kch char(6),
out msg varchar(20))
BEGIN
IF exists(select * from score where sno=xh and cno=kch) THEN
set msg='以前选过,不能再选!';
ELSEIF exists(select * from selcourses where sno=xh and cno=kch) THEN
set msg='本次选过,不能再选!';
ELSEIF (select count(*) from selcourses where sno=xh)>=5 THEN
SET msg='已经达到5门,不能再选';
ELSE
INSERT INTO selcourses(sno,cno)VALUES(xh,kch);
END IF;
END $$
delimiter ;
call xuanke('2015001','c09',@str);
select @str
触发器-习题
创建触发器,当删除某学生信息时,该生的成绩信息随之删除
delimiter $$
CREATE trigger delete_stu
after delete on students
for each ROW
begin
delete from score
where sno=old.sno;
END $$
delimiter ;
delimiter $$
CREATE trigger update_score
before update on score
for each ROW
BEGIN
IF old.grade>=60 and new.grade<60 THEN
SET new.grade=old.grade;
ELSEIF new.grade>old.grade*1.2 THEN
SET new.grade=old.grade;
END if;
END $$
delimiter ;
测试:
update score set grade=70
where sno='2015003' and cno='c04';
创建触发器,向成绩表添加数据时保证成绩在0-100之间小于0取0,大于100取100。
delimiter $$
CREATE trigger ssss
before insert on score
for each ROW
BEGIN
IF new.grade>100 THEN
SET new.grade=100;
ELSEIF new.grade<0 THEN
set new.grade=0;
END if;
END $$
delimiter ;
如果学生成绩及格,则获得相应课程的学分
delimiter $$
CREATE trigger insert_score
after INSERT on score
for each ROW
BEGIN
declare xf int;
IF new.grade>=60 THEN
BEGIN
select credit into xf from courses where cno=new.cno;
IF (SELECT scredit from students where sno=new.sno)is NULL THEN
update students set scredit=xf where sno=new.sno;
ELSE
update students set scredit=scredit+xf where sno=new.sno;
END if;
END ;
END if;
END $$
delimiter ;
测试:
insert into score(sno,cno,grade)values('2015005','c02',80);