[关闭]
@rg070836rg 2015-12-03T11:26:23.000000Z 字数 2521 阅读 1492

数据库12.03实验

数据库实验


  1. 实验十二:T-SQL(10)
  2. 1 实验目的
  3. 1 熟悉数据库的交互式SQL工具。
  4. 2 熟悉通过SQL对数据库进行操作。
  5. 3 完成作业的上机练习。
  6. 2 实验平台
  7. 利用SQLServer及其交互式工具SSMS来熟悉T-SQL
  8. 3 实验内容及要求
  9. 基于studentcoursesc表,用SQL语句实现如下要求,填写实验报告,记录所有的实验用例。
  10. 1. 创建一个视图显示学号、姓名、数据结构和数据库原理课程的成绩
  11. 2. 查询选修了全部课程的同学的学号、姓名;
  12. 3. 查询平均成绩在60分以上的同学的学号、姓名;
  13. 4. 查询平均成绩在全系平均成绩之上的同学的学号、姓名;

0 建表代码

  1. create database Students
  2. on
  3. primary(name=Students,
  4. filename='D:\test\test.mdf',
  5. size=4mb,
  6. maxsize=10mb,
  7. filegrowth=2mb
  8. )
  9. log on
  10. (name=Studentslog,
  11. filename='D:\test\testlog.ldf',
  12. size=1mb,
  13. maxsize=5mb,
  14. filegrowth=1mb
  15. )
  16. use Students;
  17. create table Student
  18. (
  19. sno char(8) not null primary key,
  20. Sname char(15) null,
  21. Ssex char(4) null,
  22. Sage int null,
  23. Sdept char(10) null
  24. )
  25. create table Course
  26. (
  27. Cno char(6) not null,
  28. Cname char(10),
  29. Cpno char(6),
  30. Ccredit int null,
  31. primary key(cno),
  32. )
  33. go
  34. create table SC
  35. (
  36. Sno char(8) not null foreign key references Student(sno),
  37. Cno char(6) not null foreign key references course(cno),
  38. primary key(Sno,Cno),
  39. Grade int null
  40. )
  41. insert into Student values('19130201','丁鹏','男',20,'Software')
  42. insert into Student values('19130202','王韵婷','女',20,'Software')
  43. insert into Student values('19130203','尹嘉琪','男',18,'Software')
  44. insert into Student values('19130204','卢冬冬','男',20,'Software')
  45. insert into Student values('19130205','史逸凡','男',19,'Software')
  46. insert into Course values('1','数据库',5,4)
  47. insert into Course values('2','数学',null,2)
  48. insert into Course values('3','信息系统',1,4)
  49. insert into Course values('4','操作系统',6,3)
  50. insert into Course values('5','数据结构',7,5)
  51. insert into Course values('6','数据处理',null,2)
  52. insert into Course values('7','PASCAL语言',6,4)
  53. insert into SC values('19130201',1,99)
  54. insert into SC values('19130202',5,95)
  55. insert into SC values('19130203',3,100)
  56. insert into SC values('19130205',1,93)
  57. insert into SC values('19130204',5,92)
  58. alter table course add foreign key(cpno) references course(cno)
  59. select * from Course;
  60. select * from SC;
  61. select * from Student;

1. 创建一个视图显示学号、姓名、数据结构和数据库原理课程的成绩

  1. create view Is_S1(sno,sname,grade1,grade2)
  2. as
  3. select a.sno,a.sname,b.Grade,c.Grade
  4. from Student a
  5. left outer join
  6. (select sno,grade from sc,course where sc.cno=cource.cno and course.cname='数据结构') b on b.sno=a.sno
  7. left outer join
  8. (select sno,grade from sc,course where sc.cno=cource.cno and course.cname='数据库') c on c.sno=a.sno

2. 查询选修了全部课程的同学的学号、姓名

  1. select a.sname,a.sno
  2. from Student a
  3. where not exists
  4. (select *
  5. from Course
  6. where not exists
  7. (select *
  8. from SC
  9. where Sno= a.Sno
  10. and Cno= Course.Cno
  11. )
  12. )

3. 查询平均成绩在60分以上的同学的学号、姓名

  1. select a.sno, b.sname,a.平均成绩
  2. from Student b,
  3. (
  4. select sno,AVG(grade) as '平均成绩'
  5. from sc
  6. group by sno
  7. having AVG(grade)>60
  8. ) a
  9. where a.sno = b.sno

4. 查询平均成绩在全系平均成绩之上的同学的学号、姓名

  1. go
  2. create view S_G(sno,sname,Gavg)
  3. as
  4. select student.sno,sname,AVG(grade)
  5. from sc,student
  6. where student.sno=sc.sno
  7. group by student.sno,student.sname
  8. go
  9. select *
  10. from S_G
  11. where Gavg>(select AVG(grade) from sc)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注