实验4——SQL语言:SELECT查询操作

《数据库系统实验》

实验报告

题目

实验4——SQL语言:SELECT查询操作

一、实验环境:

1、操作系统:Windows 11 22H2;

2、DBMS :mysql 8.0 CE;

二、实验内容与完成情况:

4.0 数据库创建、表单创建与数据插入

按照实验3创建数据库的模板创建jxgl数据库:

create database jxgl;

use jxgl;

创建相应的表:

创建student表,student(sno,sname,ssex,sage,sdept):以sno为主键primary key:

create table student  

( sno CHAR(7) primary key,  

  sname varchar(20),  

  sage INT,  

  ssex varchar(2),  

  sdept varchar(2)

);  

创建course表,course(cno,cname,cpno,ccredit): 以cno为主键primary key,cpno默认为NULL:

create table course  

( cno CHAR(2) primary key,  

  cname varchar(20),  

  cpno char(2default NULL,  

  ccredit INT

); 

创建sc表,sc(sno,cno,grade):以(sno,cno)为主键,sno为外码参照student表中的sno,cno为外码参照course表中的cno:

create table sc  

( sno CHAR(7),  

  cno CHAR(2),  

  grade INT,  

  primary key(sno,cno),  

  foreign key(sno) references student(sno),  

  foreign key(cno) references course(cno)

) ;

插入数据:

插入student的数据:

insert into student  

values('2005001','钱横',18,'','Cs'),  

      ('2005002','王林',19,'','Cs'),  

      ('2005003','李民',20,'','Is'),  

      ('2005004','赵欣然',16,'','Ma');  

插入course的数据:

insert into course  

values('1','数据库系统','5',4),  

      ('2','数学分析',null,2),  

      ('3','信息系统导论','1',3),  

      ('4','操作系统原理','6',3),  

      ('5','数据结构','7',4),  

      ('6','数据处理基础',null,4),  

      ('7','C语言','6',3);  

插入表sc的数据:

insert into sc  

values('2005001','1',87),  

      ('2005001','2',67),  

      ('2005001','3',90),  

      ('2005002','2',95),  

      ('2005003','3',88); 

根据Action Output的结果可知,各表的数据已经成功插入。

4.1 基于jxgl数据库,使用SQL语句表达以下查询

4.1.1 检索年龄大于23岁的男学生的学号和姓名

为了有查询结果,这里对表student的内容进行调整:

SQL语言:

select sno,sname  

from student  

where ssex='' and sage>23

查询结果:    

4.1.2 检索至少选修一门课的女学生姓名

SQL语言:

select sname  

from student natural join sc  

where ssex='';  

查询结果:

4.1.3 检索王林不学的课程的课程号

SQL语言:

select cno  

from course  

where cno not in    

(select cno                     

from student natural join sc  

where sname='王林');  

查询结果:

4.1.4 检索至少选修两门课的学生学号

SQL语言:

select distinct sno  

from sc  

group by sno  

having count(sno)>1

查询结果:

4.1.5 检索全部学生都选修的课程的课程号和课程名

为了有查询结果,这里对表sc的内容进行调整:

SQL语言:

select  cno,cname  

from sc natural join course 

group by cno  

having count(cno)=(select count(*)from student); 

查询结果:

4.1.6 检索选修了所有3学分课程的学生平均成绩

为了有查询结果,这里对表sc的内容进行调整:

SQL语言:

select avg(grade)

from sc

where sno in(select sno

   from student

   where not exists(select * from course where ccredit=3 and not exists(

       select * from sc where sc.cno=course.cno and sc.sno=student.sno)))

group by sno;

查询结果:

4.2 基于jxgl数据库,使用SQL语句表达以下查询

4.2.1 统计有学生选修的课程门数

SQL语言:

select count(distinct cno) 

from sc;  

查询结果:

4.2.2 求选修4号课程的学生的平均年龄

为了有更好的查询结果,这里对表sc的内容进行调整:

SQL语言:

select avg(sage)

from student

where sno in (select sno from sc where cno='4');

查询结果:

4.2.3 求学分为3的每门课程的学生平均成绩

SQL语言:

  1. select avg(grade)  
  2. from sc natural join course 
  3. where ccredit=3   
  4. group by cno;    

查询结果:

4.2.4 统计每门课程的学生选修人数,要求超过三人的课程才统计,要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

为了有更好的查询结果,这里对表sc的内容进行调整:

SQL语言:

select cno,count(cno)  

from sc  

group by cno

having count(cno)>3  

order by count(cno) desc ,cno asc;  

查询结果:

4.2.5 检索学号比“王林”同学大而年龄比她小的学生姓名

SQL语言:

                select sname

                from student

                where sno>(select s.sno from student as s where s.sname='王林')

                 and sage<(select s.sage from student as s where s.sname='王林');

查询结果:

4.2.6 检索姓名以“王”开头的所有学生的姓名和年龄

SQL语言:

                select sname,sage

                from student

                where sname like '%';

查询结果:

4.2.7 在sc表中检索成绩为空值的学生的学号和课程号

为了有查询结果,这里对表sc的内容进行调整:

SQL语言:

                select sno,cno

                from sc

                where grade is null;

查询结果:

4.2.8 求年龄大于女学生平均年龄的男学生的姓名和年龄

SQL语言:

                select sname,sage

                from student

                where ssex='' and sage>(select avg(sage) from student where ssex='');

查询结果:

4.2.9 求年龄大于所有女学生年龄的男学生的姓名和年龄

SQL语言:

                select sname,sage

                from student

                where ssex='' and sage>(select max(sage) from student where ssex='');

查询结果:

4.2.10 检索选修4门以上课程的学生总成绩(不统计不及格课程),并要求按总成绩的降序排列出来

为了更好的有查询结果,这里对表sc的内容进行调整:

SQL语言:

                select sum(grade)  

                from sc  

                group by sno    

                having count(cno)>4

                ORDER BY sum(grade) DESC;  

查询结果:

  • 27
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值