鞍山师范SQL实验专升本习题.docx

上传人:b****0 文档编号:12505509 上传时间:2023-04-19 格式:DOCX 页数:30 大小:1.36MB
下载 相关 举报
鞍山师范SQL实验专升本习题.docx_第1页
第1页 / 共30页
鞍山师范SQL实验专升本习题.docx_第2页
第2页 / 共30页
鞍山师范SQL实验专升本习题.docx_第3页
第3页 / 共30页
鞍山师范SQL实验专升本习题.docx_第4页
第4页 / 共30页
鞍山师范SQL实验专升本习题.docx_第5页
第5页 / 共30页
点击查看更多>>
下载资源
资源描述

鞍山师范SQL实验专升本习题.docx

《鞍山师范SQL实验专升本习题.docx》由会员分享,可在线阅读,更多相关《鞍山师范SQL实验专升本习题.docx(30页珍藏版)》请在冰豆网上搜索。

鞍山师范SQL实验专升本习题.docx

鞍山师范SQL实验专升本习题

《SQLserver2000》课程实验

序号

内容

实验学时

实验一

数据库及表的创建和管理

2

实验二

约束的创建及使用

2

实验三

SQLServer视图及索引的创建及使用

2

实验四

SQLServer的存储过程

4

实验五

SQLServer的触发器

2

实验六

数据库的分离

2

实验附录:

上机实验一

1.使用企业管理器创建名为student的数据库,并设置数据库主文件名为student_data,大小为10MB;日志文件为student_log,大小为2MB.其他参数取默认值。

步骤:

a.

b.

c.

d.

2.创建一个student1数据库,主文件逻辑名为student1_data,物理文件名为student1.mdf,为10MB,增长速度为10%;数据库的日志文件逻辑名为syudent1_log,物理文件名为student1.ldf,初始化大小为1MB,最大尺寸为5MB,增长速度为1MB;文件存放路径为c:

\data文件夹下。

3.在student数据库中创建一个名为pupil的表,要求:

(snochar(6)notnull,snamechar(10)notnull,ssexchar

(2)notnull,birthdaydatetimenotnull,politychar(20))

步骤:

a.

b.

c.

保存并命名为pupil即可。

上机实验二

1.将pupil表的sno设置为主键。

2.为sname字段设置惟一性约束。

步骤:

3.为ssex和birthday设置检查性约束,要求ssex只能为‘男’或‘女’,birthday应该大于‘1987-1-1’。

步骤:

4.

为polity字段设置默认约束,值为‘群众’。

5.再创建一个学生选课表sc(snochar(6)notnull,cnochar(10)notnull,gradereal).

为sc表创建外键约束,把sc表的sno和pupil表的sno关联起来,在这两个表之间创建一种制约关系。

步骤:

最后保存该关系图即可。

上机实验三

1.如何通过企业管理器和Transact_SQL语句对视图进行创建、修改和删除?

通过企业管理器:

创建步骤:

最后保存该视图即可。

修改步骤:

进入该界面重新修改该视图即可。

删除步骤:

2.

通过Transact_SQL语句创建一个视图,计算各个班级的各门课程的平均分。

Createviewv_avggrade

As

Selectsubstring(pupil.sno,1,len(pupil.sno)-2)as班级,

Cnameas课程名称,

Avg(grade)as平均分

Frompupil,sc,grade

Wherepupil.sno=sc.snoando=o

Groupbysubstring(pupil.sno,1,len(pupil.sno)-2),Cname

3.通过Transact_SQL语句创建一个视图,显示‘高等数学’未过的学生的信息。

Createviewv_gradenotpass

As

Selectpupil.sno,sname,cname,grade

Frompupil,sc,course

Wherepupil.sno=sc.snoando=oandgrade<60

Andcname=’高等数学’

4.

通过Transact_SQL语句创建一个视图,查询的数据为99521班学生的考试成绩。

Createviewv_grade99521

As

Selectpupil.sno,sname,cname,grade

Frompupil,sc,course

Wherepupil.sno=sc.snoando=oand

left(pupil.sno,5)=’99521’

5.如何通过企业管理器创建索引?

步骤:

点击‘新建’按钮

点击‘确定’

点击‘关闭’

6.

如何删除索引?

选中该索引点击‘删除’即可

利用dropindex表名.索引名完成删除

上机实验四

1.创建一个存储过程stugradeinfo,查询班级、学号、姓名、性别、课程名称、分数

createprocedurestugradeinfo

as

select班级=substring(pupil.sno,1,len(pupil.sno)-2),

pupil.snoas学号,

snameas姓名,

ssexas性别,

cnameas课程名称,

gradeas分数

frompupil,sc,course

wherepupil.sno=sc.snoando=o

2.利用企业管理器创建一个存储过程stu_info,根据传入的编号,查询某学生的基本信息。

代码如下:

createprocedurestu_info

@stunumchar(10)

as

ifnotexists(select*frompupilwheresno=@stunum)

print‘查无此人!

else

begin

selectsnoas编号,

snameas姓名,

ssexas性别,

birthdayas出生日期,

polityas政治面貌

frompupil

wheresno=@stunum

end

3.创建一个存储过程stu_age,根据输入的学生姓名,计算该学生的年龄。

Createprocedurestu_age

@stunamechar(10)

as

ifnotexists(select*frompupilwheresname=@stuname)

print‘查无此人!

else

begin

declare@ageint

select@age=floor(datediff(day,birthday,getdate())/365)

frompupil

wheresname=@stuname

end

4.

创建一个存储过程stu_grade,根据输入的学生编号,返回其选课及其成绩。

Createprocedurestu_grade

@stunumchar(10)

ifnotexists(select*frompupilwheresname=@stuname)

print‘查无此人!

else

begin

ifnotexists(select*frompupil,scwherepupil.sno=sc.sno)

print‘无此学生的选课信息!

else

selectsname,cname,grade

frompupil,course,sc

wherepupil.sno=sc.snoandcourse=cno=o

end

上机实验五

1.创建一个insert触发器,当在pupil表中插入一条新记录时,给出‘你已经插入了一条新记录!

’的提示信息。

步骤:

程序代码如下:

createtriggerstuinsertonpupil

forinsert

as

declare@msgchar(50)

set@msg=’你已经插入了一条新记录!

print@msg

2.创建一个insert触发器,当在pupil表中插入一条新记录时,不允许在学号中出现重复的编号或出现空值。

程序代码如下:

createtriggerstuinsert1onpupil

forinsert

as

declare@stunumchar(10)

set@stunum=(selectsnofrominserted)

if@stunumisnull

begin

print‘学号不能为空!

rollbacktransaction

end

else

begin

declare@numint

set@num=(selectcount(*)frompupilwheresnoin(selectsnofrominserted))

if@num>1

begin

print‘已经有相同的学号,请重新确认!

rollbacktransaction

end

else

print‘数据录入成功!

end

3.创建一个insert触发器,当在sc表中插入一条新记录时,sno和cno必须是已经存在的学号和课程号,且grade应该在0----100之间。

程序代码如下:

createtriggerscinsertonsc

forinsert

as

declare@gradevaluereal

set@gradevalue=(selectgradefrominserted)

print‘触发器开始工作………………’

ifnotexists(selectsnofrominserted

wheresnoin(selectsnofrompupil))

begin

print‘无此学生的编号,请重新确认!

rollbacktransaction

end

else

begin

ifnotexists(selectcnofrominsertedwherecnoin(selectcnofromcourse))

begin

print‘无此课程的编号,请重新确认!

rollbacktransaction

end

else

begin

if@gradevalue<0or@gradevalue>100

begin

print‘分数有误,请重新确认!

rollbacktransaction

end

else

print‘数据插入成功!

end

end

4.创建一个after触发器,在pupil表中删除某学生的记录时,删除其相应的选课记录。

程序代码如下:

createtriggerstudeleteonpupil

fordelete

as

declare@stunumchar(10)

select@stunum=snofromdeleted

ifnotexists(select*frompupil,sc

wherepupil.sno=sc.snoandpupil.sno=@stunum)

print‘无此学生的选课记录!

else

begin

print‘开始查找并删除该学生相映的选课记录……..‘

deletefromscwheresno=@stunum

print‘该学生的选课记录已经删除!

end

5.创建一个insteadof触发器,当在course表中上出记录时,不允许删除course表中的数据。

程序代码如下:

createtriggernotallowdelete

oncourse

insteadofdelete

as

print‘insteadof触发器开始工作……………’

print‘course表中的数据不允许删除!

不能执行删除操作!

上机实验六

学生学会数据库分离,上机考试结果用数据库分离方法。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1