Mysql 学习——项目实战

MySQL 学习------项目实战

项目出处 博主: Asmywishi

Linux-Ubuntu启动Mysql

shell 复制代码
sudo mysql

Data preparation

Create Database and Table

  1. Create database :
sql 复制代码
create database mysql_example1;
  1. Start database :
sql 复制代码
use mysql_example1;
  1. Create Student table :
sql 复制代码
# 学生表
CREATE TABLE Student(
s_id VARCHAR(20),  # 学生学号ID
s_name VARCHAR(20) NOT NULL DEFAULT '',  # 学生名字
s_birth VARCHAR(20) NOT NULL DEFAULT '',  # 学生生日
s_sex VARCHAR(10) NOT NULL DEFAULT '',  # 学生性别
PRIMARY KEY(s_id)  # 学生学号为表数据主键
);
  1. Create Course table :
sql 复制代码
# 课程表
CREATE TABLE Course(
c_id VARCHAR(20),  # 课程ID
c_name VARCHAR(20) NOT NULL DEFAULT '',  # 课程名
t_id VARCHAR(20) NOT NULL,  # 授课教师ID
PRIMARY KEY(c_id)  # 课程ID为表数据主键
);
  1. Create Teachers table :
sql 复制代码
# 教师表
CREATE TABLE Teacher(
t_id VARCHAR(20),  # 教师ID
t_name VARCHAR(20) NOT NULL DEFAULT '',  # 教师名
PRIMARY KEY(t_id)  # 教师ID为表数据主键
);
  1. Create Score table :
sql 复制代码
# 成绩表
CREATE TABLE Score(
s_id VARCHAR(20),  # 学生ID
c_id VARCHAR(20),  # 课程ID
s_Score INT(3),  # 科目成绩
PRIMARY KEY(s_id,c_id)  # 学生ID,课程ID为表数据主键
);

Insert Data to table

  1. Insert students' data to students' table :
sql 复制代码
# 插入学生表测试数据
insert into Student values('01' , 'zhaolei' , '1990-01-01' , 'male');
insert into Student values('02' , 'qiandian' , '1990-12-21' , 'male');
insert into Student values('03' , 'sunfeng' , '1990-05-20' , 'male');
insert into Student values('04' , 'liyun' , '1990-08-06' , 'male');
insert into Student values('05' , 'zhoumei' , '1991-12-01' , 'female');
insert into Student values('06' , 'wulan' , '1992-03-01' , 'female');
insert into Student values('07' , 'zhenzhu' , '1989-07-01' , 'female');
insert into Student values('08' , 'wangju' , '1990-01-20' , 'female');

Check the student data: select * from Student;

  • show data code: select * from [table_name] 打印出表格table_name 中的数据
  • insert into [table_name] values(data) : 向table_name表中传入data数据
  1. Insert data of Course to table of Course :
sql 复制代码
# 课程表测试数据
insert into Course values('01' , 'Chinese' , '02');
insert into Course values('02' , 'Maths' , '01');
insert into Course values('03' , 'English' , '03');

Check data of the course : select * from Course;

  1. Insert the teachers' data to Teachers' table :
sql 复制代码
# 教师表测试数据
insert into Teacher values('01' , 'zhangsan');
insert into Teacher values('02' , 'lisi');
insert into Teacher values('03' , 'wangwu');

Check teahcers' data : select * from Teacher;

  1. Insert score data to Score table :
sql 复制代码
# 成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);

Check score data : select * from Score;


Practical problems

Question 1

查询"01"课程比"02"课程成绩高的学生的信息及课程分数

Answer 1

查看学生成绩信息

sql 复制代码
select st.s_id,s_name,c_id,s_Score
from Student st,Course c
where st.s_id = c.s_id
and c.c_id in (01,02)
;

使用连接查询学生成绩信息------更清晰

sql 复制代码
select st.s_id,s_name,sc1.s_Score as 'Chinese',sc2.s_Score as 'Maths'
from Student st
left outer join Score sc1 on sc1.s_id = st.s_id and sc1.c_id = 01
left outer join Score sc2 on sc2.s_id = st.s_id and sc2.c_id = 02
where sc1.s_Score > sc2.s_Score
;

Question 2

查询"01"课程比"02"课程成绩低的学生信息及课程分数

Answer 2

与answer1 的解题思路一致,改一下判断条件即可

sql 复制代码
select st.s_id,s_name,sc1.s_Score as 'Chinese',sc2.s_Score as 'Maths'
from Student st 
left outer join Score sc1 on sc1.s_id = st.s_id and sc1.c_id = 01
left outer join Score sc2 on sc2.s_id = st.s_id and sc2.c_id = 02
where sc1.s_Score < sc2.s_Score
;

Question 3

查询平均成绩大于等于60分的同学的学生编号、学生姓名和平均成绩

Answer 3

查询出所有同学的学生编号、学生姓名和平时成绩再添加筛选条件即可

sql 复制代码
select st.s_id,st.s_name,avg(sc.s_Score) as 'avg'
from Student st,Score sc
where st.s_id = sc.s_id
group by st.s_id having avg(sc.s_Score) >= 60
;
  • group by [filed_name] having [condition] 根据 filed_name 进行排序,未设置参数则默认从小到大排序,condition 筛选条件

Question 4

查询平均成绩大于等于60分的同学的学生编号、学生姓名和平均成绩

Answer 4

与question3 一致,改变判断条件即可

sql 复制代码
select st.s_id,st.s_name,avg(sc.s_Score) as 'avg'
from Student st
left outer join Score sc
on st.s_id = sc.s_id
group by st.s_id having avg(sc.s_Score) < 60 or avg(s_Score) is null
;

Question 5

查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

Answer 5

sql 复制代码
select st.s_id,st.s_name,count(sc.c_id) as 'course_counts',sum(sc.s_Score) as 'sum_score'
from Score sc
left outer join Student st
on st.s_id = sc.s_id
group by st.s_id
;
相关推荐
eybk2 小时前
Pytorch+Mumu模拟器+萤石摄像头实现对小孩学习的监控
学习
6.942 小时前
Scala学习记录 递归调用 练习
开发语言·学习·scala
Ren_xixi2 小时前
redis和mysql的区别
数据库·redis·mysql
守护者1704 小时前
JAVA学习-练习试用Java实现“使用Arrays.toString方法将数组转换为字符串并打印出来”
java·学习
学会沉淀。4 小时前
Docker学习
java·开发语言·学习
追逐时光者4 小时前
免费、简单、直观的数据库设计工具和 SQL 生成器
后端·mysql
Rinai_R4 小时前
计算机组成原理的学习笔记(7)-- 存储器·其二 容量扩展/多模块存储系统/外存/Cache/虚拟存储器
笔记·物联网·学习
吃着火锅x唱着歌4 小时前
PHP7内核剖析 学习笔记 第四章 内存管理(1)
android·笔记·学习
drebander4 小时前
MySQL 查询优化案例分享
数据库·mysql
ragnwang4 小时前
C++ Eigen常见的高级用法 [学习笔记]
c++·笔记·学习