数据库实验课堂作业-1.2数据查询

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pfl_327/article/details/84027661
  					数据库实验课堂实验

1.2数据查询

内容:
1).单表查询
:查询的目标表达式为所有列,指定列或指定列的运算。
:使用DISTINCT保留字消除重复性。
:对查询结果排序和分组。
:集合分组使用集函数进行各项统计。
2).连接查询
:笛卡尔积连接和等值连接。
:自连接。
:外连接。
:复合条件连接。
:多表连接。
3).嵌套查询
:通过实验验证对子查询的两个限制条件。
:体会相关子查询和不相关查询的不同。
:考察4类谓词的用法,包括:
第一类,IN,NOT IN;
第二类,带有比较运算符的子查询;
第三类,SOME,ANY或ALL谓词的子查询;
第四类,带有EXISTS谓词的子查询。
4).集合运算
:使用保留字UNION进行集合或运算。
:采用逻辑运算符AND或OR来实现集合交和减运算。

代码:

--查询年级为2001的所有学生名称
select sname
from students
where grade=’2001’
order by sid

--查询选课成绩合格的学生的课程成绩,并把成绩换算成积点。
select tid,cid,score,’point of score’,(score-50)/10
from choices
where score>60

--查询课时是‘48’或‘64’的课程名称
select cname
from courses
where hour in(’48’,’64’)

--查询所有课程名称中含有data的课程编号
select cname
from courses
where cname like ’%data%’

--查询所有选修记录的课程号
select cid from choices

--去掉重复选项
select distinct cid from choices

--保证所有列组成的行的唯一性,而不保证单独的列的取值的唯一性
select distinct cid,tid
from choices

--查询所有老师的平均工资
select avg(salary)from teachers

--查询所有学生的编号,姓名和平均成绩,按总平均成绩降序排列
select tid,avg(score)
from choices
group by tid
order by avg(score) desc

--统计各个课程的选课人数和平均成绩
select cid,count(no),avg(score)
from choices
group by cid

--查询至少修了三门课程的学生编号
select sid
from choices
group by sid
having count(*)>3

--查询编号800009026的学生所选的全部课程名和成绩
select courses.cname,choices.score
from courses,choices
where choices.sid=’800009026’and courses.cname=’database’

--考虑子查询的形式,由外界向内部传递数据database
select sid
from choices
where ’database’ in
(
select cname
from courses
where courses.cid=choices.cid
)

--同一课程的所有学生对
select x.tid,y.tid
from choices x,choices y
where x.cid=y.cid and x.no<y.no
--不同别名来实现
select x.tid,y.tid
from choices as x,choices as y
where  x.cid=y.cid and x.no<y.no

--查询至少被两名学生选择的课程编号
select x.cid
from choices x
group by x.tid
having count(*)>2

--查询编号为850955252的学生所选修的学生编号
select y.sid
from choices as x, choices as y
where x.cid=y.cid and x.sid=’850955252’

select students.sid, students.sname, students.grade, choices.cid,

choices.score
from students join choices on students.sid = choices.sid

select students.sname, courses.cname, choices.score
from students, courses, choices

select students.sname, courses.cname, cname, choices.score
from students, courses, choices
where students.sid = choices.sid and courses.cid = choices.cid and

students.sid = ’850955252’

select *
from students
where  (
select grade
from students
where sid = ’850955252’
) = grade

select *
from students
where grade = (
select grade
from students
where sid = ’850955252’
)

select *
from students
where sid in
(
select sid
from choices
)

select cname
from courses
where cid not in
(
select cid
from choices
)

select sid, sname
from students
where sid in
(
select sid
from choices
where cid in
(
select cid
from courses
where cname = ’C++’
)
)

--
select cname
from courses
where hour = some
(
select hour
from courses
where cname = ’UML’ OR CNAME = ’C++’
)

--查询修10001课程的学生姓名
select sname
from students
where exists
(
select *
from choices x
where x.cid = ’10001’ and x.sid = students.sid
)

--查询所有课程的学生姓名
select sname
from students
where not exists
(
select *
from courses as
where not exists
(
select *
from choices as y
where y.sid = students.sid and y.cid = x.cid
)
)

--用集合查询选择课程C++或则选择Java的编号
select tid
from choices
where choices.cid
(
select courses.cid
from courses
where courese.cname =’C++’
)
union
select tid
from choices
where choices.cid =
(
select cid
from courses
where courses.cname = ’Java’
)
--采用or连接两个判断条件是否能够实现
select tid
from choices
where choices.cid =
(
select courses.cid
from courses
where courses,cname = ’C++’ OR courses.cname = ’Java’
)
--子查询返回是单值
select tid
from choices,courses
where choices.cid=courses.cid
and(where courses.cname=’C++’or courses.cname=’Java’)

--实现集合交运算,查询C++和Java的学生编号
select tid from choices
where cname=’C++’
intersect
celect sid from choices
where cname=’Java’
--SQL2000通过嵌套查询来实现集合交运算
select x.sid
from courses as x,courses as y
where (x.cid=(
select cid
from courses
shere cname=’C++’
)and y.cid=(
select cid
from courses
where cname=’Java’
)
and x.sid=y.sid

--实现集合减运算,查询选修课程C++没选java的学生编号
select sid from choices
where cname=’C++’
except
select sid from choices
where cname=’Java’
--SQL2000可以使用NOT运算符来实现
select distinct choices.csid
from(select sid
from choices,courses
where choices.cid=courses.cid
and courses.cname=’C++’
)as choices(csid)
where choices.csid not in
(select sid
from choices,courses
where choices.cid=courses.cid
and courses.cname=’Java’
)

问题:
虽然是照着课本再打,能够打出来,但是并不是太懂为什么要这么写,没有像以

前学的高级语言一样能够细致的理解,然后做出题目。其次最大的问题就是题量

太大了!

猜你喜欢

转载自blog.csdn.net/pfl_327/article/details/84027661