当前位置:首页 > SQL数据库原理实验指导书及答案
本实验的主要内容是:
1)简单查询操作。该实验包括投影、选择条件表达,数据排序,使用临时表等。 2)连接查询操作。该实验包括等值连接、自然连接、求笛卡儿积、一般连接、外连接。 内连接、左连接、右连接和自连接等。
实验方法:将查询需求用T-SQL语言表示;在SQL Server Query Analyzer的输入区中输入T-SQL查询语句;设置 Query Analyzer的结果区为Standard Execute(标准执行)或Execute to Grid(网格执行)方式;发布执行命令,并在结果区中查看查询结果;如果结果不正确,要进行修改,直到正确为止。
1.基本操作实验 (1)简单查询实验 1)
在学生选课库中实现其数据查询操作。
① 求数学系学生的学号和姓名
select sno,sname from student where sdept='ma'
② 求选修了课程的学生学号
select sno from sc
where grade is not null;
③ 求选修C1课程的学生学号和成绩,结果按成绩降序排列,如成绩同按学号升序排列 select sno,grade
from sc where cno='1'
5
order by grade desc,sno asc;
④ 求选修课程C1成绩在80~90之间的学生学号和成绩,并将成绩乘以0.8输出 select sno,grade*0.85
from sc
where cno='1' and grade between 80 and 90;
⑤ 求数学或计算机系姓张的学生的信息
select * from student
where sdept in ('ma','is') and sname like '张%'
⑥ 求缺少了成绩的学生的学号和课程号
select sno,cno from sc
where grade is null;
2) 在图书借阅库中实现其查询操作。
① 将计算机类的书存入永久的计算机图书表
insert into yongjiu select * from tushu
where leibie='小说';
②将借阅日期在99年以前的借阅记录存入\\临时的超期借阅表
(2)连接查询实验
6
1) 在学生选课库中实现其数据连接查询操作。
① 查询每个学生的情况以及他(她)所选修的课程 select student.*,sc.cno
from student,sc
where student.sno=sc.sno;
② 求学生的学号、姓名、选修的课程及成绩 select student.sno,sname,cno,grade
from student,sc
where student.sno=sc.sno;
③ 求选修课程C1且成绩在90分以上的学生学号、姓名及成绩 select student.sno,sname,grade from student,sc
where student.sno=sc.sno and grade>90 and sc.cno='1'; ④ 查询每一门课的间接先行课(即先行课的先行课) select first.cno,second.cpno from course first,course second where first.cpno=second.cno;
2)在图书借阅库中实现其连接查询操作。
①查询借书者的编号、姓名、单位、所借书号、书名和借阅日期 select duzhe.bianhao,xingming,danwei,shuhao,riqi from duzhe,jieyue
where duzhe.bianhao=jieyue.bianhao 2.提高操作实验
7
l)建立职工部门库和职工、部门表,并向表中输入数据
职工表
职工号 1010 1011 1012 1014 姓名 李勇 刘晨 王敏 张立 性别 男 女 女 男 年龄 20 19 22 21 所在部门 11 14 12 13
create table 职工表 (
职工号 char(20) primary key, 姓名 char(20) unique, 性别 char(20), 年龄 smallint, 部门 char(20) );
部门表
部门号 11 12 13 14 部门名称 生产科 计划科 一车间 科研所 电话 566 578 467 create table 部门表 (
8
共分享92篇相关文档