一个完整的数据库示例说明Word格式文档下载.docx

上传人:b****5 文档编号:17491352 上传时间:2022-12-06 格式:DOCX 页数:15 大小:294.45KB
下载 相关 举报
一个完整的数据库示例说明Word格式文档下载.docx_第1页
第1页 / 共15页
一个完整的数据库示例说明Word格式文档下载.docx_第2页
第2页 / 共15页
一个完整的数据库示例说明Word格式文档下载.docx_第3页
第3页 / 共15页
一个完整的数据库示例说明Word格式文档下载.docx_第4页
第4页 / 共15页
一个完整的数据库示例说明Word格式文档下载.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

一个完整的数据库示例说明Word格式文档下载.docx

《一个完整的数据库示例说明Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《一个完整的数据库示例说明Word格式文档下载.docx(15页珍藏版)》请在冰豆网上搜索。

一个完整的数据库示例说明Word格式文档下载.docx

wheredept='

计算机'

withcheckoption

授予d_jsj用户在计算机系教师视图t_view_jsj上的select、delete、update、insert权限:

grantselect,update,delete,insertont_view_jsjtod_jsj

计算机系学生视图t_view_jsj:

createviews_view_jsj

selectsno,sn,sex,age,dept,resume,native

froms

授予d_jsj用户在计算机系学生视图s_view_jsj上的select、delete、update、insert权限:

grantselect,update,delete,insertons_view_jsjtod_jsj

……

4、创建一个视图,显示学号,姓名,院系,课程名,成绩。

createviewscore_view(学号,姓名,院系,课程名,成绩)

selects.sno,sn,dept,cn,score

froms,sc,c

wheres.sno=sc.snoando=o

三、完整性控制--触发器、规则

1、要求当删除C表中某课程信息时,同时删除SC和TC中与此课程相关的记录。

createtriggerc_delete_triggeronc

afterdelete

deletefromsc

wherecnoin

(selectcnofromdeleted)

deletefromtc

go

2、为T创建一触发器,当职称从“讲师”晋升为“副教授”时,岗位津贴自动增加500元,从“副教授”晋升为“教授”时,岗位津贴自动增加900元。

createtriggert_update_triggeront

afterupdate

ifupdate(prof)

begin

declare@prof_oldchar(10),@prof_newchar(10)

select@prof_old=proffromdeleted

select@prof_new=proffrominserted

if@prof_old='

讲师'

and@prof_new='

副教授'

updatetsetcomm=comm+500wheretno=(selecttnofrominserted)

教授'

updatetsetcomm=comm+900wheretno=(selecttnofrominserted)

end

3、创建一个规则sexrule,指定变量@sex的取值只能为'

男'

或'

女'

createrulesexrule

as@sexin('

'

绑定T表的sex、S表的sex到sexrule规则:

execsp_bindrule'

sexrule'

s.sex'

t.sex'

四、索引

1、索引的分类:

●聚集索引:

primarykey自动创建聚集索引

●非聚集索引

2、使用索引的准则:

1)适合建索引的属性列

●主码所在的属性列

●外码所在的列或在连接查询中经常使用的属性列

●按关键字的范围值进行搜索的属性列

●按关键字的排序顺序访问的属性列

2)不适合建索引的属性列

●在查询中很少涉及的属性列

●包含较少的唯一值

●更新性能比查询性能更重要的属性列

●有text、ntext、image数据类型定义的属性列

3、为s表在dept属性列上创建索引

createindexs_dept_indexons(dept)

五、自定义数据类型、自定义函数

1、自定义数据类型Idnum:

学号、教师编号都是char(6),notnull。

execsp_addtypeIdnum,'

char(6)'

notnull'

2、自定一个标量函数,用于查询某个同学某门课程的成绩。

createfunctionscore_fun(@snamechar(8),@cnamechar(10))

returnstinyint

begin

declare@cjtinyint

select@cj=score

wheres.sno=sc.snoando=oand

sn=@snameandcn=@cname

return@cj

end

使用该函数,查询'

李忘'

选修'

计算机基础'

的成绩。

selectdbo.score_fun('

3、创建一个单语句表值函数。

createfunctioncname_score(@cnamechar(10))

returnstable

return

(selectsn,score

froms,sc,c

wheres.sno=sc.snoando=oandcn=@cname)

使用该函数,查询选修了“计算机基础”的学生姓名、成绩。

selectsn姓名,score成绩

fromcname_score('

4、创建一个多语句表值函数,根据教师姓名查询该教师所讲授课程名、学生人数、平均成绩、最高成绩、最低成绩。

createfunctiontname_fun(@tnamechar(10))

returns@tname_score_tabtable(cnchar(10),rsint,pjfnumeric(6,1),

zgfnumeric(6,1),zdfnumeric(6,1))

insertinto@tname_score_tab

selectcn,count(*),avg(score),max(score),min(score)

fromsc,tc,t,c

whereo=oandt.tno=tc.tnoando=oandtn=@tname

groupbycn

使用该函数,查询‘徐红霞’教师所授课信息。

select*fromtname_fun('

徐红霞'

六、存储过程和游标

1、利用课程名查询选修该课程的学生姓名、系别、成绩。

createprocedurecn_score_pro

@cnamechar(10)

selectsn姓名,dept系别,score成绩

wheres.sno=sc.snoando=oandcn=@cname

使用该存储过程,查询选修“计算机基础”的相关信息。

execcn_score_pro'

2、统计某门课程成绩为60以下,60~80(含60、80分),80以上各有多少人。

createprocedurecn_tjscore_pro

@cnamechar(10),@n1intoutput,@n2intoutput,@n3intoutput

select@n1=0,@n2=0,@n3=0

declareccursorfor

selectscore

fromsc,c

whereo=oandcn=@cname

openc

fetchnextfromcinto@cj

while@@fetch_status=0

begin

if@cj<

60

set@n1=@n1+1

else

if@cj<

=80

set@n2=@n2+1

else

set@n3=@n3+1

fetchnextfromcinto@cj

end

closec

deallocatec

使用该存储过程,查看“计算机基础”的相关成绩统计信息。

declare@n1tinyint,@n2tinyint,@n3tinyint

execcn_tjscore_pro'

@n1output,@n2output,@n3output

select@n1'

0--60人数'

@n2'

60--80人数'

@n3'

80--100人数'

3、利用课程名查询选修该课程的学生姓名、系别、成绩。

(使用输出参数)

创建存储过程

createproceduretn_scoretj_cursor_pro

@cnamechar(10),@ccursorvaryingoutput

set@c=cursorfor

selectsn,dept,score

fromsc,s,c

whereo=oandsc.sno=s.snoandcn=@cname

open@c

使用该存储过程,查看“计算机基础”的相关信息

declare@myccursor,@snchar(10),@deptchar(10),@scoretinyint

--调用过程tn_scoretj_cursor_pro

exectn_scoretj_cursor_pro'

@mycoutput

--打印表头

print'

姓名系别成绩'

fetchnextfrom@mycinto@sn,@dept,@score

while@@fetch_status=0

print@sn++@dept+'

'

+cast(@scoreaschar(5))

fetchnextfrom@mycinto@sn,@dept,@score

close@myc

deallocate@myc

七、事务设计

1、创建一个事务程序,要求:

学生“王蕾”打算选修“图像处理”课程,根据规定,此门课程选修的人数最多为30人,该生是否可以选修此门课程,给出结果提示。

begintransaction

declare@numtinyint,@cnochar(5),@snochar(6)

select@cno=cnofromcwherecn='

图像处理'

select@sno=snofromswheresn='

王蕾'

select@num=count(*)fromscwherecno=@cno

if@num<

30

insertintosc(sno,cno)values(@sno,@cno)

commit

print'

王蕾同学选修图像处理课程注册成功!

'

else

rollbacktransaction

选修图像处理课程的人数已满,王蕾同学不能再选修此课程'

2、将教师“王平”讲授的“计算机网络”转给“徐红霞”,将“微机原理”转给“田丰”讲授。

declare@cnochar(5),@tno1char(6),@tno2char(6)

select@tno1=tnofromtwheretn='

王平'

select@tno2=tnofromtwheretn='

计算机网络'

updatetcsettno=@tno2wheretno=@tno1andcno=@cno

田丰'

微机原理'

commit

八、数据库备份和恢复

1、数据库备份的方式:

●完全数据库备份

●执行差异备份

●执行日志备份

●执行文件/文件组备份

2、恢复数据库的方法

●从完全数据库备份中恢复

●从差异备份中恢复

●从日志备份中恢复

●从文件或文件组备份中恢复

●直接拷贝文件的备份和恢复

3、完全数据库备份与简单恢复

完全备份:

backupdatabasejxsk

todisk='

e:

\beifen\fullbackup_jxsk'

withinit

恢复:

restoredatabasejxsk

fromdisk='

withreplace

…………

九、报表*

打开“SQLServerBusinessIntelligenceDevelopmentStudio”,使用报表设计器实现的一个简单报表

 

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

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

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

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