实验目的:

  1. 掌握投影查询。
  2. 掌握选择查询。

    实验内容:

    用数据导入的方式生成jxgl数据库中三张表:
  • 学生表Student,由学号(Sno)、姓名(Sname)、性别(Ssex)、年龄(Sage)、所在系(Sdept)五个属性组成,记作:

Student(Sno,Sname,Ssex,Sage,Sdept)
其中主码为Sno。姓名取值唯一,性别取值为男或女,默认为男。
Student

Sno Sname Sage Ssex Sdept
98001 钱横 18 CS
98002 王林 19 CS
98003 李民 20 IS
98004 赵三 16 MA
98005 欧阳勇 19 MA
98019 李四 18 IS
  • 课程表Course,由课程号(Cno)、课程名(Cname)、先修课号(Cpno)、学分(Ccredit)四个属性组成,记作:

Course(Cno,Cname,Cpno,Ccredit)
其中主码为Cno。Cname取值唯一
Course

Cno Cname Cpno Ccredit
1 数据库系统 5 4
2 数学分析 NULL 2
3 信息系统导论 1 3
4 操作系统原理 6 3
5 数据结构 7 4
6 数据处理基础 NULL 4
7 C语言 6 3
  • 学生SC,由学号(Sno)、课程号(Cno)、成绩(Grade)三个属性组成,记作:

SC(Sno,Cno,Grade)
其中主码为(SNO,CNO),sno参照student表的sno列,cno参照course表的cno列。grade取值范围0-100。
Sc

Sno Cno Grade
98001 1 87
98001 2 67
98001 3 90
98002 2 95
98002 3 88
98004 2 Null

对JXGL数据库进行查询。

  1. 查询全体学生的学号与姓名。

    1. select sno,sname from student
  2. 查询全体学生的姓名、学号、所在系。

    select sname,sno,sdept from student
    
  3. 查询全体学生的详细记录。

    select * from student
    
  4. 查全体学生的姓名及其出生年份。

    select sname,year(getdate())-sage as '出生年份' from student
    
  5. 查询全体学生的姓名、出生年份和所在系,要求用小写字母表示所有系名。

    select sname,year(getdate())-sage as '出生年份',lower(sdept) 'sdept' from student
    
  6. 查询选修了课程的学生学号。

    select distinct sno from sc
    
  7. 查询所有年龄在20岁以下的学生姓名及其年龄。

    select sname,sage from student where sage<20
    
  8. 查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。

    Select sname,sdept,sage From student 
    Where sage between 20 and 23
    
  9. 查询年龄不在20~23岁之间的学生姓名、系别和年龄。

    Select sname,sdept,sage From student 
    Where sage not between 20 and 23
    
  10. 查询信息系(IS)、数学系(MA)和计算机科学系(CS)学生的姓名和性别。

    Select sname,ssex From student 
    Where sdept in('IS','MA','CS')
    
  11. 查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别。

    Select sname,ssex From student 
    Where sdept not in('IS','MA','CS')
    
  12. 查询学号为98001的学生的详细情况。

    Select * From student 
    Where sno='98001'
    
  13. 查考试成绩大于等于90的学生的学号。

    Select sno From sc  
    Where grade>=90
    
  14. 查询所有姓刘学生的姓名、学号和性别。

    Select sname,sno,ssex From student 
    Where sname like '刘%'
    
  15. 查询姓”欧阳”且全名为三个汉字的学生的姓名。

    Select sname From student 
    Where sname like '欧阳_'
    
  16. 查询名字中第2个字为”阳”字的学生的姓名和学号。

    Select sname,sno From student 
    Where sname like '_阳%'
    
  17. 查询所有不姓刘的学生姓名。

    Select sname,sno,ssex From student 
    Where sname not like '刘%'
    
  18. 某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。查询缺少成绩的学生的学号和相应的课程号。

    Select sno,cno From sc 
    Where grade is null
    
  19. 查所有有成绩的学生学号和课程号。

    Select sno,cno From sc 
    Where grade is not null