数据库实验报告四(单表查询)

实验内容

1、 单表查询

Select 所有列 select * from s –select Sno。。。from s/*更改列的位置*/

        某些列 更改列名Select year()-Sage as 出生年,Sname from s    /*得到年*/

对行查询

2、 连接查询

3、 嵌套查询

4、 集合查询

实验过程

下面显示四个表

select * from s

select * from p

select * from j

select * from spj

1得到出生年

Select year(2007)-Sage as 出生年,Sname from student

2查询所有课程,列名用汉语显示

Select Cname as 课程名from Course

3查询课程名和学分

select Cname as 课程名,Ccredit as 学分 from Course

4 查询课程平均学分

select avg(Ccredit) as 平均学分 from Course

5 查询课程学分最高的

select Cname as 课程学分最高的课程 from Course where Ccredit=max(Ccredit)

/*错误 聚合不应出现在 WHERE 子句中,除非该聚合位于 HAVING 子句或选择列表所包含的子查询中,并且要对其进行聚合的列是外部引用。*/

select max(Ccredit) as 课程学分最高的 from Course

查询课程学分最高的课程名

select cname from course

where ccredit=

   (select max(ccredit)

    from course)

6查询选修了课程的学生学号(消除重复行用distinct

select distinct Sno from SC/*如果不加distinct 则默认为all*/

7 查询计算机科学系全体学生的名单

select Sname from student where Sdept='CS'

8 查询所有年龄在20岁以下的学生姓名及年龄

select Sname,Sage from student where Sage<20

9 查询考试成绩有小于90分的学生的学号

select distinct Sno from SC where Grade<90

10 查询年龄在20-23岁(包括2023岁)之间的学生的姓名、系别和年龄

select Sname,Sdept,Sage from student where Sage between 20 and 23

select Sname,Sdept,Sage from student where Sage not between 20 and 23

11 查询学号=9500195002的信息

select * from Student where Sno='95001' or Sno='95002'

select * from Student where Sno in('95001','95002')

select * from Student where Sno not in('95001','95002')

12 查询所有姓刘的学生的姓名、学号和性别。

Select Sname,Sno,Ssex from Student where Sname like '%'/*%通配所有_统配单个字符*/

Select Sname,Sno,Ssex from Student where Sname not like '__'/*查去掉(名字2个的姓刘的汉字两个字节)

13 查询DB_Design课程的课程号和学分

Select Cno,Ccredit from Course Where Cname like 'DB/_Design' escape'/' /*指定/为转义字符*/

14 查询所有有成绩的学生学号和课程号

Select Sno,Cno from Sc where Grade is not null

Select Sno,Cno from Sc where Grade is null/*is不能换成=*/

15 排序 先按Sno升序排列 再按(如果Sno相同)Grade降序排列

select * from SC order by Sno Asc,Grade desc

/*Order by 1 asc,列2 desc,列3+4 asc (asc升序)*/

 

实验中的问题的排除与总结:

1go可以不加但是要注意顺序 注:go --注释             提示错误

2、用year函数时要有参数

 

 

你可能感兴趣的:(数据库,Go)