数据库实验答案.doc

上传人:罗晋 文档编号:5656103 上传时间:2020-07-20 格式:DOC 页数:8 大小:79KB
返回 下载 相关 举报
数据库实验答案.doc_第1页
第1页 / 共8页
数据库实验答案.doc_第2页
第2页 / 共8页
数据库实验答案.doc_第3页
第3页 / 共8页
数据库实验答案.doc_第4页
第4页 / 共8页
数据库实验答案.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《数据库实验答案.doc》由会员分享,可在线阅读,更多相关《数据库实验答案.doc(8页珍藏版)》请在三一文库上搜索。

1、实验3 使用T-SQL语言完成单表查询一、 实验目的掌握使用T-SQL语言完成单表查询掌握常用谓词的用法掌握where子句的用法掌握order by 子句的用法掌握group by 子句和having短语的用法二、 实验环境Microsoft SQL Server 2000。三、 实验内容和要求1. 查询全体学生的详细信息。2. 查询所有课程的详细信息。3. 查询所有选课记录的详细信息,要结果表中的列名以中文的形式显示,分别为:学号,课程号,成绩。4. 查询已被学生选修了的课程的编号。5. 查询系别编号为“d002”的学生的姓名和性别。6. 查询年龄在19至21岁或者性别为“女”的学生的学号和

2、所在系别编号。7. 查询系别编号为d001、d002和d003的学生的学号和姓名。8. 查询课程名为“C_”开头的课程名和学分。9. 某些学生入学后还没有确定所在的系,查询还没有确定系的学生的姓名。10. 查询成绩大于60分的学生的学号、课程号和成绩,并将查询结果按课程编号升序排列,同一课程的成绩按分数降序排列。11. 查询学校所开设的总的课程数。12. 计算2号课的学生成绩总和以及平均成绩,对应的列名分别为“总成绩”和“平均成绩”。13. 查询选修了3号课程的学生的最高分和最低分,对应的列名分别为“最高分”和“最低分”。14. 求各个系别的编号以及各系的人数。15. 查询选课人数大于等于2人

3、的课程编号以及选课的人数。16. 查询学生200215122选修课程的总成绩对应的列名为“总成绩”,并显示出学号。17. 查询有2门以上课程是80分以上的学生的学号及课程数。18. 查询选修了1号课的学生的学号和成绩,结果按成绩降序、学号升序排列。实验1 使用T-SQL语言建库、建表实验2 向表中增、删、改数据四、 实验目的掌握使用T-SQL建库、建表、修改表;掌握使用T-SQL对表中数据进行插入、删除和修改。五、 实验环境Microsoft SQL Server 2000。六、 实验内容和要求19. 建立数据库STDB20. 在数据库STDB中建立四个表:Student表列名类型约束或索引说

4、明SnoChar(9)主键约束学号SnameVarchar(20)非空约束姓名SsexChar(2)性别sageInt年龄deptnoChar(10)外键约束所在系别编号Department表列名类型约束或索引说明DeptnoChar(10)主键约束系别编号dnameVarchar(50)唯一索引系名称Course表列名类型约束或索引说明CnoChar(4)主键约束课程编号cnameVarchar(40)唯一约束课程名称CpnoChar(4)先行课Ccreditsmallint学分Sc表列名类型约束或索引说明SnoChar(9)外键约束,与cno共同构成主键学号CnoChar(4)外键约束,与

5、sno共同构成主键课程编号gradeint成绩21. 分别向以上四个表中增加数据。department表:Student表:Course表:SC表:22. 将学生200215122的年龄改为21岁。23. 将所有学生的所有成绩增加5分。24. 删除7号课程的记录。25. 向学生表增加新列“血型”,并记录下学生表中已存在学生的血型信息。实验4 使用T-SQL语言完成多表查询七、 实验目的掌握使用T-SQL语言完成等值连接查询掌握使用T-SQL语言完成自身连接查询掌握使用T-SQL语言完成外连接查询掌握使用T-SQL语言完成嵌套查询掌握使用T-SQL语言完成集合查询掌握常用谓词在嵌套查询中的用法八

6、、 实验环境Microsoft SQL Server 2000。九、 实验内容和要求26. 查询每个学生的学号、姓名、性别、所在系的名称、选修的课程名、学分以及成绩。27. 查询所有课程的课程编号、课程名称、学分、选修该课程的学生编号以及成绩。28. 查询选修了1号课程且不及格的学生的学号、姓名。(要求分别用连接查询和嵌套查询完成)29. 查询姓名为“刘晨”的学生选修的课程名和学分。(要求分别用连接查询和嵌套查询完成)30. 查询CS系、IS系和MA系的学生的学号、姓名和性别。31. 查询200215121号同学选修课程的总学分数,显示出姓名和总学分数。32. 找出每个学生小于他选修课程平均成

7、绩的学号和课程号。33. 查询200215121号同学所选修的课程中,成绩低于其他同学选修的某一课程成绩的课程,要求显示出其课程号和成绩。34. 查询200215121号同学所选修的课程中,成绩低于其他有成绩的同学选修的所有课程成绩的课程,要求显示出其课程号和成绩。35. 查询200215121号同学选修课程的课程号和课程名。(要求分别使用包含EXISTS谓词的嵌套查询以及连接查询完成)36. 查询既选修了课程1又选修了课程2的学生的学号。(要求使用嵌套查询完成)37. 查询选修了课程1或者选修了课程2的学生的学号。(要求分别用集合查询和多重条件查询完成)-实验1、2create databa

8、se stdb1go use stdb1gocreate table department( deptno char(10) primary key, dname varchar(50)gocreate unique index deptname on department(dname)gocreate table student( sno char(9) primary key, sname varchar(20) not null, ssex char(2), sage int, deptno char(10), foreign key (deptno) references depart

9、ment(deptno) )create table course( cno char(4) primary key, cname varchar(40) unique, cpno char(4), ccredit smallint)gocreate table sc( sno char(9), cno char(4), grade int, primary key(sno,cno), foreign key (sno) references student(sno), foreign key (cno) references course(cno) )-3insert into depart

10、ment values(d001,cs)insert into department values(d002,is)insert into department values(d003,ma)insert into student values(200215121,李勇,男,20,d001)insert into student values(200215122,刘晨,女,20,d001)insert into student values(200215123,王敏,女,20,d002)insert into student values(200215125,张立,男,20,d003)inse

11、rt into student values(200215126,陈天华,男,20,d003)insert into student values(200215128,宋阳,女,20,d002)insert into course values(1,数据库,5,4)insert into course values(2,数学,null,2)insert into course values(3,信息系统,1,4)insert into course values(4,操作系统,6,3)insert into course values(5,数据结构,7,4)insert into course

12、 values(6,数据处理,null,2)insert into course values(7,PASCAL语言,6,4)insert into course values(8,C_语言,null,4)insert into sc values(200215121,1,92)insert into sc values(200215121,2,85)insert into sc values(200215121,3,88)insert into sc values(200215121,4,72)insert into sc values(200215121,5,65)insert into

13、sc values(200215121,6,58)insert into sc values(200215121,8,68)insert into sc values(200215121,9,68)insert into sc values(200215122,2,90)insert into sc values(200215122,3,80)insert into sc values(200215126,1,null)-4update student set sage=21 where sno=200215122-5update sc set grade=grade+5-6delete fr

14、om course where cno=7-7alter table student add bloodtype varchar(10)update student set bloodtype=A where sno=200215121update student set bloodtype=B where sno=200215122update student set bloodtype=O where sno=200215123update student set bloodtype=AB where sno=200215125update student set bloodtype=A

15、where sno=200215126update student set bloodtype=B where sno=200215128-1.查询每个学生的学号、姓名、性别、所在系的名称、选修的课程名、学分以及成绩。select student.sno,sname,ssex,dname,cname,ccredit ,grade from student,course,department,scwhere student.sno=sc.sno and student.deptno=department.deptno and o=o-2.查询所有课程的课程编号、课程名称、学分、选修该课程的学生编

16、号以及成绩。select o,cname,ccredit,sc.sno,grade from sc,course where o=o-3.查询选修了1号课程且不及格的学生的学号、姓名。(要求分别用连接查询和嵌套查询完成)select sc.sno,sname,grade from sc,student where sc.sno=student.sno and cno=1 and grade60select sno,sname from student where sno in(select sno from sc where cno=1 and grade60)-4.查询姓名为“刘晨”的学生选

17、修的课程名和学分。(要求分别用连接查询和嵌套查询完成)select cname,ccredit from sc,course,student where o=o and sc.sno=student.sno and sname=刘晨select cname,ccredit from course where cno in (select cno from sc where sno in(select sno from student where sname=刘晨) )-5.查询CS系、IS系和MA系的学生的学号、姓名和性别。select sno,sname,ssex from student,

18、department where student.deptno=department.deptno and dname in (cs,is,ma)select sno,sname,ssex from student,department where student.deptno=department.deptno and (dname =csor dname=is or dname=ma)-6.查询200215121号同学选修课程的总学分数,显示出姓名和总学分数。select sname,sum(ccredit) from student,sc,course where student.sno

19、=sc.sno and o=oand sc.sno=200215121group by sname-7.找出每个学生小于他选修课程平均成绩的学号和课程号。select sno,cno from sc xwhere grade(select avg(grade) from sc y where x.sno=y.sno )-8.查询200215121号同学所选修的课程中,成绩低于其他同学选修的某一课程成绩的课程,要求显示出其课程号和成绩。select cno,grade from scwhere sno=200215121 and grade any (select grade from sc w

20、here sno200215121)select cno,grade from scwhere sno=200215121 and grade (select max(grade) from sc where sno200215121)-9.查询200215121号同学所选修的课程中,成绩低于其他有成绩的同学选修的所有课程成绩的课程,要求显示出其课程号和成绩。select cno,grade from scwhere sno=200215121 and grade all (select grade from sc where sno200215121 and grade is not nul

21、l)select cno,grade from scwhere sno=200215121 and grade (select min(grade) from sc where sno200215121 and grade is not null)-10.查询200215121号同学选修课程的课程号和课程名。(要求分别使用包含EXISTS谓词的嵌套查询以及连接查询完成)select o,cname from course,sc where o=o and sno=200215121select cno,cname from course where cno in(select cno from

22、 sc where sno=200215121)select cno,cname from course where exists (select * from sc where o=o and sno=200215121)-11.查询既选修了课程1又选修了课程2的学生的学号。(要求使用嵌套查询完成)select sno from sc where cno=1 and sno in(select sno from sc where cno=2)select sno from sc x where cno=1 and 2 in(select cno from sc y where x.sno=y

23、.sno)-12.查询选修了课程1或者选修了课程2的学生的学号。(要求分别用集合查询和多重条件查询完成)select distinct sno from sc where cno=1 or cno=2select sno from sc where cno=1unionselect sno from sc where cno=2-1select * from student-2select * from course-3select sno 学号,cno 课程号,grade 成绩 from sc-4select distinct cno from sc-5select sname,ssex f

24、rom student where deptno=d002-6select sno,deptno from student where sage between 21 and 22 or ssex=女-7select sno,sname from student where deptno=d001 or deptno=d002 or deptno=d003-8select cname,ccredit from course where cname like C_% escape -9select sname from student where deptno is null-10select

25、sno,cno,grade from sc where grade 60 order by cno,grade desc-11select count(*) from course-12select sum(grade) 总成绩,avg(grade) 平均成绩 from sc where cno=2-13select max(grade) 最高分,min(grade) 最低分 from sc where cno=3-14select deptno, count(sno) from student group by deptnoselect * from student-15select cno, count(sno) from sc group by cno having count(sno)=2select * from sc-16select sno, sum(grade) 总成绩 from sc where sno=200215122 group by sno-17select sno,count(cno) from sc where grade 80 group by sno having count(*)2-18select sno, grade from sc where cno=1 order by grade de

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

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1