时间:2021-07-01 10:21:17 帮助过:4人阅读
复制代码 代码如下:select distinct a.* from Score a where a.sno IN (select top 1 Score.sno from Score where Score.cno = a.cno order by grade desc)
(37)查询学习“C001”课程的学生最高分数;
复制代码 代码如下:select max(grade)最高分数from Score where cno=‘C001‘
(38)计算各个课程号与相应的选课人数;
复制代码 代码如下:select count(sno) 选课人数from Score group by cno;
(39)查询“计算机系”选修了两门课程以上的学生学号、姓名;
复制代码 代码如下:select Student.sno,sname from Student where Student.sno in
(select Student.sno from Student,Score where
sdept=‘计算机系‘and Student.sno=Score.sno group by Student.sno having count(cno)>=2);
(40)自然连接student和score表;
复制代码 代码如下:select student.*,Score.grade from student ,Score where student.sno=Score.sno;
(41)使用自身连接查询每一门课程的间接先行课(即先行课的先行课)
复制代码 代码如下: select a.cno,b.cpno from Course a,Course b where a.cpno=b.cno;
(42)使用复合条件连接查询选修“c001”号课程且成绩在90分以上的所有同学;
复制代码 代码如下: select sname,grade from student,Score where Student.sno=Score.sno and cno=‘C001‘ and grade>=90;
(43)使用复合条件连接查询每个学生选修的课程名及其成绩;
复制代码 代码如下: select Student.sno,sname,cname,grade from Course,Score,Student where Course.cno=Score.cno and student.sno=Score.sno;
(44)查询选修了全部课程的学生;
复制代码 代码如下:select Sname from Student where not exists (select * from Course where not exists(select * from Score where Sno=Student.Sno and Cno=Course.Cno))
(45)查询所有选修了C001号课程的学生学号、姓名;
复制代码 代码如下:select student.sno,sname from student,Score where student.sno=Score.sno and cno=‘C001‘;
(46)查询选修了课程C001或C007的学生学号、姓名;
[code]select student.sno,sname,cno from student,Score where student.sno=Score.sno and cno in (‘C001‘,‘C007‘);[/code]
(47)查询“计算机系”的学生及年龄不大于23岁的学生;
复制代码 代码如下:select sno ,sname,2014-year(sbirth) age ,sclass from student where sdept=‘计算机系‘ or 2014-year(sbirth)<=23;
(48)查询既选修了课程C001又选修了课程C007的所有学生学号、姓名;
复制代码 代码如下:select student.sno,sname from student,Score where student.sno=Score.sno and cno=‘C001‘ and student.sno in (select student.sno from student,Score where student.sno=Score.sno and cno=‘C007‘)
(49)查询选修了课程名为“数据库原理”的学生的学号、姓名、性别、年龄;
复制代码 代码如下:select student.sno ,sname,ssex,cname,2011-year(sbirth) age from student,Score,Course where student.sno=Score.sno and Score.cno=Course.cno and cname=‘数据库原理‘;
(50)查询其他班中比“计算机系”所有学生年龄都小的学生名单;
复制代码 代码如下:select sno,sname ,2014-year(sbirth) age from student where 2014-year(sbirth)<(select min(2014-year(sbirth)) from student where sclass=‘计61‘)and sclass !=‘计61‘;
(51)查询与“夏天”在同一个系学习的学生学号、姓名、性别、年龄;
复制代码 代码如下:select sno,sname,ssex,2014-year(sbirth) age from student where sdept=(select sdept from student where sname=‘夏天‘) and sname!=‘夏天‘
(52)建立“计算机系”学生的视图1;
复制代码 代码如下:create view view_student
as select sno,sname,ssex,sbirth,sclass from student where sclass=‘13z网络‘
(53)建立“计算机系”学生的视图2,并要求进行修改与插入时,仍须保证该视图只有“计算机系”班学生;
复制代码 代码如下:create view view_student2
as select sno,sname,ssex,sbirth,sclass from student where sclass=‘13z网络‘ with check option;
(54)建立“计算机系”选修了“C001”课程的学生的视图,定义视图名为“v_cs_C001_student1”;
复制代码 代码如下:create view v_cs_C001_student1
as select student.sno,sname,ssex,sbirth,sclass from Student ,Score where
student.sno=Score.sno and sclass=‘13z网络‘ and cno=‘C001‘;
(55)建立“计算机系”班选修了“C001”课程且成绩在90分以上的学生的视图,定义视图名为“cs_c001_student2”;
复制代码 代码如下:create view cs_c001_student2
as
select student.sno,sname ,ssex,sbirth,sclass,cno from student,Score where
student.sno=Score.sno and cno=‘C001‘ and sclass=‘13z网络‘and student.sno in (select student.sno from student,Score where student.sno=Score.sno and grade>90)
(56)定义一个反映学生年龄的视图,定义视图名为“v_birth_student”;
复制代码 代码如下:create view v_birth_student
as
select sno,sname,2014-year(sbirth) age from student
(57)将学生表中所有女生记录定义为一个视图,视图名为“v_female_student”;
复制代码 代码如下:create view v_female_student
as
select * from student where ssex=‘女‘;
(58)将学生的学号及其平均成绩定义为一个视图,视图名为“v_average_student”;
复制代码 代码如下:create view v_average_student
as
select sno,avg(grade) avscore from Score group by sno;
(59)在“计算机系”学生视图中找出年龄小于22岁的学生;
复制代码 代码如下:select * from view_student where 2014-year(sbirth)<=22;
(60)利用视图查询“计算机系”选修了“C001”课程的学生;
复制代码 代码如下:select * from v_cs_C001_student1;
(61)通过(52)中的“计算机系”视图修改某个学生的名字;
复制代码 代码如下:update view_student set sname=‘王某某‘where sno=04261001;
(62)通过(53)中的“计算机系”视图,插入一个新学生记录。
复制代码 代码如下:insert into view_student2(sno,sname,ssex,sbirth,sclass) values (‘04262004‘,‘张某某‘,‘男‘,‘1987/11/09‘,‘计‘);
(63)通过(53)中的“计算机系”视图,删除一个学生记录。
复制代码 代码如下:delete from view_student2 where sno=‘04262004‘and sname=‘张某某‘;
实验课结束了,相信通过本节课的实践操作,小伙伴们都对数据库表的操作有了更进一步的了解。
以上就是查询数据库表的基本操作,几乎涵盖了各种查询操作所遇到的情况,值得大家亲自操作一下,相信对大家的学习有所帮助。
数据库表的查询操作实践演练(实验三),数据库演练
标签: