************************************************************

为sc表中的sno和cno创建外键

alter table sc add foreign key(sno) references student(sno);

alter table sc add foreign key(cno) references course(cno);

************************************************************

a) 插入如下学生记录(学号:95030,姓名:李莉,年龄:18)

insert into student(sno,sname,sage) values('95030','李莉',18);

b) 插入如下选课记录(95030,1)

insert int sc(sno,cno) values('95030','001');

c) 计算机系学生年龄改成20

update student set sage = 20 where sdept = 'cs';

d) 数学系所有学生成绩改成0

update sc set grade = 0 where cno = '002';

e) 把低于总平均成绩的女同学成绩提高5分

f) 修改2号课程的成绩,若成绩小于75分提高5%,成绩大于75时提高

4%(两个语句实现,注意顺序)

update sc set grade=grade+grade*0.05 where cno='002' and grade<75;

update sc set grade=grade+grade*0.04 where cno='002' and grade>75;

g) 删除95030学生信息

delete from student where sno = '95030';

h) 删除SC表中无成绩的记录

delete from sc where grade is null;

i) 删除张娜的选课记录

delete from sc where sno = (select sno from student where sname = '张娜');

j) 删除数学系所有学生选课记录

delete from sc where cno = '002';

k) 删除不及格的学生选课记录

delete from sc where grade < 60;

l) 查询每一门课程成绩都大于等于80分的学生学号、姓名和性别,把值送往另一个已经存在的基本表STU(SNO,SNAME,SSEX)中

insert into stu(sno,sname,ssex)

select student.sno,student.sname,student.ssex

from student,sc

where student.sno=sc.sno and sc.grade>=80;

m) 把所有学生学号和课程号连接追加到新表中

insert into STUsc(sno,cno)

select student.sno,sc.cno from student,sc

where student.sno = sc.sno;

n) 所有学生年龄增1

update student set sage = sage+1;

o) 统计3门以上课程不及格的学生把

相应的学生姓名、系别追加到另外一个表中

insert into stu(sname,sdept) --插入表中

select sname,sdept from student,sc --选择出列名

where --条件

student.sno=(select sno from sc

where grade<60 group by sno having count(*)>3);

查询每个学生及其选课情况;

select sno,cno from sc;

将STUDENT,SC进行右连接

查询有不及格的学生姓名和所在系

select a.sname,a.sdept from student a right join sc b on a.sno = b.sno;

查询所有成绩为优秀(大于90分)的学生姓名

select a.grade,b.sname ,a.sno

from sc a right join student b on a.grade > 90 and a.sno = b.sno;

+-------+--------+-------+

| grade | sname | sno |

+-------+--------+-------+

| NULL | 丽丽 | NULL |

| NULL | 赵海 | NULL |

| 97 | 刘晨 | 08005 |

| 93 | 刘丹丹 | 08006 |

| NULL | 刘立 | NULL |

| NULL | 王江 | NULL |

| NULL | 高晓 | NULL |

| 100 | 张丽 | 08010 |

| NULL | NULL | NULL |

+-------+--------+-------+

select a.grade,b.sname ,a.sno from sc a

join student b on a.grade > 90 and a.sno = b.sno;

+-------+--------+-------+

| grade | sname | sno |

+-------+--------+-------+

| 97 | 刘晨 | 08005 |

| 93 | 刘丹丹 | 08006 |

| 100 | 张丽 | 08010 |

+-------+--------+-------+

查询既选修了2号课程又选修了3号课程的

学生姓名、学号;

select sname,sno from student where sno in(

select sno from sc

where cno = '003'

and

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

----或者

select a.sname,a.sno from student a join sc b where a.sno = b.sno and cno = '003' and b.sno in(select sno from sc where cno = '006');

查询和刘晨同一年龄的学生

select sno ,sname from student where sage = (select sage from student where sname = '刘晨');

选修了课程名为“数据库”的学生姓名和年龄

select a.sname,a.sage from student a join sc b on a.sno = b.sno and b.cno = '001

查询其他系比IS系任一学生年龄小的学生名单

select sname,sage from student where sage < any

(select sage from student where sdept = 'is');

查询其他系中比IS系所有学生年龄都小的学生名单

select sname,sage from student where sage < all

(select sage from student where sdept = 'is');

查询选修了全部课程的学生姓名

select sname,sno

from student where sno in(select sno from sc);

查询计算机系学生及其性别是男的学生

select *from student where sdept = 'cs' and ssex = '男';

查询选修课程1的学生集合和选修2号课程学生集合的差集

select sno from sc where cno = '001' and sno not

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

查询李丽同学不学的课程的课程号

select cno from course where cno not in(select a.cno from sc a join student b on a.sno = b.sno and b.sname = '李丽');

查询选修了3号课程的学生平均年龄

select avg(sage) from student where sno in(select sno from sc where cno = '003');

求每门课程学生的平均成绩

select avg(grade) from sc group by cno;

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

select cno ,count(*) from sc group by cno having count(sno) > 3;

查询学号比刘晨大,而年龄比他小的学生姓名。

select sname from student where

sno > (select sno from student where sname = '刘晨')

and sage < (select sage from student where sname = '刘晨');

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

select sname,sage from student where ssex = '男' and

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

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

select sname,sage from student where ssex = '男' and

sage > all (select sage from student where ssex = '女');

查询95001和95002两个学生都选修的课程的信息

select *from sc where sno in ('95001','95002');

-----------------------------------------------------

alter table student add test varchar(20); -

alter table student drop test; -

-----------------------------------------------------

为学生表按学号建立唯一索引

mysql> create UNIQUE INDEX stusno ON student(sno);

Query OK, 10 rows affected (0.61 sec)

Records: 10 Duplicates: 0 Warnings: 0

为course表按课程号升序(默认)建立唯一索引

mysql> create UNIQUE INDEX coucno ON course(cno);

Query OK, 7 rows affected (0.38 sec)

Records: 7 Duplicates: 0 Warnings: 0

为sc表按学号升序和课程号降序建立唯一索引

mysql> create UNIQUE INDEX scno ON sc(sno asc,cno desc);

Query OK, 11 rows affected (0.34 sec)

Records: 11 Duplicates: 0 Warnings: 0

//删除索引

drop index scno on sc;

//插入学生信息

insert into student (sno,sname,sage) values('95030',';李莉',18);

a) 查询全体学生的学号和姓名

select sno,sname from student;

b) 查询全体学生的详细记录

select *from student;

c) 查询所有选修过课程的学生学号

select distinct sno from sc ;

d) 查询考试有不及格的学生学号

select sno from sc where grade < 60;

e) 查询不是信息系(IS)、计算机系(CS)的学生性别、年龄、系别

select sname,ssex,sdept from student where sdept not in('is','cs');

f) 查询选修了4号课的学生学号和成绩,结果按成绩降序排列

select sno,grade from sc where cno = '004' order by grade desc;

g) 查询每个课程号和相应的选课人数

select cno,count(cno) as cnonumed from sc group by cno;

h) 查询计算机系(CS)的学生姓名、年龄、系别

select sname,sage,sdept from student where sdept = 'cs';

i) 查询年龄18-20岁的学生学号、姓名、系别、年龄;

select sname,sage,sdept from student where sage between 18 and 20;

j) 查询姓刘的学生情况

select *from student where sname like '刘%';

k) 查询既选修3号课程,又选修6号课程的学生学号和成绩

select sno ,grade from sc

where cno = '003'

and

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

l) 查询学生的姓名和出生年份(今年2003年)

select sname,sbirthday from student;

m) 查询没有成绩的学生学号和课程号

select sno,cno from sc where grade is null;

n) 查询总成绩大于200分的学生学号

select sno,sum(grade) from sc

group by sno having sum(grade) > 200;

o) 查询每门课程不及格学生人数

select cno,count(sno) from sc

where grade < 90

group by cno;

p) 查询不及格课程超过3门的学生学号

select cno,count(sno) from sc

where grade < 60

group by cno

having count(sno) > 3;

q) 查询年龄在10到19岁之间的学生信息

select *from student

where sage between 10 and 19;

r) 查询全体学生情况,按所在系升序排列,同一个系的学生按年龄降序排列

select *from student order by sdept asc,sage desc;

s) 查询选了1号课程的学生平均成绩

select cno,avg(grade) from sc where cno = '001' group by cno;

+-----+------------+

| cno | avg(grade) |

+-----+------------+

| 001 | 92.6667 |

+-----+------------+

1 row in set (0.00 sec)

mysql> select cno,avg(grade) from sc group by cno having cno = '001';

+-----+------------+

| cno | avg(grade) |

+-----+------------+

| 001 | 92.6667 |

+-----+------------+

t) 查询选了3号课程的学生的最高分

select cno, max(grade)

from sc where cno = '003' group by cno;

u) 查询每个同学的总成绩

select sno,sum(grade)

from sc

group by sno;

mysql学生表选课表课程表_mysql查询(学生表、课程表、选课表)相关推荐

  1. mysql数据表中取几列_MySQL查询数据表中数据记录(包括多表查询)

    MySQL查询数据表中数据记录(包括多表查询) 转自:http://www.baike369.com/content/?id=5355 在MySQL中创建数据库的目的是为了使用其中的数据. 使用sel ...

  2. mysql获取多张表中的数据_mysql 之多表查询

    阅读目录 一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习 一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 首先说一下,我们写项目一般都会建一个数据库,那数据库里 ...

  3. mysql查询含有某个值的表_MYSQL查询数据表中某个字段包含某个数值

    当某个字段中字符串是"1,2,3,4,5,6"或者"123456" 查询数据表中某个字段是否包含某个值 1:模糊查询  使用like select * from ...

  4. mysql连接多个存储存储_Mysql(三):多表查询和存储程序

    今天内容: ● 多表查询(内连接 外连接 子查询) ● 存储程序(存储过程 函数) 多表查询 ​同时从多张数据表中查取到需要的数据即是多表查询. 多表查询时,参与查询的表中每条数据进行组合,这种效果称 ...

  5. mysql 最多查询数据库_mysql 查询全表某字段值最大的10条数据

    展开全部 select * from 表 order by 要最大值的字段 desc limit 0,10 FILE: 在MySQL服务器上读写文件.62616964757a686964616fe59 ...

  6. mysql获取多张表中的数据_mysql之多表查询

    1.新建的一个数据库db3 create database db3 charset utf8; 2.为db3数据库创建两张表和数据 (1)创建person表 create tableperon ( i ...

  7. mysql全表重命名备份_Mysql数据库和表的常用操作以及数据备份恢复

    数据库 查看所有数据库 show databases; 使用数据库 use 数据库名; 查看当前使用的数据库 select database(); 创建数据库 create database 数据库名 ...

  8. mysql 查询表 第一列报错_MySQL----DQL(查询数据库表中数据)

    ##DQL:查询表中的记录 1.语法: select 字段列名 from 表名列表 where 条件列表 group  by 分组字段 having  分组之后的条件 order  by 排序 lim ...

  9. mysql中表与表的关系代码_mysql 中表与表之间的关系

    如何找出两张表的对应关系 分析步骤: 1.先找出左表的角度去找 ​ 是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段 (通常是id) 2.再站 ...

最新文章

  1. python中字符串有什么组成_python字符串操作
  2. [有限元] 刚度矩阵每一行的元素之和为零的条件
  3. iphone短信尚未送达_潮讯:iPhone12重大升级;华为麒麟被炒价;抖音上线云游戏;骚扰短信要凉了;一加刘作虎回归OPPO...
  4. android 字典转json,在Swift中将字典转换为JSON
  5. Leetcode每日一题:16.3sum-closest(最接近的三数之和)
  6. 备份VMWare ESXi虚拟机
  7. Java导出excel合并单元格边框消失问题
  8. Windows Server2012搭建Git服务器
  9. hourglass论文_人体姿态估计论文笔记-Hourglass
  10. c语言开发无人机自动驾驶仪,无人机自动驾驶仪.pdf
  11. 新时达电梯服务器维修,常见的新时达电梯维修时问题分析
  12. 创业者李一男:过去的荣耀早已归零
  13. 在线下载离线地图瓦片 (支持百度、高德和谷歌地图)
  14. 看顶级渣男如何邀约100个女朋友(一)
  15. 原生PHP上传图片并且裁剪图片生成推广海报
  16. 核雕图案的寓意(上)
  17. 刷脸支付是一个不容错过的商机
  18. 信息安全-网络安全的三大支柱和攻击向量
  19. 亚马逊智能化管理软件解孵全面便捷的仓库管理功能
  20. 文本编辑器批量删除带有某个字符的一行

热门文章

  1. hadoop启动命令 start-all.sh提示Permission denied问题解决
  2. CAT的Server初始化
  3. 金蝶应收应付模块流程_金蝶专业版应收应付管理
  4. “云上进化——2022全球AI生物制药大赛”来袭,30万奖池等你来战!
  5. Pulsar 社区周报| 2020-12-05 ~ 2020-12-11
  6. 计算机保存到桌面没有显示,电脑系统进不去桌面,没有图标怎么办?
  7. 1-1 MySQL数据库的基本操作 【增删改查】
  8. 莫比乌斯函数(Mobius)的求法 每日一遍,算法再见!
  9. 编码通信与魔术初步(四)——通信编码魔术的基本原理
  10. c/c++程序员发展方向