数据库存储过程练习附答案(优.选)
存储过程、触发器练习
1、在学生选课数据库中,创建一存储过程deptmale,查询指定系的男生人数,其中系为输入参数,人数为输出参数。 create proc p_dept
@dept char(20),@mannum int output as
select @allcre=count(sno) from student where sdept=@dept and ssex='男'
declare @num int
exec p_dept '计算机系',@num output
print @num
2、在s_c数据库中,创建一个存储过程totalcredit,根据输入的学生姓名,计算其总学分。(使用输出参数)。并执行该存储过程。
create proc p_cou
@name char(10),@allcre int output as
select @allcre=sum(ccredit) from student,course,sc where student.sno=sc.sno and course.cno=sc.cno and sname=@name group by sc.sno
declare @asum int
1 / 5doc 可编辑
exec p_cou '刘晨',@asum output print @asum
3、创建一更新触发器upd_grade,设置sc表的grade字段不能被更新,并显示信息“学生成绩不能被修改,请与教务处联系”。
CREATE TRIGGER mes_sc ON sc FOR UPDATE AS
IF UPDATE(grade) BEGIN
ROLLBACK TRAN
PRINT '学生成绩不能被修改,请与教务处联系' END
4、创建一个insert触发器uninsertstu,当在student表中插入一条新纪录时,如果是“计算机系”的学生,则撤销该插入操作,并返回“此系人数已满,不能再添加”信息。 create trigger unisnertstu on student for insert as
if exists (select * from inserted where sdept='计算机系') begin
2 / 5doc 可编辑
print '此系人数已满,不能再添加' rollback transaction end
4、创建一删除触发器tri_xs,功能是当某个学生从student表中被删除时,同时也删除sc表中该学生的相关记录。
USE 学生选课 GO
CREATE TRIGGER tri_xs ON student FOR DELETE AS DELETE sc
WHERE sno IN (SELECT sno FROM DELETED) GO
5、在学生选课数据库中,创建一学生联系方式表stu_info(id,sno,sname,address,phone)。其中id是自动编号。并向该表中插入一条记录(0611105,李雷,汉正街5号,1111111)。
3 / 5doc 可编辑
6、在创建的stu_info表中,查找是否有‘王梅梅’同学,如果没有,则在此表中添加该记录(0611108,王梅梅,人民路1号,22222222),编程实现将此信息添加到表中。
declare @id int
set IDENTITY_INSERT stu_info ON
if exists(select * from stu_info where sname='王梅梅') begin
print '此人已存在.' end else begin
select @id=max(id) from stu_info set @id=@id+1 insert
into
stu_info(id,sno,sname,address,phone)
values(@id,'0611108','王梅梅','人民路1号','22222222')
end
最新文件---------------- 仅供参考--------------------已改成word文本 --------------------- 方便更改
4 / 5doc 可编辑
感谢您使用本店文档 您的满意是我们的永恒的追求! (本句可删)
------------------------------------------------------------------------------------------------------------
5 / 5doc 可编辑
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- yrrf.cn 版权所有 赣ICP备2024042794号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务