MySQL 学习------项目实战
项目出处 博主: Asmywishi
Linux-Ubuntu启动Mysql
shell
sudo mysql
Data preparation
Create Database and Table
- Create database :
sql
create database mysql_example1;
- Start database :
sql
use mysql_example1;
- 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) # 学生学号为表数据主键
);
NOT NULL DEFAULT
属于两个约束,是指填写数据时不能为空 [可参考链接内容](Markdown 语法速查表 | Markdown 教程)PRIMARY KEY
表示主键必须包含的唯一值,且不能是NULL [可参考W3school](SQL PRIMARY KEY 约束 (w3school.com.cn))
- 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为表数据主键
);
- Create Teachers table :
sql
# 教师表
CREATE TABLE Teacher(
t_id VARCHAR(20), # 教师ID
t_name VARCHAR(20) NOT NULL DEFAULT '', # 教师名
PRIMARY KEY(t_id) # 教师ID为表数据主键
);
- 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
- 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
数据
- 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;
- 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;
- 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
查看学生成绩信息
sqlselect 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) ;
使用连接查询学生成绩信息------更清晰
sqlselect 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 的解题思路一致,改一下判断条件即可
sqlselect 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
查询出所有同学的学生编号、学生姓名和平时成绩再添加筛选条件即可
sqlselect 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 一致,改变判断条件即可
sqlselect 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
sqlselect 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 ;