数据库系统实验

一、数据库管理系统软件的使用

1、实验概要

        创建用于学生管理的数据库,数据库名为xsgl,包含学生的基本信息,课程信息和选课信息。数据库xsgl包含下列3个表:

  • student:学生基本信息;
  • course:课程信息表;
  • sc:学生选课表。

2、实验步骤

(1)建立xsgl数据库

  • 启动SQL Server Management Studio;
  • 选择树形菜单“数据库”,点击鼠标右键,出现弹出式菜单,选择新建数据库;
  • 分别设置数据库xsgl的属性,点击确定按钮,完成数据库的创建。

(2)建立student、course、sc表

  • 选择xsgl数据库树形菜单,选择表,在表逻辑对象中点击鼠标的右键,在弹出式菜单中选择新建表,启动表设计器;
  • 在表设计器中建立表结构,保存为student;
  • 继续按要求分别建立course表和sc表。

(3)在表中添加记录

  • 选择student表,按右键,选择“编辑前200行”,进入表中;
  • 输入学生表中的记录,一行数据输入完成后,单击下一行的行标记(有*号), 选定这一行,即完成输入;
  • 依次按相同方法建立course表和sc表,并输入其中的记录。

(4)修改表中的数据

        在表格中将相应的数据修改即可。

(5)删除表中的记录

        单击左边的行标记, 选定某一行, 或单击后拖动选择相邻的多行, 再右击鼠标选择弹出式菜单中的删除。

3、实验结果

4、问题分析

        SQL SERVER数据库主要由哪些逻辑对象组成?物理数据库文件包括哪些文件?

答: 逻辑对象包括表、视图、存储过程、函数等;物理数据库文件包括日志文件和数据文件:

  • 日志文件分为主要日志文件和次要日志文件LDF;
  • 数据文件分为主数据文件MDF和次要数据文件NDF。

二、数据库的建立和维护

1、实验概要

        创建学生管理数据库,数据库名为xsgl,包含学生的基本信息,课程信息和选课信息。数据库xsgl包含下列3个表:

  • student:学生基本信息;
  • course:课程信息表;
  • sc:学生选课表。

2、实验步骤

(1)建立数据库

        ① 启动SQL Server Management Studio,点击“新建查询”;

        ② 在SQL SERVER中,输入SQL语句:

CREATE DATABASE xsgl
ON (NAME='XSGL_DATA', FILENAME='E:\XSGL.MDF', SIZE=10MB, MAXSIZE=50MB, FILEGROWTH=5%)
LOG ON (NAME='XSGL_Log', FILENAME='e:\XSGL_Log.ldf', SIZE=2MB, MAXSIZE=5MB, FILEGROWTH=1MB)

        ③  点击“执行”,执行命令。

(2)建立数据表

use xsgl
Create table student(sno CHAR(10), sname CHAR(10), ssex CHAR(2), sage INT, sdept CHAR(4))
go
Create table course(cno	CHAR(3), cname CHAR(30), credit INT, pcno CHAR(3) NULL)
go
Create table sc(sno CHAR(10), cno CHAR(3), grade INT NULL)
go

(3)添加表数据

insert into student(sno,sname,ssex,sage,sdept) values('95001','李勇','男', 20,'CS') ......
insert into course(cno,cname,credit,pcno) values('1','数据库',4,'5') ......
insert into sc(sno,cno,grade) values('95001','1',92) ......

(4)修改表数据

        ① 将所有学生的年龄增加一岁:

update student set sage = sage + 1

        ② 将4号课程的学分改为4:

update course set credit = 4 where cno = 4

        ③ 设置7号课程没有先行课:

update course set pcno = null where cno = 7

        ④ 将95001号学生的1号课程的成绩增加3分:

update sc set grade = grade + 3 where sno = 95001 and cno = 1

(5)删除表数据

        ① 删除学号为95005的学生的记录:

delete from student where sno = 95005

        ② 删除所有的课程记录:

delete from course

        ③ 删除成绩为不及格(少于60分)的学生的选课记录:

delete from sc where grade < 60

3、实验结果

(1)添加数据

(2)修改数据

(3)删除数据

4、问题分析

        比较用可视化界面与命令方式在数据的插入、修改、删除方面的优缺点。

答:可视化界面,可以轻易上手,操作性高,不容易出错,设计工作量相对大,但因为现在的设计语言或工具都可以可视化,所以缺点不明显;命令方式,需要操作人员的熟练度,操作性差,容易出错;

三、数据库的简单查询和连接查询

1、实验概要

        通过操作数据库中的数据表,掌握简单查询操作和连接查询操作。

2、实验步骤

(1)单表查询

        ① 查询全体学生的学号和姓名

select sno, sname from student

        ② 查询全体学生的所有信息

select * from student
或 select sno, sname, ssex,sage, sdept from student

        ③ 查询全体学生的姓名, 出生年份,和所在系, 并用小写字母表示所有系名

select sname, '出生年份为: ', year(getdate()) - sage, lower(sdept) from student

        ④ 给上例的结果集指定列名

select sname, '出生年份为: '出生, year(getdate()) - sage 年份, lower(sdept) 系名from student

        ⑤ 查询选修了课程的学生的学号

select distinct sno from sc
比较:select sno from sc

        ⑥ 查询年龄在20岁以下的学生的姓名及其年龄

select sname, sage from student where sage < 20

        ⑦ 查询考试成绩有不及格的学生的学号

select distinct sno from sc where grade<60
比较:select sno from sc where grade < 60

        ⑧ 查询年龄在20-30岁直接的学生的姓名, 姓名, 所在系

select sname, ssex, sdept from student where sage between 20 and 30

        ⑨ 查询IS,CS,MA系的所有学生的姓名和性别

select sname, ssex from student where sdept in ('IS', 'MA','CS')

        ⑩ 查找所有姓’李’的学生的姓名, 学号和性别

select sname, sno, ssex from student where sname like '李%'
比较:将学生表中的’95001’号学生的姓名’李勇’改为’李勇勇’, 再执行:
select sname, sno, ssex from student where sname like '李_'

        ⑪ 查询没有先行课的课程的课程号cno和课程名cname

select cno, cname from course where pcno is null

(2)查询结果排序

        ① 查询选修了3号课程的学生的学号和成绩, 并按分数降序排列

select sno, grade from sc where cno = '3' order by grade DESC

        ② 查询全体学生的情况,查询结果按所在系号升序排列, 同一系中的学生按年龄降序排列

select * from student order by sdept ASC, sage DESC

(3)连接查询

        ① 查询每个学生及其选修课程的情况

select student.*, sc.* from student, sc where student.sno = sc.sno
比较:笛卡尔集: select student.*, sc.* from student, sc
比较:自然连接: select student.sno, sname, ssex, sdept, cno, grade from student, sc where student.sno = sc.sno

        ② 查询每一门课程的间接先行课(只求两层即先行课的先行课)

select First.cno, Second.pcno 间接先行课 from course First, course Second where First.pcno=Second.cno
比较:select First.cno, Second.pcno 间接先行课 from course First, course Second where First.pcno = Second.cno and Second.pcno is not null

        ③ 列出所有学生的基本情况和选课情况, 若没有选课,则只列出基本情况信息

select s.sno, sname, ssex, sdept, cno, grade from student s, sc sc where s.sno* = sc.sno

        ④ 查询每个学生的学号, 姓名, 选修的课程名和成绩

select S.sno, sname, cname, grade from student S, course C, sc SC where S.sno = SC.sno and C.cno = SC.cno

3、实验结果(部分)

4、问题分析

        如何求出不及格学生的学号, 姓名, 不及格的课程名以及成绩。

select sno, sname,cno, grade from student, sc where student.sno = sc.sno and grade < 60

四、数据库的嵌套查询实验

1、实验概要:

        使用IN、比较符、ANY或ALL和EXISTS操作符进行嵌套查询操作。

2、实验步骤:

(1)使用带IN谓词的子查询

        ① 查询与’刘晨’在同一个系学习的学生的信息

select * from student where sdept in (select sdept from student where sname = '刘晨')
比较:select * from student where sdept = (select sdept from student where sname = '刘晨')
比较:select * from student where sdept = (select sdept from student where sname = '刘晨') and sname <> '刘晨'
比较: select S1.* from student S1, student S2 where S1.sdept=S2.sdept and S2.sname = '刘晨'

        ② 查询选修了课程名为’信息系统’ 的学生的学号和姓名

select sno, sname from student where sno in (select sno from sc where cno in (select cno from course where cname = '信息系统'))

        ③ 查询选修了课程’1’和课程’2’的学生的学号

select sno from student where sno in (select sno from sc where cno = '1') and sno in (select sno from sc where cno = '2')
比较: 查询选修了课程’1’或课程’2’的学生的sno:
select sno from sc where cno = '1' or cno = '2'
比较:连接查询:
select A.sno from sc A, sc B where A.sno = B.sno and A.cno = '1' and B.cno = '2'

(2)使用带比较运算的子查询

        查询比’刘晨’年龄小的所有学生的信息

select * from student where sage < (select sage from student where sname = '刘晨')

(3) 使用带Any, All谓词的子查询

        ① 查询其他系中比信息系(IS)某一学生年龄小的学生姓名和年龄

select sname, sage from student where sage < Any (select sage from student where sdept = 'IS') and sdept <> 'IS'

        ② 查询其他系中比信息系(IS)学生年龄都小的学生姓名和年龄

select sname, sage from student where sage < ALL (select sage from student where sdept = 'IS') and sdept <> 'IS'

        ③ 查询与计算机系(CS)系所有学生的年龄均不同的学生学号, 姓名和年龄

select sno, sname, sage from student where sage <> all (select sage from student where sdept = 'CS')

(4)使用带Exists谓词的子查询和相关子查询

        ① 查询与其他所有学生年龄均不同的学生学号, 姓名和年龄

select sno, sname, sage from student A where not exists (select * from student B where A.sage = B.sage and A.sno <> B.sno)

        ② 查询所有选修了1号课程的学生姓名

select sname from student where exists (select * from sc where sno = student.sno and cno = '1')

        ③ 查询没有选修了1号课程的学生姓名

select sname from student where not exists (select * from sc where sno = student.sno and cno = '1')

        ④ 查询选修了全部课程的学生姓名

select sname from student where not exists (select * from course where not exists (select * from sc where sno = student.sno and cno = course.cno))

        ⑤ 查询至少选修了学生95002选修的全部课程的学生的学号

select distinct sno from sc A  where not exists (select * from sc B where sno = '95002'and not exists (select * from sc C where sno = A.sno and cno = B.cno))

        ⑥ 求没有人选修的课程号cno和cnamecname

select cno, cname from course C where not exists (select * from sc where sc.cno = C.cno )

        ⑦ 查询满足条件的(sno,cno)对, 其中该学号的学生没有选修该课程号cno的课程

select sno, cno from student, course where not exists (select * from sc where cno = course.cno and sno = student.sno)

        ⑧ 查询每个学生的课程成绩最高的成绩信息(sno,cno,grade)

select * from sc A where grade = (select max(grade) from sc where sno = A.sno)

3、实验结果(部分)

 

4、问题分析

        如何查询所有学生都选修了的课程的课程号cno?

select cou.cno from (select cno,count(*) as c_snum from sc group by cno) as cou,(select count(*) as snum from student) as stu where stu.snum = cou.c_snum
  • 18
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有为肥宅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值