MySQL学生选课系统

1.设计背景

该练习主要针对学生选课系统而设计,其中设计到三张表,分别为学生表,课程表,学生和课程对应的关联表。
学生表主要用于存储学生信息,包括姓名,性别,地址,电话等信息。
课程表主要用于存储课程的信息,包括课程的名称,课程的描述,学分等信息。
关联表,用于关联学生表和课程表,课程的分数等。

2.表创建/数据准备

-- 查看MySQL服务器所有数据库
show databases;
-- 删除SRS数据库
drop database if exists SRS;
-- 创建学生选课系统(SRS)数据库并指定默认字符集
create database SRS default charset utf8;
-- 切换至SRS数据库
use SRS;
-- 查看当前数据库中所有表
show tables;-- 创建学生表
create table TbStudent
(
stuid integer not null,
stuname varchar(20) not null,
stusex bit default 1,
stubirth datetime not null,
stutel varchar(11),
stuaddr varchar(255),
stuphoto longblob,
primary key (stuid)
);
-- 修改学生表删除stutel列
alter table TbStudent drop column stutel;
-- 查看学生表结构
desc TbStudent;
-- 如果表TbCourse已经存在就删除它
drop table if exists TbCourse;-- 创建课程表
create table TbCourse
(
cosid integer not null,
cosname varchar(50) not null,
coscredit tinyint not null,
cosintro varchar(255)
);
-- 给课程表指定主键
alter table TbCourse add constraint pk_course primary key (cosid);-- 创建学生选课记录表
create table TbSC
(
scid integer primary key auto_increment,
sid integer not null,
cid integer,
scdate datetime not null,
score float
);
-- 给表TbSC添加外键约束
alter table TbSC add constraint fk_sid foreign key (sid) references TbStudent (stuid) on delete cascade on update cascade;
alter table TbSC add constraint fk_cid foreign key (cid) references TBCourse (cosid) on delete set null on update cascade;-- 添加学生记录
insert into TbStudent values (1001, '张三丰', default, '1978-1-1', '成都市一环路西二段17号', null);
insert into TbStudent (stuid, stuname, stubirth) values (1002, '郭靖', '1980-2-2');
insert into TbStudent (stuid, stuname, stusex, stubirth, stuaddr) values (1003, '黄蓉', 0, '1982-3-3', '成都市二环路南四段123号');
insert into TbStudent values (1004, '张无忌', 1, '1990-4-4', null, null);
insert into TbStudent values
(1005, '丘处机', 1, '1983-5-5', '北京市海淀区宝盛北里西区28号', null),
(1006, '王处一', 1, '1985-6-6', '深圳市宝安区宝安大道5010号', null),
(1007, '刘处玄', 1, '1987-7-7', '郑州市金水区纬五路21号', null),
(1008, '孙不二', 0, '1989-8-8', '武汉市光谷大道61号', null),
(1009, '平一指', 1, '1992-9-9', '西安市雁塔区高新六路52号', null),
(1010, '老不死', 1, '1993-10-10', '广州市天河区元岗路310号', null);
(1011, '王大锤', 0, '1994-11-11', null, null),
(1012, '隔壁老王', 1, '1995-12-12', null, null),
(1013, '郭啸天', 1, '1977-10-25', null, null);-- 删除学生记录
delete from TbStudent where stuid=1004;
-- 更新学生记录
update TbStudent set stubirth='1980-12-12', stuaddr='上海市宝山区同济支路199号' where stuid=1002;-- 添加课程记录
insert into TbCourse values
(1111, 'C语言程序设计', 3, null),
(2222, 'Java程序设计', 3, null),
(3333, '数据库概论', 2, null),
(4444, '操作系统原理', 4, null);-- 添加学生选课记录
insert into TbSC values
(default, 1001, 1111, '2016-9-1', 95),
(default, 1002, 1111, '2016-9-1', 94),
(default, 1001, 2222, now(), null),
(default, 1001, 3333, '2017-3-1', 85),
(default, 1001, 4444, now(), null),
(default, 1002, 4444, now(), null),
(default, 1003, 2222, now(), null),
(default, 1003, 3333, now(), null),
(default, 1005, 2222, now(), null),
(default, 1006, 1111, now(), null),
(default, 1006, 2222, '2017-3-1', 80),
(default, 1006, 3333, now(), null),
(default, 1006, 4444, now(), null),
(default, 1007, 1111, '2016-9-1', null),
(default, 1007, 3333, now(), null),
(default, 1007, 4444, now(), null),
(default, 1008, 2222, now(), null),
(default, 1010, 1111, now(), null);

3.题目练习

1) 查询所有学生信息

select * from TbStudent;

2) 查询所有课程名称及学分(投影和别名)

select cosname as `课程名称`, coscredit as `学分` from TbCourse;

3) 查询所有女学生的姓名和出生日期(筛选)

select stuname, stubirth from TbStudent where stusex=0;

4) 查询所有80后学生的姓名、性别和出生日期(筛选)

select stuname as `姓名`, if(stusex, '男', '女') as `性别`, stubirth as `出生日期` from TbStudent where stubirth between '1980-1-1' and '1989-12-31';

5) 查询姓王的学生姓名和性别(模糊)

select stuname, stusex from TbStudent where stuname like '王%';

6) 查询姓郭名字总共两个字的学生的姓名(模糊)

select stuname from TbStudent where stuname like '郭_';

7) 查询姓郭名字总共三个字的学生的姓名(模糊)

select stuname from TbStudent where stuname like '郭_ _';

8) 查询名字中有王字的学生的姓名(模糊)

select stuname from TbStudent where stuname like '%王%';

9) 查询没有录入家庭住址和照片的学生姓名(多条件筛选和空值处理)

select stuname from TbStudent where stuaddr is null and stuphoto is null;

10) 查询学生选课的所有日期(去重)

select distinct scdate from TbSC;

11) 查询学生的姓名和生日按年龄从大到小排列(排序)

select stuname, stubirth from TbStudent order by stubirth;

12) 查询所有录入了家庭住址的男学生的姓名、出生日期和家庭住址按年龄从小到大排列(多条件筛选和排序)

select stuname, stubirth, stuaddr from TbStudent where stusex=1 and stuaddr is not null order by stubirth desc;

13) 查询年龄最大的学生的出生日期(聚合函数)

select min(stubirth) from TbStudent;

14) 查询年龄最小的学生的出生日期(聚合函数)

select max(stubirth) from TbStudent;

15) 查询男女学生的人数(分组和聚合函数)

select if(stusex, '男', '女') as `性别`, count(stusex) as `人数` from TbStudent group by stusex;

16) 查询课程编号为1111的课程的平均成绩(筛选和聚合函数)

select avg(score) as `平均成绩` from TbSC where cid=1111;

17) 查询学号为1001的学生所有课程的总成绩(筛选和聚合函数)

select sum(score) as `总成绩` from TbSC where sid=1001;

18) 查询每个学生的学号和平均成绩, null值处理成0(分组和聚合函数)

select sid as `学号`, ifnull(avg(score), 0) as `平均成绩` from TbSC group by sid;

19) 查询平均成绩大于等于90分的学生的学号和平均成绩

select sid as `学号`, avg(score) as `平均成绩` from TbSC group by sid having avg(score)>=90;

20) 查询年龄最大的学生的姓名(子查询)

select stuname from TbStudent where stubirth=(select min(stubirth) from TbStudent);

21) 查询选了两门以上的课程的学生姓名(子查询/分组条件/集合运算)

select stuname from TbStudent where stuid in (select sid from TbSC group by sid having count(sid)>2);

22) 查询选课学生的姓名和平均成绩(子查询和连接查询)

写法1:
select stuname, avgscore from TbStudent t1 inner join (select sid, avg(score) as avgscore from TbSC where score is not null group by sid) t2 on t1.stuid=t2.sid;写法2:
select stuname, avgscore from TbStudent t1, (select sid, avg(score) as avgscore from TbSC where score is not null group by sid) t2 where t1.stuid=t2.sid;

23) 查询学生姓名、所选课程名称和成绩(连接查询)

写法1:
select stuname, cosname, score from  TbStudent t1, TbCourse t2, TbSC t3 where t1.stuid=t3.sid and t2.cosid=t3.cid and t3.score is not null;写法2:
select stuname, cosname, score from TbStudent t1 inner join TbCourse t2 inner join (select sid, cid, score from TbSC where score is not null) t3 on t1.stuid=t3.sid and t2.cosid=t3.cid;

24) 查询每个学生的姓名和选课数量(左外连接和子查询)

select stuname as `姓名`, ifnull(coscount, 0) as `选课数` from TbStudent t1
left outer join (select sid, count(sid) as coscount from TbSC group by sid) t2
on t1.stuid=t2.sid;

数据库 - 02 MySQL学生选课系统相关推荐

  1. Java Swing Mysql学生选课系统

    此篇是基于Java Swing Mysql学生选课系统的第二阶段 主要功能: 课程添加.编辑.查询.删除.选课.退课.修改已选课程.以及老师.管理员.学生三种角色的权限分配 开发环境: 开发工具:Ec ...

  2. 我的数据库大作业——学生选课系统实现(准备)

    前言 不好意思,由于年代久远,本文不打算继续更新了. 不知不觉已经大二了,快要步入大三了,回想过去,自己好像都没有做过什么实际的编程活动,自己上学期因为一个古怪的离散老师导致没头没脑得了个刚过及格线的 ...

  3. 数据库设计之学生选课系统

    版权声明:对于本博客所有原创文章,允许个人.教育和非商业目的使用,但务必保证文章的完整性且不作任何修改地以超链接形式注明原始作者.出处及本声明. 博客地址:http://blog.csdn.net/s ...

  4. 数据库设计之学生选课系统_转载

    博客地址:http://blog.csdn.net/shuxiao9058 原始作者:季亚 一.概要设计 1.1 目的和意义 随着无纸化办公的普遍实现,信息的自动处理以及网络式的信息交互方式已经被人们 ...

  5. java swing课程表设计_阶段2:手把手快速做一个Java swing mysql学生选课系统附带完整源码及视频开发教程【猿来入此自营】...

    <p> <span style="font-family:微软雅黑;font-size:16px;color:#666666;background:#FFFFFF;line ...

  6. 数据库大作业——学生选课系统(基于SpringBoot+Mysql)

    文章目录 一.需求分析 1.项目背景 2.项目目标 二.系统功能分析 1.多角色划分 2.模块功能详述 三.系统架构 1.技术选型 2.系统分析 3.架构设计 4.系统演变 四.数据库设计 1.概念结 ...

  7. Django+Mysql学生选课系统/学分管理系统

    前言 ▶之前报名了学校Python比赛,题目很高大上,不过放心,我做的完全达不到这个深度.这个项目其实需要改进的地方还很多,不过作为简单的选课系统来说,基本功能均已实现.后续会继续完善上传到GitHu ...

  8. mysql学生选课系统的关系模型_使用PowerDesigner搭建学生选课管理系统(学生老师管理员一体系结构)由基础设计至数据库生成(SQL语句源代码的生成)全过程实例操作...

    思考构图大小及范围 首先,我们大致的写出要建的几张表,并自定义其Code(所谓Code其实就是自定义一个字段名,因为SQL语句不能由中文) 我们可以将不同的表按照不同的类型分开,这样一个类型的表可以一 ...

  9. 数据库选课系统mysql_数据库设计(学生选课系统).doc

    WORD完美整理版 PAGE 范文范例 参考指导 WORD完美整理版 范文范例 参考指导 <数据库原理与应用>实验指导与报告 2010 / 2011 学年 第 2 学期 姓 名: 季亚 学 ...

最新文章

  1. android自定义view获取控件,android 自定义控件View在Activity中使用findByViewId得到结果为null...
  2. python 远程控制win10界面切换_Python3如何实现Win10桌面自动切换
  3. 用tinyscript解一些典型算法题,小试牛刀
  4. PowerDesigner显示注释字段问题
  5. junit基础学习之-断言注解(3)
  6. java泛型(一)、泛型的基本介绍和使用
  7. php实现文本替换,php 如何实现文字替换
  8. 解构产品经理的技术思维
  9. Linux Kernel Git国内镜像源
  10. 光纤收发器的原理及应用_光纤收发器的应用与讲解
  11. 尚硅谷JVM笔记(宋红康主讲)
  12. iir数字滤波器设计及matlab实现,终稿毕业论文:IIR数字滤波器设计及其MATLAB实现.docOK版(样例3)...
  13. ArcCatalog添加数据库连接
  14. Mac下驱动BCM20702A0 USB蓝牙
  15. LInux 的流量限制
  16. A Dual Weighting Label Assignment Scheme for Object Detection阅读笔记
  17. 纯java写2D格斗游戏(一)——界面背景设置及人物的简单设置
  18. Java 实现企业级支付
  19. python从入门到人生巅峰
  20. 聪明的人脸识别3——Pytorch 搭建自己的Facenet人脸识别平台

热门文章

  1. 初级信息处理技术员access教程_软考初级的信息处理技术员请问怎么复习好?
  2. 大连医科大学中山学院计算机科学与技术,2021年大连医科大学中山学院各专业录取分数线...
  3. Word排版之段落样式+如何创建一个三线表外观的表格样式
  4. RSA已知密文和公钥
  5. svnsync: Failed to get lock on destination repos, currently held by 'localhost.localdomain
  6. 音频codec调试心得
  7. 命令提示符的三种打开方式
  8. 大学android五子棋课程目的,基于安卓开发的五子棋课程设计报告..docx
  9. 如何使用python编程抢京东优惠券 知乎_小猿圈Python之实现京东秒杀功能代码
  10. 小米笔记本AIR13.3第一代(6200U+8GB+256GB,不带指纹)双系统(WIN10+DEEPIN)安装过程避坑