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
;
相关推荐
数学人学c语言2 小时前
从熟练Python到入门学习C++(record 6)
c++·python·学习
Simulink_4 小时前
ROS学习笔记15——Xacro
linux·笔记·学习·机器人·ros
2301_775281195 小时前
当日本人说「お疲れ様」时,该怎么回?柯桥日语培训零基础学习
学习
E___V___E6 小时前
CSAPP学习
学习
Tony_long74836 小时前
Python学习——猜拳小游戏
开发语言·python·学习
苹果醋36 小时前
前端面试之九阴真经
java·运维·spring boot·mysql·nginx
不是二师兄的八戒6 小时前
MYSQL索引优化
mysql
m0_748230216 小时前
mysql约束和高级sql
数据库·sql·mysql
Diamond技术流6 小时前
从0开始学习Linux——环境变量详解
linux·服务器·学习·centos·软件安装·环境变量
程序员学姐8 小时前
基于SpringBoot+Vue的高校社团管理系统
java·开发语言·vue.js·spring boot·后端·mysql·spring