当前位置:网站首页>MySQL小练习(仅适合初学者,非初学者勿进)
MySQL小练习(仅适合初学者,非初学者勿进)
2022-04-23 08:50:00 【一个热爱编程的小白白】
哈哈,这是我们老师布置的作业,我在想用不用发博客呢?
最后想了一下,还是发出来吧,虽然很简单,但是可以给那些刚学数据库的朋友来练习一下。因为没有答案,我也不知道对不对,如果有大佬发现错误,请指出来。
题目:
- 查询各位学生的学号、班级和姓名
- 查询课程的全部信息
- 查询数据库中有哪些专业班级
- 查询学时大于60的课程信息
- 查询出生在1986年出生的学生的学号、姓名和出生日期
- 查询三次作业成绩都在80分以上的学号、课程号
- 查询姓张的学生的学号、姓名和专业班级
- 查询05级的男生信息
查询没有作业成绩的学号和课程号
- 查询学号为0538的学生的作业1总分
- 查询选修了K001课程的学生人数
- 查询数据库中共有多少个班级
- 查询选修三门以上(含3门)课程的学生的学号和作业1平均分,作业2平均分,作业3 平均分
如果小伙伴们是0基础 也没关系,看看下面这篇博客 然后再来做题。
1.创建数据库
create database if not exists db2 ;
好了创建成功 ,然后我们打开控制台
2.创建表
1.创建学生表
分析:
学号:字符型
姓名:字符型
性别:字符型 性别是一个字 所以 varchar(1)
专业班级:字符型
出生日期:时间类型 date
联系电话:字符型 电话号码11位 varchar(11)即可。
drop table if exists student;
create table student
(
id varchar(10) comment '学号',
name varchar(10) NOT NULL comment '姓名',
gender char(1) comment '性别',
class varchar(20) comment '专业班级',
date date comment '出生日期',
iphone varchar(11) comment '联系电话'
)
comment '学生表';
select * from student;
2.创建课程表
drop table if exists student_course;
create table student_course
(
course_id varchar(10) comment '课程号',
course_name varchar(15) comment '课程名',
course_number double unsigned comment '学分数',
student_time int unsigned comment '学时数',
teacher varchar(10) comment '任课教师'
)
comment '课程表';
select *
from student_course;
3.学生作业表
drop table if exists student_homework;
create table student_homework
(
course_id varchar(10) comment '课程号',
id varchar(10) comment '学号',
homework_1 int comment '作业1成绩',
homework_2 int comment '作业2成绩',
homework_3 int comment '作业3成绩'
)
comment '学生作业表';
select *
from student_homework;
3.添加数据
照着图片上的数据一个个打出来的,呜呜呜
1.学生表
insert into student
values ('0433', '张艳', '女', '生物04', '1986-9-13', null),
('0496', '李越', '男', '电子04', '1984-2-23', '1381290xxxx'),
('0529', '赵欣', '男', '会计05', '1984-1-27', '1350222xxxx'),
('0531', '张志国', '男', '生物05', '1986-9-10', '1331256xxxx'),
('0538', '于兰兰', '女', '生物05', '1984-2-20', '1331200xxxx'),
('0591', '王丽丽', '女', '电子05', '1984-3-20', '1332080xxxx'),
('0592', '王海强', '男', '电子05', '1986-11-1', null);
查询一下:
select * from student;
2.课程表
INSERT INTO student_course
values ('K001', '计算机图形学', 2.5, 40, '胡晶晶'),
('K002', '计算机应用基础', 3, 48, '任泉'),
('K006', '数据结构', 4, 64, '马跃先'),
('M001', '政治经济学', 4, 64, '孔繁新'),
('S001', '高等数学', 3, 48, '赵晓尘');
查询一下:
select *
from student_course;
3.学生作业表
insert into student_homework values
('K001','0433',60,75,75),
('K001','0529',70,70,60),
('K001','0531',70,80,80),
('K001','0591',80,90,90),
('K002','0496',80,80,90),
('K002','0529',70,70,85),
('K002','0531',80,80,80),
('K002','0538',65,75,85),
('K002','0592',75,85,85),
('K006','0531',80,80,90),
('K006','0591',80,80,80),
('M001','0496',70,70,80),
('M001','0591',65,75,75),
('S001','0531',80,80,80),
('S001','0538',60,null,80);
查询一下:
select *
from student_homework;
4.开始做题
1.查询各位学生的学号、班级和姓名
select id,class,name from student;
2.查询课程的全部信息
select *
from student_course;
3.查询数据库中有哪些专业班级
select class from student;
4.查询学时大于60的课程信息
select course_id,course_name from student_course where student_time>60;
5.查询出生在1986年出生的学生的学号、姓名和出生日期
select id,name,date from student where date>=('1986-1-1') AND date<('1987-1-1');
6.查询三次作业成绩都在80分以上的学号、课程号
一开始我是用这个查询了一遍:
select * from student_homework where homework_1>80 and homework_2>80 and homework_3>80;
发现啥也没有,于是我看了一下数据 发现满足三次作业成绩都在80分以上的 没有这种数据
故此题目:“查询三次作业成绩都在80分以上的学号、课程号” 应该包括80分
于是,我修改一下:
select * from student_homework where homework_1>=80 and homework_2>=80 and homework_3>=80;
7.查询姓张的学生的学号、姓名和专业班级
错误示范:
由于几百年没写SQL了, 我竟然写成(给自己一巴掌):
select id,name,class from student where name = '张%';
select id,name,class from student where name like '张%';
8.查询05级的男生信息
select * from student where class like '%05' and gender='男';
9.查询没有作业成绩的学号和课程号
select id,course_id from student_homework where homework_1 is null or homework_2 is null or homework_3 is null ;
10.查询学号为0538的学生的作业1总分
select sum(homework_1) '总分' from student_homework where id='0538';
11.查询选修了K001课程的学生人数
select count(*) from student_homework where course_id='K001';
12.查询数据库中共有多少个班级
select count(*) from student where class is not null ;
13.查询选修三门以上(含3门)课程的学生的学号和作业1平均分,作业2平均分,作业3 平均分
select student.id, avg(homework_1), avg(homework_2), avg(homework_3)
from student
left join student_homework on student.id = student_homework.id
group by student.id
having count(course_id) >= 3;
版权声明
本文为[一个热爱编程的小白白]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Javascript_tsj/article/details/124350331
边栏推荐
- MySQL查询两张表属性值非重复的数据
- 求简单类型的矩阵和
- Test your machine learning pipeline
- Introduction to matlab
- Notes on 30 steps of introduction to the Internet of things of yangtao electronics STM32 III. cubemx graphical programming and setting the IO port on the development board
- K210 learning notes (II) serial communication between k210 and stm32
- OneFlow學習筆記:從Functor到OpExprInterpreter
- How much inventory recording does the intelligent system of external call system of okcc call center need?
- Use of Arthas in JVM tools
- valgrind和kcachegrind使用运行分析
猜你喜欢
爬虫使用xpath解析时返回为空,获取不到相应的元素的原因和解决办法
Cadence process angle simulation, Monte Carlo simulation, PSRR
Correct method of calculating inference time of neural network
L2-022 rearrange linked list (25 points) (map + structure simulation)
Star Trek's strong attack opens the dream linkage between metacosmic virtual reality
洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口
Pctp test experience sharing
洋桃电子STM32物联网入门30步笔记一、HAL库和标准库的区别
经典题目刷一刷
2022-04-22 OpenEBS云原生存储
随机推荐
'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...
Summary of solid problems
Notes on 30 steps of introduction to the Internet of things of yangtao electronics STM32 III. cubemx graphical programming and setting the IO port on the development board
The crawler returns null when parsing with XPath. The reason why the crawler cannot get the corresponding element and the solution
2021 Li Hongyi's adaptive learning rate of machine learning
Chris LATTNER, father of llvm: the golden age of compilers
错误: 找不到或无法加载主类
Illegal character in scheme name at index 0:
玩转二叉树 (25 分)
Multi view depth estimation by fusing single view depth probability with multi view geometry
洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別
数字政府建设中政务中台中的技术创新点
2022-04-22 openebs cloud native storage
L2-022 重排链表 (25 分)(map+结构体模拟)
1099 establish binary search tree (30 points)
Valgrind and kcache grind use run analysis
Consensus Token:web3.0生态流量的超级入口
是否同一棵二叉搜索树 (25 分)
Single chip microcomputer nixie tube stopwatch
关于cin,scanf和getline,getchar,cin.getline的混合使用