[关闭]
@rg070836rg 2015-12-03T09:52:49.000000Z 字数 2670 阅读 1384

数据库11.26实验

数据库实验


  1. 实验十一:T-SQL(9)
  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. 删除“王兰”所选的全部课程;
  13. 4. 将“计算机”系的学生成绩全部清零;
  14. 5. 查询没有选修1号课程的学生姓名。
  15. 6. 查询选修了课程1或者选修了课程2的学生姓名。
  16. 7. 查询既选修了课程1又选修了课程2 的学生姓名
  17. 8. 查询选修了课程1但没有又选修了课程2的学生姓名

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. select Sdept,ssex,count(ssex)as '人数' from Student group by Sdept,ssex

2. 查询选修了课程名为“信息系统”的学生学号和姓名

  1. select b.cname,a.sname,a.sno
  2. from Student a,course b,sc c
  3. where a.sno=c.sno
  4. and b.cno=c.cno
  5. and b.cname='数据库'

3. 删除“王兰”所选的全部课程;

  1. delete from sc
  2. where sno in
  3. (select sno from Student where sname='丁鹏')

4. 将“计算机”系的学生成绩全部清零;

  1. update sc
  2. set grade = null
  3. where sno in
  4. (select sno from student where sdept ='software')

5. 查询没有选修1号课程的学生姓名。

  1. select sname from Student
  2. where sno not in
  3. (select sno from sc where cno =1)

6. 查询选修了课程1或者选修了课程2的学生姓名。

  1. select sname from Student
  2. where sno in
  3. (select sno from sc where cno ='1')
  4. union
  5. select sname from Student
  6. where sno in
  7. (select sno from sc where cno ='2')

7. 查询既选修了课程1又选修了课程2的学生姓名

  1. select sname from Student
  2. where sno in
  3. (select sno from sc where cno ='1')
  4. intersect
  5. select sname from Student
  6. where sno in
  7. (select sno from sc where cno ='2')

8. 查询选修了课程1但没有又选修了课程2的学生姓名

  1. select sname from Student
  2. where sno in
  3. (select sno from sc where cno ='1')
  4. except
  5. select sname from Student
  6. where sno in
  7. (select sno from sc where cno ='2')
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注