day27 SQL强化和实践
课程目标:练习常见的SQL语句和表结构的设计。
课程概要:
- SQL强化
- 表结构设计(博客系统)
1. SQL强化
-
根据上图创建数据库 & 表结构并录入数据。(可自行添加数据)
-- 创建数据库 create database day27 DEFAULT CHARSET utf8 COLLATE utf8_general_ci; -- 进入数据库 use day27;
- 批量导入
mysql -u root -p day27 < C:\users\xuanxiaomo\day27.sql
- 批量导出
-- 导出结构+数据 mysqldump -u root -p day27 > /Users/xuanxiaomo/day27.sql -- 只导出结构 mysqldump -u root -p -d day27 > /Users/xuanxiaomo/day27.sql
create table class( cid int not null auto_increment primary key, caption varchar(16) not null )default charset=utf8; INSERT INTO class VALUES ('1', '三年二班'), ('2', '三年三班'), ('3', '一年二班'), ('4', '二年九班'); create table student( sid int not null auto_increment primary key, gender char(1) not null, class_id int not null, sname varchar(16) not null, constraint fk_student_class foreign key (class_id) references class(cid) )default charset=utf8; INSERT INTO student VALUES ('1', '男', '1', '李杰'), ('2', '女', '1', '钢蛋'), ('3', '男', '1', '张三'), ('4', '男', '1', '张一'), ('5', '女', '1', '张二'), ('6', '男', '1', '张四'), ('7', '女', '2', '铁锤'), ('8', '男', '2', '李三'), ('9', '男', '2', '李一'), ('10', '女', '2', '李二'), ('11', '男', '2', '李四'), ('12', '女', '3', '如花'), ('13', '男', '3', '刘三'), ('14', '男', '3', '刘一'), ('15', '女', '3', '刘二'), ('16', '男', '3', '刘四'); create table teacher( tid int not null auto_increment primary key, tname varchar(16) not null )default charset=utf8; INSERT INTO `teacher` VALUES ('1', '张磊老师'), ('2', '李平老师'), ('3', '刘海燕老师'), ('4', '朱云海老师'), ('5', '李杰老师'); create table course( cid int not null auto_increment primary key, cname varchar(16) not null, teacher_id int not null, constraint fk_course_teacher foreign key (teacher_id) references teacher(tid) )default charset=utf8; INSERT INTO `course` VALUES ('1', '生物', '1'), ('2', '物理', '2'), ('3', '体育', '3'), ('4', '美术', '2'); CREATE TABLE `score` ( `sid` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `student_id` int NOT NULL, `course_id` int NOT NULL, `num` int NOT NULL, CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`), CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`) ) DEFAULT CHARSET=utf8; INSERT INTO `score` VALUES ('1', '1', '1', '10'), ('2', '1', '2', '9'), ('5', '1', '4', '66'), ('6', '2', '1', '8'), ('8', '2', '3', '68'), ('9', '2', '4', '99'), ('10', '3', '1', '77'), ('11', '3', '2', '66'), ('12', '3', '3', '87'), ('13', '3', '4', '99'), ('14', '4', '1', '79'), ('15', '4', '2', '11'), ('16', '4', '3', '67'), ('17', '4', '4', '100'), ('18', '5', '1', '79'), ('19', '5', '2', '11'), ('20', '5', '3', '67'), ('21', '5', '4', '100'), ('22', '6', '1', '9'), ('23', '6', '2', '100'), ('24', '6', '3', '67'), ('25', '6', '4', '100'), ('26', '7', '1', '9'), ('27', '7', '2', '100'), ('28', '7', '3', '67'), ('29', '7', '4', '88'), ('30', '8', '1', '9'), ('31', '8', '2', '100'), ('32', '8', '3', '67'), ('33', '8', '4', '88'), ('34', '9', '1', '91'), ('35', '9', '2', '88'), ('36', '9', '3', '67'), ('37', '9', '4', '22'), ('38', '10', '1', '90'), ('39', '10', '2', '77'), ('40', '10', '3', '43'), ('41', '10', '4', '87'), ('42', '11', '1', '90'), ('43', '11', '2', '77'), ('44', '11', '3', '43'), ('45', '11', '4', '87'), ('46', '12', '1', '90'), ('47', '12', '2', '77'), ('48', '12', '3', '43'), ('49', '12', '4', '87'), ('52', '13', '3', '87');
-
创建用户 xuanxiaomo 并赋予此数据库的所有权限。
-- 创建用户 create user 'xuanxiaomo'@'%' identified by 'xxm123'; -- 授权 grant all privileges on day27.* TO 'xuanxiaomo'@'%'; flush privileges;
-
查询姓“李”的老师的个数。
select count(1) from teacher where tname like '李%';
-
查询姓“张”的学生名单。
select sname from student where sname like '张%';
-
查询男生、女生的人数。
select gender,count(1) from student group by gender;
-
查询同名同姓学生名单,并统计同名人数。
select sname,count(1) from student group by sname having count(1)>1;
-
查询 “三年二班” 的所有学生。
select * from student left outer join class on student.class_id = class.cid where class.caption='三年二班';
-
查询每个班级的班级名称、班级人数。
select caption,count(1) from class left outer join student on class.cid = student.class_id group by cid;
-
查询成绩小于60分的同学的学号、姓名、成绩、课程名称。
select student.sid, student.sname, score.num, course.cname from score left outer join student on score.student_id=student.sid left outer join course on score.course_id=course.cid where num<60;
-
查询选修了 “生物课” 的所有学生ID、学生姓名、成绩。
select student.sid, student.sname, score.num from score left outer join student on score.student_id=student.sid where score.course_id=1;
-
查询选修了 “生物课” 且分数低于60的的所有学生ID、学生姓名、成绩。
select student.sid, student.sname, score.num from score left outer join student on score.student_id=student.sid where score.course_id=1 and score.num<60;
-
查询所有同学的学号、姓名、选课数、总成绩。
select student_id, student.sname, count(1), sum(num) from score left outer join student on score.student_id=student.sid group by student_id;
-
查询各科被选修的学生数。
select cname, count(1) from course left outer join score on course.cid=score.course_id group by cid;
-
查询各科成绩的总分、最高分、最低分,显示:课程ID、课程名称、总分、最高分、最低分。
select course_id, course.cname, sum(num), max(num), min(num) from score left outer join course on score.course_id=course.cid group by course_id;
-
查询各科成绩的平均分,显示:课程ID、课程名称、平均分。
select course_id, course.cname, avg(num) from score left outer join course on score.course_id=course.cid group by course_id;
-
查询各科成绩的平均分,显示:课程ID、课程名称、平均分(按平均分从大到小排序)。
select course_id, course.cname, avg(num) from score left outer join course on score.course_id=course.cid group by course_id order by avg(num) desc;
-
查询各科成绩的平均分和及格率,显示:课程ID、课程名称、平均分、及格率。
select course_id, course.cname, avg(num), sum(case when score.num >= 60 then 1 else 0 end)/count(1) *100 as percent from score left outer join course on score.course_id=course.cid group by course_id;
-
查询平均成绩大于60的所有学生的学号、平均成绩。
select student_id, avg(num) from score group by student_id having avg(num)>60;
-
查询平均成绩大于85的所有学生的学号、平均成绩、姓名。
select student_id, avg(num), student.sname from score left outer join student on score.student_id=student.sid group by student_id having avg(num)>85;
-
查询 “三年二班” 每个学生的学号、姓名、总成绩、平均成绩。
select student.sid, student.sname, sum(num), avg(num) from score left outer join student on score.student_id=student.sid left outer join class on student.class_id=class.cid where class.caption='三年二班' group by sid;
-
查询各个班级的班级名称、总成绩、平均成绩、及格率(按平均成绩从大到小排序)。
select class.caption, sum(num), avg(num), sum(case when num >60 then 1 else 0 end)/count(1)*100 as percent from score left outer join student on score.student_id=student.sid left outer join class on class.cid=student.class_id group by class.cid order by avg(num) desc;
-
查询学过 “张磊老师” 课的同学的学号、姓名。
select student.sid, sname from student left outer join score on student.sid=score.student_id left outer join course on score.course_id=course.cid left outer join teacher on course.teacher_id=teacher.tid where teacher.tname='张磊老师';
-
查询没学过 “张磊老师” 课的同学的学号、姓名。
select student.sid, sname from student where sid not in( select student.sid from student left outer join score on student.sid=score.student_id left outer join course on score.course_id=course.cid left outer join teacher on course.teacher_id=teacher.tid where teacher.tname='张磊老师');
-
查询选修 “李平老师” 所授课程的学生中,成绩最高的学生姓名及其成绩(不考虑并列)。
select student.sname, num from score left outer join student on score.student_id=student.sid left outer join course on score.course_id=course.cid left outer join teacher on course.teacher_id=teacher.tid where teacher.tname='李平老师' order by num desc limit 1;
-
查询选修 “李平老师” 所授课程的学生中,成绩最高的学生姓名及其成绩(考虑并列)。
select student.sname, num from score left outer join student on score.student_id=student.sid left outer join course on score.course_id=course.cid left outer join teacher on course.teacher_id=teacher.tid where teacher.tname='李平老师' and num=( select max(num) from score left outer join course on score.course_id=course.cid left outer join teacher on course.teacher_id=teacher.tid where teacher.tname='李平老师');
-
查询只选修了一门课程的全部学生的学号、姓名。
select student.sid, student.sname from score left outer join student on score.student_id=student.sid group by student_id having count(1) = 1;
-
查询至少选修两门课程的学生学号、学生姓名、选修课程数量。
select student.sid, student.sname, count(1) from score left outer join student on score.student_id=student.sid group by student_id having count(1) >= 2;
-
查询两门及以上不及格的同学的学号、学生姓名、选修课程数量。
select student.sid, student.sname, count(1) from score left outer join student on score.student_id=student.sid where num<60 group by student_id having count(1) >= 2;
-
查询选修了所有课程的学生的学号、姓名。
select student.sid, student.sname from score left outer join student on score.student_id=student.sid group by student_id having count(1) = (select count(1) from course);
-
查询未选修所有课程的学生的学号、姓名。
select student.sid, student.sname from score left outer join student on score.student_id=student.sid group by student_id having count(1) != (select count(1) from course);
-
查询所有学生都选修了的课程的课程号、课程名。
select course.cid, course.cname from score left outer join course on score.course_id=course.cid group by course.cid having count(1) = (select count(1) from student);
-
查询选修 “生物” 和 “物理” 课程的所有学生学号、姓名。
select student.sid, student.sname from score left outer join student on score.student_id=student.sid left outer join course on score.course_id=course.cid where course.cname in ('生物','物理') group by student_id having count(1)=2;
-
查询至少有一门课与学号为“1”的学生所选的课程相同的其他学生学号和姓名 。
select student.sid, student.sname from score left outer join student on score.student_id=student.sid left outer join course on score.course_id=course.cid where score.course_id in ( select course_id from score where student_id=1) and score.student_id != 1 group by student_id;
-
查询与学号为 “2” 的同学选修的课程完全相同的其他学生学号和姓名 。
select student.sid, student.sname from score left outer join student on score.student_id=student.sid left outer join course on score.course_id=course.cid where score.course_id in ( select course_id from score where student_id=2) and score.student_id in ( select student_id from score where student_id!=2 group by student_id having count(1)=(select count(1) from score where student_id=2)) group by student_id having count(1)=(select count(1) from score where student_id=2);
-
查询“生物”课程比“物理”课程成绩高的所有学生的学号。
select student_id, max( case cname when "生物" then num else 0 end ) AS sw, max( case cname when "物理" then num else 0 end ) AS wl from score left outer join course on score.course_id = course.cid where cname in ( "生物", "物理" ) group by student_id having sw > wl;
-
查询每门课程成绩最好的前3名 (不考虑成绩并列情况) 。
select cid, cname, (select student.sname from score left join student on score.student_id=student.sid where course_id = course.cid order by num desc limit 1 offset 0) as "第1名", (select student.sname from score left join student on student.sid = score.student_id where course_id = course.cid order by num desc limit 1 offset 1) as "第2名", (select student.sname from score left join student on student.sid = score.student_id where course_id = course.cid order by num desc limit 1 offset 2) as "第3名" from course;
-
查询每门课程成绩最好的前3名 (考虑成绩并列情况) 。
select * from score left join ( select cid, cname, (select num from score where course_id = course.cid GROUP BY num order by num desc limit 1 offset 0) as "最高分", (select num from score where course_id = course.cid GROUP BY num order by num desc limit 1 offset 1) as "第二高分", (select num from score where course_id = course.cid GROUP BY num order by num desc limit 1 offset 2) as third from course) as C on score.course_id = C.cid where score.num >= C.third;
-
创建一个表
sc
,然后将 score 表中所有数据插入到 sc 表中。create table sc( sid int not null auto_increment primary key, student_id int not null, course_id int not null, num int not null, constraint fk_student_id foreign key score(student_id) references student(sid), constraint fk_course_id foreign key score(course_id) references course(cid) )default charset=utf8; insert into sc select * from score;
-
向 sc 表中插入一些记录,这些记录要求符合以下条件:
- 学生ID为:没上过ID为 “2” 课程的学生的学号;
- 课程ID为:2
- 成绩为:80
insert into sc(student_id, course_id, num) select sid, 2, 80 from student where sid NOT IN (select student_id from score where course_id = 2);
-
向 sc 表中插入一些记录,这些记录要求符合以下条件:
- 学生ID为:没上过ID为 “2” 课程的学生的学号。
- 课程ID为:2。
- 成绩为:课程ID为3的最高分。
insert into sc(student_id, course_id, num) select sid, 2, (select max(num) from score where course_id=3) as num from student where sid not in (select student_id from score where course_id = 2);
2. 设计表结构
根据如下的业务需求设计相应的表结构,内部需涵盖如下功能。
- 注册
- 登录
- 发布博客
- 查看博客列表,显示博客标题、创建时间、阅读数量、评论数量、赞数量等。
- 博客详细,显示博文详细、评论等。
- 发表评论
- 赞 or 踩
- 阅读数量 + 1
参考如下图片请根据如下功能来设计相应的表结构。
注意:只需要设计表结构,不需要用python代码实现具体的功能(再学一点知识点后再更好的去实现)。
2.1 注册和登录
2.2 文章列表
2.3 文章详细
2.4 评论 & 阅读 & 赞 & 踩
注意:假设都是一级评论(不能回复评论)。
-- 创建数据库
create database blog default charset utf8 collate utf8_general_ci;
-- 进入数据库
use blog;
-- 创建数据表
create table user(
id int not null auto_increment primary key,
username varchar(16) not null,
nickname varchar(16) not null,
mobile char(11) not null,
password varchar(64) not null,
email varchar(64) not null,
ctime datetime not null
)default charset=utf8;
create table article(
id int not null auto_increment primary key,
title varchar(255) not null,
text text not null,
read_count int default 0,
comment_count int default 0,
up_count int default 0,
down_count int default 0,
user_id int not null,
ctime datetime not null,
constraint fk_article_user foreign key (user_id) references user(id)
)default charset=utf8;
create table comment(
id int not null auto_increment primary key,
content varchar(255) not null,
user_id int not null,
article_id int not null,
ctime datetime not null,
constraint fk_comment_user foreign key (user_id) references user(id),
constraint fk_comment_article foreign key (article_id) references article(id)
)default charset=utf8;
create table up_down(
id int not null auto_increment primary key,
choice tinyint not null,
user_id int not null,
article_id int not null,
ctime datetime not null,
constraint fk_up_down_user foreign key (user_id) references user(id),
constraint fk_up_down_article foreign key (article_id) references article(id)
)default charset=utf8;