数据库原理与应用实训报告
实训报告
第一部分:实训目的
通过本次课程设计让学生能够综合运用所学的关系数据库原理知识解决并能设计一个实际问题,进一步掌握数据库原理的相关理论和数据库的设计实现过程,进一步提高学生的分析问题和解决问题的能力以及学生的动手能力,能够独立完成数据库的功能的设计和开发。
第二部分:实训准备
安装有SQL SERVER 2005以上版本的软件的计算机。
第三部分:实训要求
1.画出所给任务中表的E-R 图
2. 实训部分(全部使用T-SQL 语句完成以下实验内容)
第四部分:实训内容(步骤及程序) E-R 图
实验1 数据库操作 1.创建数据库:
操作1.1:创建一个test 数据库,其主数据文件逻辑名test_data,物理文件名test_data.mdf,初始大小10MB ,最大尺寸为无限大,增长速度1MB ;数据库日志文件逻辑名称为test_log,物理文件名为test_log.ldf,初始大小为1MB ,最大尺寸为5MB ,增长速度为10%。
create database test数据库 on (
name ='test_data',
filename ='D:\课程设计\数据库\test_data.mdf', size =10mb , filegrowth =1mb , maxsize =unlimited ) log on (
name ='test_log',
filename ='D:\课程设计\数据库\test_log.ldf', size =1mb , filegrowth =10%, maxsize =5mb ) go
2.查看数据库属性:
操作1.2:使用T-SQL 语句查看数据库test 属性
exec sp_helpdb test数据库 go
3.删除数据库:
操作1.3:使用T-SQL 语句删除数据库test drop database test数据库 go
实验2 表操作 1.创建表:
操作2.1:创建学生表:
use test数据库 go
create table student (
st_id nvarchar (9) not null primary key , st_nm nvarchar (8) not null, st_sex nvarchar (2), st_birth datetime , st_score int , st_date datetime , st_from nchar (20), st_dpid nvarchar (2), st_mnt tinyint ) go
操作2.2:创建课程信息表:
use test数据库 go
create table couse (
cs_id nvarchar (4) not null primary key , cs_nm nvarchar (20) not null, cs_tm int , cs_sc int ) go
操作2.3:创建选课表:
use test数据库
go
create table slt_couse (
cs_id nvarchar (4) not null, st_id nvarchar (9) not null, score int , sltdate datetime ) go
alter table slt_couse add constraint fk_cs_id foreign key (cs_id) references couse(cs_id) go
alter table slt_couse add constraint fk_st_id foreign key (st_id) references student(st_id) go
操作2.4:创建院系信息表:
use test数据库 go
create table dept (
dp_id nvarchar (2) not null, dp_nm nvarchar (20) not null, dp_drt nvarchar (8) , dt_tel nvarchar (12) ) go
2.修改表结构: (1)向表中添加列:
操作2.5:为“dept”表添加“dp_count”列(数据类型为nvarchar ,长度为3,允许为空)
use test数据库 go
alter table dept
add dp_count nvarchar (3) go
(2)修改列数据类型:
操作2.6:修改“dept”表的“dp_count”列数据类型为int
use test数据库 go
alter table dept
alter column dp_count int go
(3)删除表中指定列:
操作2.7:删除“dept”表的“dp_count”列
use test数据库 go
alter table dept drop column dp_count go
3.删除表
操作2.8:删除“dept”表
use test数据库 go
drop table dept go
4.向表中输入数据记录
操作2.9:分别向“student”表、“couse”表、“slt_couse”表、“dept”表中输入数据记录
use test数据库 go
insert student
values ('000000001' , ' 小二' , ' 男' , '1991-01-01' , '89' , '2011-09-01' , ' 广东' , '10' , '1' ) insert student
values ('000000002' , ' 小三' , ' 男' , '1991-01-02' , '79' , '2011-09-01' , ' 广东' , '12' , '1' ) insert student
values ('000000003' , ' 小四' , ' 女' , '1991-01-03' , '69' , '2011-09-01' , ' 广东' , '8' , '1' ) insert student
values ('000000004' , ' 小六' , ' 男' , '1991-04-01' , '59' , '2011-09-01' , ' 广东' , '13' , '1' ) insert student
values ('000000005' , ' 小七' , ' 男' , '1991-01-11' , '89' , '2011-09-01' , ' 广东' , '11' , '1' ) Go
use test数据库 go
insert couse
values ('0001' , ' 计算机网络' , '72' , '2' ) insert couse
values ('0002' , 'c 语言程序设计' , '72' , '2' ) insert couse
values ('0003' , 'SQL 数据库' , '72' , '2' ) insert couse
values ('0004' , ' 计算机导论' , '72' , '2' ) insert couse
values ('0005' , ' 高级数学' , '72' , '2' ) Go
use test数据库 go
insert slt_couse
values ('0001' , '000000001' , '72' , '2011-10-12' ) insert slt_couse
values ('0002' , '000000002' , '92' , '2011-10-12' ) insert slt_couse
values ('0003' , '000000003' , '62' , '2011-10-12' ) insert slt_couse
values ('0004' , '000000004' , '82' , '2011-10-12' ) insert slt_couse
values ('0005' , '000000005' , '52' , '2011-10-12' )
use test数据库 go
insert dept
values ('8' , ' 艺术系' , ' 王大仁' , '12345768' ) insert dept
values ('9' , ' 建筑系' , ' 李小仁' , '12645678' ) insert dept
values ('10' , ' 信息系' , ' 李大仁' , '12349878' ) insert dept
values ('13' , ' 管理系' , ' 王小仁' , '16345678' ) insert dept
values ('12' , ' 外语系' , ' 王小明' , '14512678' ) go
实验3 数据完整性 1.空值约束( NULL )
操作3.1:将student 表中的st_sex列属性更改为NOT NULL
use test数据库 go
alter table student
alter column st_sex nvarchar (2) not null go
2.默认值约束( DEFAULT )
操作3.2:将student 表中的st_from列默认值设置为“陕西省”
use test数据库 go
alter table student add constraint df_from default ' 陕西省' for st_from go
3.默认值对象
操作3.3:创建默认值对象df_today为当前日期,并将其绑定到slt_couse表中的sltdate 列,然后取消绑定,最后删除默认值对象df_today。
use test数据库 go
create default df_today as '2014-12-30' go
exec sp_bindefault 'df_today', 'slt_couse.[sltdate]' go
exec sp_unbindefault 'slt_couse.[sltdate]' go
drop default df_today go
4.检查约束( CHECK )
操作3.4:将slt_couse表中的score 列的检查约束设置为>=0且
use test数据库 go
alter table slt_couse add constraint ck_score
check (score >=0 and score
5.规则约束对象
操作3.5:创建规则约束对象rl_sex,用于检查性别的取值仅限于“男”和“女”,并将其绑定到student 表中的st_sex列,然后取消绑定,最后删除规则约束对象rl_sex。
use test数据库 go
create rule rl_sex as @sex =' 男' or @sex=' 女' go
exec sp_bindrule 'rl_sex' , 'student.st_sex' go
exec sp_unbindrule 'student.st_sex' go
drop rule rl_sex
go
6.主键
操作3.6:将dept 表中的dp_id列设置为主键
use test数据库 go
alter table dept
add constraint pk_dp_id primary key (dp_id) go
7.唯一性约束( UNIQUE )
操作3.7:将dept 表中的dp_nm列设置为唯一性约束
use test数据库 go
alter table dept add constraint ix_nm unique (dp_nm) go
8.标识列
操作3.8:向slt_couse表中添加标识列id ,第1行默认值为1,相邻两个标识列间的增量为1
use test数据库 go
alter table slt_couse
add id int IDENTITY (1, 1) not null go
9.外键( FOREIGN KEY )
操作3.9:被参照表为dept ,参照表为student
use test数据库 go
alter table student add constraint fk_dpid foreign key (st_dpid ) references dept(dp_id)
go
实验4 数据更新 1.表中插入数据 操作4.1:向dept 表插入一条记录,系号11,系名自动控制系,系主任为李其余,电话81234567
use test数据库 go
insert dept
values ('11' , ' 自动控制系' , ' 李其余' , '81234567' ) go
操作4.2:向student 表插入一条记录,学号070201001,姓名为王小五,性别为男,出生日
期1990年9月9日,系号为11,其余字段为NULL 或默认值
use test数据库 go
insert student(st_id, st_nm, st_sex, st_birth, st_dpid ) values ('070201001' , ' 王小五' , ' 男' , '1990-9-9' , '11' ) go
操作4.3:向couse 表插入一条记录,课程号1234,课程名为操作系统,其余字段为NULL 或默认值
use test数据库 go
insert couse(cs_id, cs_nm) values ('1234' , ' 操作系统' ) go
操作4.4:向slt_couse表插入一条记录,课程号1234,学名070201001,其余字段为NULL 或默认值
use test数据库 go
insert slt_couse (cs_id, st_id) values ('1234' , '070201001' ) go
2.修改表中数据
操作4.5:修改student 表记录,将王小五的入学成绩改为88
use test数据库 go
update student set st_score ='88' where st_nm=' 王小五' go
操作4.6:修改couse 表记录,将所有记录的学分改为4,学时改为64
use test数据库 go
update couse
set cs_sc='4' , cs_tm='64' go
操作4.7:修改slt_couse表记录,将课程号为1234,学名为070201001的记录的成绩改为77
use test数据库 go
update slt_couse set score= 77 where cs_id='1234' and st_id='070201001' go
3.删除表中数据
操作4.8:删除slt_couse表记录,将课程号为1234,学名为070201001的记录删除 use test数据库
go
delete slt_couse
where cs_id='1234'
and st_id='070201001'
go
操作4.9:删除couse 表记录,将课程号为1234的记录删除
use test数据库
go
delete couse
where cs_id='1234'
go
实验5 数据查询(1)——简单查询
(1)查询表中所有的列
操作5.1:查询所有系的信息
use test数据库
go
select *
from dept
go
(2)查询表中指定列的信息
操作5.2:查询所有的课程号与课程名称
use test数据库
go
select cs_id , cs_nm
from couse
(3)在查询列表中使用列表达式
操作5.3:在查询student 表时使用列表达式:入学成绩+400
use test数据库
go
select st_score+400
from student
go
(4)重新命名查询结果
操作5.4:使用AS 关键字为dept 表中属性指定列名:系号、系名、系主任、联系电话 use test数据库
go
select dp_id as ' 系号' , dp_nm as ' 系名' , dp_drt as ' 系主任' , dt_tel as ' 联系电话'
from dept
go
操作5.5:使用"="号为couse 表中属性指定列名:课程号、课程名、学时(=cs_sc*16)、学分 use test数据库
go
select ' 课程号' =cs_id, ' 课程名' =cs_nm , ' 学时=cs_sc*16'=cs_tm, ' 学分' =cs_sc from couse
go
(5)增加说明列
操作5.6:查询dept 表的系号、系名和系主任,向查询结果中插入说明列:系号、系名和系主任
use test数据库
go
select dp_id ' 系号' , dp_nm ' 系名' , dp_drt ' 系主任'
from dept
go
(6)查询列表中使用系统函数
操作5.7:显示所有学生的学号、姓名、性别和入学年份
use test数据库
go
select st_id , st_nm , st_sex , datepart(yy , st_date) ' 入学年份'
from student
go
操作5.8:显示所有学生学号、姓名、性别和班级(学号前6位)
use test数据库
go
select left(st_id , 6) ' 班级' , st_nm , st_sex , st_dpid
from student
go
(7)消除查询结果中的重复项
操作5.9:显示所有学生班级
use test数据库
go
select distinct left(st_id,6) ' 班级'
from student
go
(8)取得查询结果的部分行集
操作5.10:显示前5条学生记录信息
use test数据库
go
select top 5 *
from student
go
操作5.11:显示前25%条学生记录信息
use test数据库
go
select top 25 percent *
from student
go
操作5.12:显示前n 条学生记录信息,n 为局部变量
use test数据库
go
declare @n int
set @n = 5
select top @n *
from student
go
实验6 数据查询(2)——条件查询
1.使用关系表达式表示查询条件
操作6.1:查询dept 表中系号为11的院系信息
use test数据库
go
select *
from dept
where dp_id='11'
go
操作6.2:查询student 表中11系的学生学号、姓名、性别和所在系编号
use test数据库
go
select st_id , st_nm , st_sex , st_dpid
from student
where st_dpid ='11'
go
操作6.3:查询student 表中2008年及以后入学的学生信息
use test数据库
go
select *
from student
where datepart (yy , st_date)>='2008'
go
操作6.4:在查询student 表080808班学生的学号、姓名、性别和入学成绩
use test数据库
go
select left(st_id, 6) , st_nm , st_sex , st_score
from student
where left(st_id, 6)='080808'
go
2.使用逻辑表达式表示查询条件
操作6.5:查询student 表中非11系的学生信息
use test数据库
go
select *
from student
where not (st_dpid ='11')
go
操作6.6:查询选修了1002号课程且成绩在60以下的学生学号
use test数据库
go
select st_id
from slt_couse
where cs_id='1002'
and score
go
操作6.7:查询2007年入学的11系所有男生信息
use test数据库
go
select *
from student
where st_dpid='11'
and st_sex=' 男'
and datepart (yy , st_date)='2007'
go
操作6.8:查询11系和12系的学生信息
use test数据库
go
select *
from student
where st_dpid='11'
or st_dpid='12'
go
操作6.9:查询11系和12系所有2007年入学的学生信息
use test数据库
go
select *
from student
where (st_dpid='11' or st_dpid='12' )
and datepart (yy , st_date)='2007'
go
3.使用LIKE 关键字进行模糊查询
操作6.10:查询所有“计算机”开头的课程信息
use test数据库
go
select *
from couse
where cs_nm like ' 计算机%'
go
操作6.11:查询所有由三个字组成的“王”姓学生信息
use test数据库
go
select *
from student
where st_nm like ' 王__'
go
操作6.12:查询所有课程名中包含“信息”的课程信息
use test数据库
go
select *
from couse
where cs_id like '%信息%'
go
操作6.13:查询学生姓名介于王姓到张姓的信息
use test数据库
go
select *
from student
where st_nm like '[王-张]%'
go
4.使用Between…And关键字进行查询
操作6.14:查询在1989.7.1到1990.6.30之间出生的学生信息
use test数据库
go
select *
from student
where st_birth between '1989-7-1' and '1990-6-30'
go
操作6.15:查询选修了1001号课程且成绩在60到80之间的学生选课信息
use test数据库
go
select *
from slt_couse
where cs_id='1001'
and score between 60 and 80
go
5.使用IN 关键字进行查询
操作6.16:查询11系、12系、13系的学生信息
use test数据库
select *
from student
where st_dpid in (11, 12, 13)
go
操作6.17:查询所有张,王,李,赵姓的学生的学号、姓名、性别
use test数据库
go
select st_id , st_nm , st_sex
from student
where left(st_nm, 1) in (' 张' , ' 王' , ' 李' , ' 赵' )
go
6.使用[NOT] NULL关键字进行查询
操作6.18:查询所有生源为非空的学生信息
use test数据库
go
select *
from student
where st_from is not null
go
操作6.19:查询选修了1001号课程且成绩为空的学生选课信息
use test数据库
go
select *
from slt_couse
where cs_id='1001'
and score is null
go
实验7 数据查询(3)——查询排序与查询结果存储
操作7.1:查询课程信息,按课程名称降序排序
use test数据库
go
select *
from couse
order by cs_nm desc
go
操作7.2:查询选修了1001号课程成绩非空的学生学号和成绩,并按成绩降序排序 use test数据库
go
select st_id ,score
from slt_couse
where cs_id='1001'
and score is not null
order by score desc
操作7.3:查询11系学生学号、姓名和年龄,按年龄升序排序
use test数据库
go
select st_id , st_nm , datepart (yy , getdate ())-datepart (yy , st_birth) ' 年龄'
from student
where st_dpid='11'
order by 年龄 asc
go
操作7.4:查询学生信息,按姓名升序排序,再按系号降序排序
use test数据库
go
select *
from student
order by st_nm asc , st_dpid desc
go
操作7.5:创建学生表副本student01,仅保留学生学号、姓名和性别
use test数据库
go
select st_id , st_nm , st_sex
into student01
from student
go
操作7.6:查询陕西籍学生,将结果保存在新表st_shanxi
use test数据库
go
select *
into st_shanxi
from student
where st_from=' 陕西省'
go
操作7.7:查询选修了1001号课程学生的选课信息,按学号升序排序,将结果保存在新表 use test数据库
go
select *
into slt_couse01
from slt_couse
where cs_id='1001'
order by st_id asc
go
操作7.8:用局部变量@stage保存学生张三的年龄
use test数据库
go
declare @stage int
select @stage = datepart (yy , getdate ())-datepart (yy , st_birth) from student
where st_nm=' 张三'
go
操作7.9:用局部变量@name和@stscore保存070101班按学号排序后最后一个学生的姓名和入学成绩
use test数据库
go
declare @name nvarchar (8) , @stscore int
select @name=st_nm , @stscore=st_score
from student
where left(st_id, 6)='070101'
order by st_id desc
go
实验8 数据查询(4)——查询统计与汇总
操作8.1:查询课程总数
use test数据库
go
select count (*)
from couse
go
操作8.2:查询选修1001号课程的学生人数
use test数据库
go
select count (st_id)
from slt_couse
where cs_id='1001'
go
操作8.3:查询被选修课程的数量
use test数据库
go
select count (distinct cs_id)
from slt_couse
go
操作8.4:查询选修070101班学生的平均入学成绩
use test数据库
go
select avg (st_score)
from student
where left(st_id , 6) ='070101'
go
操作8.5:查询070101001号学生选修课程的数量、总分以及平均分
use test数据库
go
select count (cs_id) ' 总量' , sum (score ) ' 总分' , avg (score ) ' 平均分' from slt_couse
go
操作8.6:查询选修1001号课程的学生人数、最高分、最低分和平均分
use test数据库
go
select count (*) ' 学生总数' , max (score ) ' 最高分' , min (score ) ' 最低分' from slt_couse
where cs_id='1001'
go
操作8.7:求各个课程号和相应的选课人数
use test数据库
go
select cs_id , count (st_id)
from slt_couse
group by cs_id
go
操作8.8:统计各班人数
use test数据库
go
select left(st_id , 6) ' 班级' , count (st_id) ' 人数'
from student
group by left(st_id , 6)
go
操作8.9:依次按班级、系号对学生进行分类统计人数、入学平均分
use test数据库
go
select (left(st_id, 6)) ' 班级' ,(st_dpid) ' 系级' , avg (st_score) ' 入学平均分' ,count(st_id) ' 人数'
from student
group by left(st_id , 6),st_dpid
go
操作8.10:查询选修了均分在75以上的课程号及均分
use test数据库
go
select cs_id ' 课程号' , avg (score ) ' 平均分'
from slt_couse
group by cs_id
having avg (score )>75
go
操作8.11:查询选修了2门以上课程的学生学号
use test数据库
go
select st_id
from slt_couse
group by st_id
having count (*)>2
go
操作8.12:明细汇总年龄
use test数据库
go
select st_nm, datepart (yy , getdate ())-datepart (yy , st_birth) ' 平均年龄' from student
where datepart (yy , getdate ())-datepart (yy , st_birth)
compute count (st_nm),
avg (datepart (yy , getdate ())-datepart (yy , st_birth))
go 操作8.13:按班级明细汇总成绩
use test数据库
go
select st_nm ,left(st_id, 6) ' 班级' , st_score
from student
where st_score
order by left(st_id, 6)
compute count (st_nm), avg (st_score) by left(st_id, 6)
go
实验9 数据查询(5)——连接查询
操作9.1:用SQL Server形式连接查询学生学号、姓名、性别及其所选课程编号 use test数据库
go
select student. st_id, st_nm, st_sex, cs_id
from student , slt_couse
where student. st_id =slt_couse. st_id
go
操作9.2:用ANSI 形式连接查询学生学号、姓名、性别及其所选课程编号
use test数据库
go
select student. st_id, st_nm, st_sex, cs_id
from student
inner join slt_couse
on student. st_id = slt_couse. st_id
go
操作9.3:用SQL Server形式连接查询学生学号、姓名及其所选课程名称及成绩 use test数据库
go
select student. st_id , st_nm , cs_nm , score
from student, couse, slt_couse
where student. st_id=slt_couse. st_id
and couse. cs_id=slt_couse. cs_id
go
操作9.4:用ANSI 形式连接查询学生学号、姓名及其所选课程名称及成绩
use test数据库
go
select student. st_id , st_nm , cs_nm , score
from slt_couse
inner join student on student. st_id=slt_couse. st_id
inner join couse on slt_couse. cs_id=couse . cs_id
go
操作9.5:查询选修了1002课程的学生学号、姓名及1001课程成绩
use test数据库
go
select student. st_id , st_nm , score
from student , slt_couse
where student. st_id=slt_couse. st_id
and cs_id='1002'
and cs_id='1001'
go
操作9.6:查询选修了“数据结构”课程的学生学号、姓名及课程成绩
use test数据库
go
select student. st_id, st_nm, score
from student, slt_couse, couse
where student. st_id=slt_couse. st_id
and couse. cs_id=slt_couse. cs_id
and cs_nm=' 数据结构'
go
操作9.7:用左外连接查询没有选修任何课程的学生学号、姓名
use test数据库
go
select student. st_id , st_nm , score
from student left join slt_couse
on student. st_id=slt_couse. st_id
where score is null
go
操作9.8:用右外连接查询选修各个课程的学生学号
use test数据库
go
select slt_couse. st_id , couse . cs_id
from slt_couse right join couse
on couse. cs_id=slt_couse. cs_id
go
实验10 数据查询(6)——子查询
操作10.1:用子查询对各班人数进行查询(新增列)
use test数据库
go
select distinct left(st_id , 6) ' 班级' , ' 人数' =(select count (st_id) from student b
where left(student . st_id, 6)= left(b . st_id, 6))
from student
go
操作10.2:用子查询对各课程的选课人数进行查询(新增列)
use test数据库
go
select distinct slt_couse. cs_id , ' 人数' =(select count (cs_id) from slt_couse b
where slt_couse. cs_id=b . cs_id)
from slt_couse
go
操作10.3:查询选修了1002课程成绩不及格的学生的学号、姓名和性别,并按姓名升序排序
通过子查询实现:——使用IN 关键字
use test数据库
go
select st_id, st_nm, st_sex
from student
where st_id in (select st_id from slt_couse
where cs_id='1002'
and score
Order by st_nm
go
通过子查询实现:——使用比较运算符
use test数据库
go
select st_id , st_nm , st_sex
from student
where (select score from slt_couse
where student. st_id=slt_couse. st_id and cs_id='1002' )
order by st_nm
go
操作10.4:查询“东方红”同学所在班的学生信息,并按姓名降序排序
通过子查询实现:——IN 运算符
use test数据库
go
select *
from student
where left(st_id, 6) in (select left(st_id, 6) from student
where st_nm=' 东方红' )
order by st_nm desc
go
通过自连接查询实现:——JOIN
use test数据库
go
select a. st_id, a . st_nm, a . st_sex
from student a join student b
on left(a . st_id, 6)=left(b . st_id, 6)
where b. st_nm=' 东方红'
order by st_nm desc
go
操作10.5:查询其它班比070511班某一学生的1002号课程成绩高的学生信息(ANY/ALL) use test数据库
go
select *
from slt_couse
where score > any
(select score from slt_couse
where cs_id='1002' and left(st_id, 6)='070511'
and left(st_id, 6) '070511' and cs_id='1002' )
go
操作10.6:查询其它班比070511班任一学生的1002号课程成绩高的学生信息(ANY/ALL) use test数据库
go
select *
from slt_couse
where score > all
(select score from slt_couse
where cs_id='1002' and left(st_id, 6)='070511'
and left(st_id, 6) '070511' and cs_id='1002' )
go
操作10.7:查询大于等于60分且且比1003课程平均成绩低的学生课程信息(Between…And) use test数据库
go
select *
from slt_couse a
where a. score between 60 and
(select avg (b . score ) from slt_couse b
where b. cs_id='1003' )
go
操作10.8:查询系主任为“赵虎”的系的所有学生信息
通过子查询实现:——IN 运算符
use test数据库
go
select *
from student
where exists
(select * from dept
where st_dpid=dp_id and dp_drt=' 赵虎' )
go
通过子查询实现:——=运算符
use test数据库
go
select *
from student
where st_dpid =(select dp_id from dept
where dp_drt=' 赵虎' )
go
实验11 数据查询(7)——数据更新与子查询
操作11.1:将070511班所有学生信息插入到表student01(st_id,st_nm,st_sex)
use test数据库
go
insert into student01
select st_id , st_nm , st_sex
from student
where left(st_id, 6)='070511'
go
操作11.2:生成1002号课程的成绩单student02(st_id,st_nm, score)
use test数据库
go
insert into student02
select student. st_id , st_nm , score
from student, slt_couse
where student. st_id=slt_couse. st_id
and cs_id='1002'
go
操作11.3:将有不及格成绩的学生的st_mnt值更改为3
use test数据库
go
update student
set st_mnt=3
where st_id in(select distinct st_id from slt_couse
where score
go
操作11.4:将没有被选修的课程的学分更改为0
use test数据库
go
update couse
set cs_sc=0
where cs_id not in (select distinct cs_id from slt_couse )
go
操作11.5:删除5系学生的选课信息
use test数据库
go
delete slt_couse
where st_id in (select st_id from student
where st_dpid=5)
go
操作11.6:删除学分为0的选课信息
use test数据库
go
delete slt_couse
where cs_id in (select distinct cs_id from couse
where cs_sc=0)
go
实验12 事务和锁
操作12.1:对课程信息表的增、删、改操作应放在事务中对执行结果做判断,语句执行无错误后方可增、删、改操作所涉及到的数据表的记录信息。
use test数据库
go
set nocount on
print '=====原couse====='
select *
from couse
go
begin tran
declare @serror int
set @serror =0
update couse
set cs_sc=3
where cs_id='0001'
set @serror =@serror +@@ERROR
if @serror 0
begin
rollback tran
print ' 修改失败!'
end
else
begin
commit tran
print ' 修改成功!'
end
go
set nocount on
print '=====原count====='
select *
from couse
go
begin tran
declare @es nvarchar (4)
declare @nm nvarchar (8), @tm int
set @es=0
insert couse(cs_id, cs_nm, cs_tm, cs_sc)
values ('0008' , 'ASP.NET' , 78, 4)
set @es= @es +@@ERROR
set @nm='ASP.NET'
set @tm=78
if @es0
begin
rollback tran
print ' 添加失败!'
end
else
begin
commit tran
print ' 添加成功!'
end
go
set nocount on
print '=====原couse====='
select *
from couse
go
begin tran
declare @sc int
set @sc =0
delete couse
where cs_id='0001'
set @sc =@sc +@@ERROR
if @sc0
begin
rollback tran
print ' 删除失败!'
end
else
begin
commit tran
print ' 删除成功!'
end
go
select * from couse
go
实验13 索引
操作13.1:为“院系信息表”的“院系名称”字段创建非聚集唯一索引
use test数据库
go
create unique nonclustered
index ix_dept_dp_nm
on dept (dp_nm)
go
实验14 视图
操作14.1:创建视图查询某位学生的成绩
use test数据库
go
create view v_slt_couse
as
select st_id , score
from slt_couse
go
实验15 游标
操作15.1:利用游标从学生表取出所有数据
use test数据库
go
set nocount on
go
declare cs_student cursor
scroll dynamic for
select *
from student
open cs_student
fetch next from cs_student
fetch last from cs_student
fetch prior from cs_student
fetch first from cs_student
close cs_student
deallocate cs_student
go
set nocount off
go
实验16 存储过程
操作16.1:创建存储过程按照学号查询学生基本信息
use test数据库
go
create procedure p_st_id
as
select *
from student
order by st_id
go
操作16.2:创建带参数(输入、输出)的存储过程通过学号查询学生选课信息
use test数据库
go
create proc p_slt_couse
@st_id nvarchar (9), @cs_id nvarchar (4) output
as
select @st_id =st_id
from slt_couse
where cs_id=@cs_id
go
实验17 自定义函数
操作17.1:创建一个名为“f_score”的多语句表值函数,当输入课程名称时,返回学生的成绩信息
use test数据库
go
create function f_score (@nm nvarchar (20))
returns @score table
(
cs_nm nvarchar (20),
score int
)
as
begin
insert @score
select cs_nm, score
from slt_couse , couse
where slt_couse. cs_id=couse . cs_id
and cs_nm= @nm
return
end
go
实验18 触发器
操作18.1:创建触发器,当向学生表添加一条记录成功后,显示“记录添加成功!”,并且显示学生表的记录
use test数据库
go
create trigger t_student
on student
after insert
as
print ' 记录添加成功!'
select *
from student
go
操作18.2:创建触发器,当向学生表中添加一条记录时,检查学号是否存在于学生表中,如果存在则显示“该学生已存在!”,并且不做插入操作。
use test数据库
go
create trigger t_student
on student
for update
as
begin
declare @st_id nvarchar (9)
if exists (select * from student where @st_id=st_id)
rollback transaction else
print ' 该学生已存在!'
end
go
第五部分:实训结果(结果分析)
在这次课程设计中,出现过了很多错误,比如标点符号的换算以及单词的拼
错,但除了这些还出现了一些比较严重的错误,如下:
1. 在创建数据库时,由于一些标点符号的错漏,导致了整个数据库无法创
建,但修改了之后,数据库成功建立。
2. 外键的创建,由于分不清主表和从表,导致了外键创建失败,但是在看
过书了解后,问题解决了。
3. 数据查询时大量出错,比如不清楚查询条件:一些关键字查询容易导致
失误;统计和汇总不知道如何下手填写查询条件;不知道什么是SQL sever 形式和ANSI 形式连接查询导致无法做题;对于比较简单的子查询还可以做出来,但遇到难一点的子查询,必须要通过查找资料才能做出了。
4. 事务和锁的创建。当把代码写出来后,出现“事务与另一个进程被死锁
在锁资料上”,然后通过对代码多次改写和保持事务简短,问题解决。
5. 存储过程的创建。对带参数(输出、输入)的存储过程不了解,导致在
声明函数时出现了差错,在执行时错误为“未声明函数”,导致整个存储过程失败。但后来,对代码分析后,成功解决问题。
6. 在做最后一道题时,使用了“instead of 触发器”,但是按照原先所打
的代码,并未有检查学号是否存在于学生表中这过程,所以,在上网看了一些实例后,使用了“if exists „ rollback transaction else „ ”这一语句后,问题解决。
第六部分:实训心得
通过这次的课程设计,使我了解了更多数据库原理与应用这门课程,对以前不太理解和熟悉的内容有更多的掌握,在我设计的学生成绩管理系统中,虽然一开始我对这个系统怎样设计完完全全没有概念。但是如果仔细的想一下,还是可以找到出发点的。首先要建立一个完整的管理系统,就必须明白这个系统所包含的基本内容、处理一些怎样的信息等。最主要的是画出E —R 图,便可直观的看出它们关系。在设计的时候,有时候也会感到没有思路了,或许是只有一点点的头绪,可经过对图的分析和认真观察他们的关系后,便有了新的思路和闪光点。最后根据自己的整理资料将每一步都运用SQL 语言执行出来,并且看以下是否符合结果要求。我想经过这次课程设计可以更好的将所学知识运用到实际中去。也
让我加深了对数据库知识的认识和了解。
在这次实践设计过程中,我懂得了如何将所学的理论知识运用到实际中去,使得所学的知识能够融会贯通。同时,在课程设计过程中,我懂得了许多知识,增加了我对数据库的兴趣 ,今后一定努力的学习,在实践中增加自己的学习乐趣,让自己更加充实。
指导教师评语:
实训报告成绩:**
****
指导教师(签字) : 年**月**日 30