当前位置:网站首页>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
边栏推荐
- GUI编程简介 swing
- 是否完全二叉搜索树 (30 分)
- 正点原子携手OneOS直播 OneOS系统教程全面上线
- Harbor企业级镜像管理系统实战
- How much inventory recording does the intelligent system of external call system of okcc call center need?
- Complete binary search tree (30 points)
- 【58】最后一个单词的长度【LeetCode】
- Swagger document export custom V2 / API docs interception
- Latex paper typesetting operation
- ONEFLOW learning notes: from functor to opexprinter
猜你喜欢
OneFlow學習筆記:從Functor到OpExprInterpreter
企业微信应用授权/静默登录
2021李宏毅机器学习之Adaptive Learning Rate
RCC introduction of Hal Library
论文阅读《Multi-View Depth Estimation by Fusing Single-View Depth Probability with Multi-View Geometry》
洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口
Multi view depth estimation by fusing single view depth probability with multi view geometry
ONEFLOW learning notes: from functor to opexprinter
Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise
Solidity 问题汇总
随机推荐
Concave hull acquisition method based on convex hull of point cloud
Yangtao electronic STM32 Internet of things introduction 30 steps notes 1. The difference between Hal library and standard library
Failed to prepare device for development
Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
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
引用传递1
企业微信应用授权/静默登录
STM32使用HAL库,整体结构和函数原理介绍
idea打包 jar文件
Stm32f103zet6 [development of standard library functions] - Introduction to library functions
Idea import commons-logging-1.2 Jar package
tsdf +mvs
Yangtao electronic STM32 Internet of things entry 30 step notes II. Cube ide download, installation, sinicization and setting
Use of Arthas in JVM tools
mycat配置
How much inventory recording does the intelligent system of external call system of okcc call center need?
错误: 找不到或无法加载主类
1099 建立二叉搜索树 (30 分)
是否同一棵二叉搜索树 (25 分)