[关闭]
@rg070836rg 2015-10-22T10:17:03.000000Z 字数 2110 阅读 1709

数据库10.22实验

数据库


  1. 实验七:T-SQL(5)
  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. 查询年龄在1921岁之间的同学的学号、姓名、性别,并按学号降序显示。。
  12. 3. 查询既不是英语系、数学系,也不是计算机科学系的学生的学号、姓名和性别。
  13. 4. 查询所有姓王的学生的学号、姓名和性别,并按学号升序显示。
  14. 5. 查询所有不姓刘的学生姓名。
  15. 6. 查询名字中第2个字为"敏"字的学生的学号和姓名。
  16. 7. 查所有有成绩的学生学号和课程号。

0.建表代码

  1. use Students;
  2. drop table Student;
  3. drop table Course;
  4. drop table SC;
  5. create table Student
  6. (
  7. sno char(8) not null primary key,
  8. Sname char(15) null,
  9. Ssex char(4) null,
  10. Sage int null,
  11. Sdept char(10) null
  12. )
  13. create table Course
  14. (
  15. Cno char(6) not null,
  16. Cname char(10),
  17. Cpno int null,
  18. Ccredit int null,
  19. primary key(cno),
  20. )
  21. go
  22. create table SC
  23. (
  24. Sno char(8) not null primary key,
  25. Cno int null,
  26. Grade int null
  27. )
  28. insert into Student values('19130201','丁鹏','男',20,'Software')
  29. insert into Student values('19130202','王韵婷','女',20,'Software')
  30. insert into Student values('19130203','尹嘉琪','男',18,'Software')
  31. insert into Student values('19130204','卢冬冬','男',20,'Software')
  32. insert into Student values('19130205','史逸凡','男',19,'Software')
  33. insert into Course values('1','数据库',5,4)
  34. insert into Course values('2','数学',null,2)
  35. insert into Course values('3','信息系统',1,4)
  36. insert into Course values('4','操作系统',6,3)
  37. insert into Course values('5','数据结构',7,5)
  38. insert into Course values('6','数据处理',null,2)
  39. insert into Course values('7','PASCAL语言',6,4)
  40. insert into SC values('19130201',1,99)
  41. insert into SC values('19130202',5,95)
  42. insert into SC values('19130203',3,100)
  43. insert into SC values('19130205',1,93)
  44. insert into SC values('19130204',5,92)
  45. select * from Course;
  46. select * from SC;
  47. select * from Student;

建表示例图
DQQPAUM`X~V~S4D@T1[O_1P.png-14.4kB

1. 查询所有被选的课程号及课程名,并将查询结果的列名用中文表示

  1. select Cno as '课程编号',Cname as '课程名称' from Course where Cno in
  2. (select distinct Cno from SC);

01.png-2.7kB

2. 查询年龄在19到21岁之间的同学的学号、姓名、性别,并按学号降序显示

  1. select sno,sname,ssex from Student where Sage>=19 and Sage<=21 order by sno desc

02.png-3.3kB

3. 查询既不是英语系、数学系,也不是计算机科学系的学生的学号、姓名和性别

  1. select sno,sname,ssex from Student where Sdept!='English' and
  2. Sdept!='Math' and Sdept!='Computer'

03.png-3.8kB

4. 查询所有姓王的学生的学号、姓名和性别,并按学号升序显示

  1. select sno,sname,ssex from Student where sname like '王%' order by sno

04.png-1.5kB

5. 查询所有不姓刘的学生姓名

  1. select sname from Student where sname not like '刘%'

05.png-2.2kB

6. 查询名字中第2个字为"敏"字的学生的学号和姓名

  1. select sno,sname from Student where sname like '_敏%'

06.png-0.6kB

7. 查所有有成绩的学生学号和课程号

  1. select sno,cno from SC where Grade is not null

07.png-2.3kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注