数据库存储过程练习附答案(优.选)

2022-05-24 05:23:22   第一文档网     [ 字体: ] [ 阅读: ] [ 文档下载 ]
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。下载word有问题请添加QQ:admin处理,感谢您的支持与谅解。点击这里给我发消息

#第一文档网# 导语】以下是®第一文档网的小编为您整理的《数据库存储过程练习附答案(优.选)》,欢迎阅读!
练习,存储,答案,过程,数据库



存储过程、触发器练习

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

2s_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

exec p_cou '刘晨',@asum output print @asum

3、创建一更新触发器upd_grade,设置sc表的grade字段不能被更新,并显示信息学生成绩不能被修改,请与教务处联系

CREATE TRIGGER mes_sc ON sc FOR UPDATE AS

IF UPDATEgrade BEGIN

ROLLBACK TRAN

PRINT '学生成绩不能被修改,请与教务处联系' END



1 / 3word.




4、创建一个insert触发器uninsertstu,当在student表中插入一条新纪录时,如果是“计算机系”的学生,则撤销该插入操作,并返回“此系人数已满,不能再添加”信息。

create trigger unisnertstu on student for insert as

if exists (select * from inserted where sdept='计算机系') begin

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



5stu_info(id,sno,sname,address,phone)。其中id是自动编号。并向该表中插入一条记录(0611105,李雷,汉正街5号,1111111

2 / 3word.






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文本 --------------------- 方便更



3 / 3word.


本文来源:https://www.dywdw.cn/a6f6819ba36925c52cc58bd63186bceb18e8ed61.html

相关推荐
推荐阅读