2)存储过程:
①以学员编号为输入参数查询学员基本信息。
②以教练员编号为输入参数统计正在跟随该教练员学习的学员人数并输出。
3)触发器:
①当“学生考试信息表”中的补考次数大于2时,触发事件,级联更新“学员基本信息”中的学费,使学费增加10%。
②当“学生考试信息”表中的“是否参与/通过考试”信息被修改时,触发事件自动生成一行新的考试信息,其中规定:
1、报名完成后1个月考理论,理论考试通过1个月后考倒桩,倒桩考试通过半个月后考小路,小路考试通过半个月后考大路,如果其中某次考试未通过,补考均安排在1个月后;注意周末不考试。
2、当大路考试通过后,不再生成新的考试信息,同时级联更新“学员基本信息”表中的“顺利结业”情况。
二、概念结构设计
画出系统整体的E-R模型;并对模型中所出现的实体及属性等信息加以说明。
1)学员基本信息:
学员编号sno、姓名sname、身份证号sidno、性别ssex、出生年月sbirth、身高stature、体重sweight、左眼视力lefteye、右眼视力righteye、是否色盲colorbind、家庭住址saddr、工作单位scomp、教练员tno、学习费用sfee、照片sphoto,是否已通过全部考试success(加:
联系方式scall,计划学习日期(工作日/周末)sday),报名时间(enterday),其中学员编号为主键。
2)教练员基本信息:
教练编号tno、姓名tname、身份证号tidno、性别tsex、出生年月tbirth(加:
联系方式tcall,计划教学日期(工作日/周末)tday),其中教练员编号为主键。
3)学员考试信息:
学员编号sno、考试日期examday、考试内容(理论、倒桩、小路、大路)examinfo、补考次数bkcs、是否参加考试attend、是否通过access。
其中学员编号、考试内容、补考次数共同作为主键。
4)学员教练:
学员编号sno、教练编号tno
三、逻辑结构设计
(1)模式设计:
按系统整体E-R模型,写出关系模式;并利用数据字典加以描述。
(如每个关系模式有何属性、属性的类型、属性值的长度、是否可取空值、是否为主码、
有何约束条件等信息)
(2)子模式设计:
即针对自己的设计模块的应用需定义什么样的视图表结构;并
利用数据字典加以描述。
(如每个关系子模式有何属性、属性的类型、属性值的长度、是否可取空值、是否为主
码、有何约束条件等信息)
学员基本信息student表
字段名
数据类型
是否主键
是否为空
约束条件
默认值
描述
sno
char(8)
是
否
学员编号
tno
char(8)
否
否
教练编号
sname
varchar(10)
否
是
姓名
sidno
char(18)
否
是
身份证号
ssex
tinyint
否
是
性别
sbirth
datetime
否
是
出生年月
stature
smallint
否
是
身高
sweight
smallint
否
是
体重
lefteye
real
否
是
左眼视力
righteye
real
否
是
右眼视力
colorbind
tinyint
否
是
是否色盲
saddr
varchar(40)
否
是
家庭住址
scomp
varchar(40)
否
是
工作单位
sfee
int
否
是
4500
学习费用
sphoto
nvarchar(100)
否
是
照片
success
tinyint
否
是
是否顺利结业
scall
char(11)
否
是
联系方式
sday
tinyint
否
是
计划学习日期
enterday
datetime
否
是
报名时间
教练基本信息teacher表
字段名
数据类型
是否主键
是否为空
约束条件
默认值
描述
tno
char(8)
是
否
教练编号
tname
varchar(10)
否
是
姓名
tidno
char(18)
否
是
身份证号
tsex
tinyint
否
是
性别
tbirth
datetime
否
是
出生年月
tcall
char(11)
否
是
联系方式
tday
tinyint
否
是
计划教学日期
学员教练关系stte表
字段名
数据类型
是否主键
是否为空
约束条件
默认值
描述
Tno
Char(8)
否
是
教练编号
sno
Char(8)
是
否
学员编号
学员考试信息exam表
字段名
数据类型
是否主键
是否为空
约束条件
默认值
描述
Examday
Datetime
否
是
考试日期
Examinfo
smallint
是
否
考试内容
Bkcs
Smallint
是
否
补考次数
Sno
Char(8)
是
否
学员编号
Attend
Tinyint
否
是
是否参加考试
Access
tinyint
否
是
是否通过
四、数据库的物理设计
为了提高在表中搜索元组的速度,在实际实现的时候应该基于某些属性建立索引。
给出
所建立的索引。
(包括按哪些表中哪些属性按升序还是降序来创建索引及相应的T-SQL的创建语句)。
exam表建立了三个索引:
CREATEINDEXsyexam_examdayONexam
(examdayASC)
CREATEINDEXsyexam_examinfoONexam
(examinfoASC)
CREATEINDEXsyexam_snoONexam
(snoASC)
stte表建立了两个索引:
CREATEINDEXsystte_snoONstte
(snoASC)
CREATEINDEXsystte_tnoONstte
(tnoASC)
Student表建立了三个索引:
CREATEINDEXsystudent_enterdayONstudent
(enterdayASC)
CREATEINDEXsystudent_snameONstudent
(snameASC)
CREATEINDEXsystudent_snoONstudent
(snoASC)
五、数据库设计实现及运行
(1)数据库的创建
T-SQL语句:
/*========================================*/
/*DBMSname:
MicrosoftSQLServer2005*/
/*Createdon:
2012/1/48:
57:
37*/
/*========================================*/
ifexists(select1
fromsysobjects
whereid=object_id('"CLRTrigger_student"')
andtype='TR')
droptrigger"CLRTrigger_student"
go
ifexists(select1
fromsysobjects
whereid=object_id('td_student')
andtype='TR')
droptriggertd_student
go
ifexists(select1
fromsysobjects
whereid=object_id('ti_student')
andtype='TR')
droptriggerti_student
go
ifexists(select1
fromsysobjects
whereid=object_id('tu_student')
andtype='TR')
droptriggertu_student
go
ifexists(select1
fromsysobjects
whereid=object_id('"CLRTrigger_stte"')
andtype='TR')
droptrigger"CLRTrigger_stte"
go
ifexists(select1
fromsysobjects
whereid=object_id('td_stte')
andtype='TR')
droptriggertd_stte
go
ifexists(select1
fromsysobjects
whereid=object_id('ti_stte')
andtype='TR')
droptriggerti_stte
go
ifexists(select1
fromsysobjects
whereid=object_id('tu_stte')
andtype='TR')
droptriggertu_stte
go
ifexists(select1
fromsysobjects
whereid=object_id('exam')
andtype='U')
droptableexam
go
ifexists(select1
fromsysobjects
whereid=object_id('student')
andtype='U')
droptablestudent
go
ifexists(select1
fromsysobjects
whereid=object_id('teacher')
andtype='U')
droptableteacher
go
ifexists(select1
fromsysobjects
whereid=object_id('stte')
andtype='U')
droptablestte
go
(2)数据表的创建(可同时在创建时定义相关的约束)
T-SQL语句:
createtableexam(
examdaydatetimenull,
examinfosmallintnotnull,
bkcssmallintnotnull,
snochar(8)notnull,
attendtinyintnull,
accesstinyintnull,
constraintPK_EXAMprimarykeynonclustered(examinfo,bkcs,sno)
)
go
createtablestudent(
snochar(8)notnull,
tnochar(8)notnull,
snamevarchar(10)null,
sidnochar(18)null,
ssextinyintnull,
sbirthdatetimenull,
staturesmallintnull,
sweightsmallintnull,
lefteyerealnull,
righteyerealnull,
colorbindtinyintnull,
saddrvarchar(40)null,
scompvarchar(40)null,
sfeeintnull,
sphotonvarchar(100)null,
successtinyintnull,
scallchar(11)null,
sdaytinyintnull,
constraintPK_STUDENTprimarykeynonclustered(sno)
)
go
createtableteacher(
tnochar(8)notnull,
tnamevarchar(10)null,
tidnochar(18)null,
tsextinyintnull,
tbirthdatetimenull,
tcallchar(11)null,
tdaytinyintnull,
constraintPK_TEACHERprimarykeynonclustered(tno)
)
go
createtablestte(
tnochar(8)null,
snochar(8)notnull,
constraintPK_stteprimarykeynonclustered(sno)
)
go
(3)存储过程的定义实现(仅需给出自己设计模块中所用的)
T-SQL语句:
1、计算教练员所带学员数量的procsr_teacher
Createprocsr_teacher
@@tnochar(8),@@scountintoutput
as
begin
select@@scount=count(sno)
fromstte
wherestte.tno=@@tno
end
测试结果粘贴:
2、根据学员学号返回学员全部信息的procsrstudent
Createprocsrstudent
@@snochar(8)
as
select*fromstudents
wheres.sno=@@sno
3、根据学员学号返回学员部分信息的procrsname等(在进行修改操作时需要调用)
Createprocrsname
@@snochar(8),@@snamevarchar(10)
as
select@@sname=sname
fromstudent
wheresno=@@sno
(4)触发器的定义实现(仅需给出自己设计模块中所用的)
T-SQL语句:
1、exam表中的exam_plan触发器
Createtriggerexam_plan
onexam
afterupdate
as
begin
declare@examdaydatetime
declare@examinfosmallint
declare@bkcssmallint
declare@snochar(8)
declare@attendtinyint
declare@accesstinyint
declare@nexamdaydatetime--新插入数据行的数据
declare@nexaminfosmallint
declare@nbkcssmallint
declare@nsnochar(8)
declare@nattendtinyint
declare@naccesstinyint
declare@weekvarchar(10)--标记日期为“星期*”的
select@access=accessfrominserted
select@attend=attendfrominserted
select@examinfo=examinfofrominserted
select@sno=snofrominserted
select@bkcs=bkcsfrominserted
select@examday=examdayfrominserted
if(@access=1and@examinfo=3)
updatestudentsetsuccess=1wheresno=@sno
else
begin
if@access=1
begin
select@nexaminfo=@examinfo+1
select@nsno=@sno
select@nbkcs=@bkcs
select@nattend=0
select@naccess=0
if((@nbkcs=0and(@nexaminfo=0or@nexaminfo=1))or@nbkcs<>0)
begin
select@nexamday=@examday+30
select@week=datename(weekday,@nexamday)
while(@week='星期六'or@week='星期日')
begin
select@nexamday=@nexamday+1
select@week=datename(weekday,@nexamday)
end
end
else
begin
select@nexamday=@examday+15
select@week=datename(weekday,@nexamday)
while(@week='星期六'or@week='星期日')
begin
select@nexamday=@nexamday+1
select@week=datename(weekday,@nexamday)
end
end
end
else
begin
select@nsno=@sno
select@nexaminfo=@examinfo
select@nbkcs=@bkcs+1
select@nattend=0
select@naccess=0
if((@nbkcs=0and(@nexaminfo=0or@nexaminfo=1))or@nbkcs<>0)
begin
select@nexamday=@examday+30
select@week=datename(weekday,@nexamday)
while(@week='星期六'or@week='星期日')
begin
select@nexamday=@nexamday+1
select@week=datename(weekday,@nexamday)
end
end
else
begin
select@nexamday=@examday+15
select@week=datename(weekday,@nexamday)
while(@week='星期六'or@week='星期日')
begin
select@nexamday=@nexamday+1
select@week=datename(weekday,@nexamday)
end
end
end
insertintoexam
values(@nexamday,@nexaminfo,@nbkcs,@nsno,@attend,@access)
end
end
测试结果粘贴:
对其中学员编号为’20090003’的人进行修改:
参加考试,并且通