mysql考试总结

USE school;
-- 班级表
CREATE TABLE class(
    cid TINYINT PRIMARY KEY AUTO_INCREMENT,
    caption VARCHAR(20)
);

INSERT INTO class(caption) VALUES("三年二班"),("一年三班"),("三年一班");

SELECT * FROM class;

-- 老师表
CREATE TABLE teacher(
    tid TINYINT PRIMARY KEY AUTO_INCREMENT,
    tname VARCHAR(20)
);

INSERT INTO teacher(tname) VALUES("波多"),("苍空"),("饭岛");

SELECT * FROM teacher;

-- 学生表
CREATE TABLE student(
    sid TINYINT PRIMARY KEY AUTO_INCREMENT,
    sname VARCHAR(20),
    gender VARCHAR(10),
    class_id TINYINT,
    FOREIGN KEY (class_id) REFERENCES class(cid)
);

INSERT INTO student(sname,gender,class_id) VALUES
    ("钢蛋","女",1),
    ("铁锤","女",1),
    ("山炮","男",2);

SELECT * FROM student;

-- 课程表
CREATE TABLE course(
    cid TINYINT PRIMARY KEY AUTO_INCREMENT,
    cname VARCHAR(20),
    teacher_id TINYINT,
    FOREIGN KEY (teacher_id) REFERENCES teacher(tid)
);
ALTER TABLE course MODIFY cid TINYINT, DROP PRIMARY KEY;
ALTER TABLE course ADD CONSTRAINT xx FOREIGN KEY (cid) REFERENCES class(cid);
DESC course;
SHOW CREATE TABLE course;
INSERT INTO course(cname,teacher_id) VALUES
    ("生物",1),
    ("体育",1),
    ("物理",2);

SELECT * FROM course;
-- 成绩表
CREATE TABLE score(
    sid TINYINT PRIMARY KEY AUTO_INCREMENT,
    student_id TINYINT,
    course_id TINYINT,
    number INT,
    FOREIGN KEY (student_id) REFERENCES student(sid),
    FOREIGN KEY (course_id) REFERENCES course(cid)
);

INSERT INTO score(student_id, course_id, number) VALUES
    (1,1,60),
    (1,2,59),
    (2,2,100);

SELECT * FROM score;

DELETE FROM score WHERE sid=6;

# 二、操作表
#
# 1、自行创建测试数据
#
# 2、查询“生物”课程比“物理”课程成绩高的所有学生的学号;
SELECT B.student_id FROM
(SELECT score.student_id,score.number FROM score INNER JOIN course ON score.course_id = course.cid WHERE course.cname="生物") as A
    INNER JOIN
(SELECT score.student_id,score.number FROM score INNER JOIN course ON score.course_id = course.cid WHERE course.cname="物理") as B
        ON A.student_id=B.student_id AND A.number>B.number;

# 3、查询平均成绩大于60分的同学的学号和平均成绩;
INSERT INTO score(student_id, course_id, number) VALUES(2,3,56),
    (3,1,46),(3,2,59),(3,3,71),(4,1,90),(4,2,27);
SELECT student_id,avg(number) 平均成绩 FROM score GROUP BY student_id HAVING avg(number)>60;
# 4、查询所有同学的学号、姓名、选课数、总成绩;
SELECT student.sid,student.sname,count(student.class_id) 选课数,sum(number) 总成绩
    FROM student INNER JOIN score ON student.sid = score.student_id GROUP BY sname ORDER BY 总成绩;

# 5、查询姓“波”的老师的个数;
SELECT count(*) 波老师个数 FROM teacher WHERE tname LIKE "波%";
INSERT INTO teacher(tname) VALUES ("波大");
# 6、查询没学过“叶平”老师课的同学的学号、姓名;
  -- 得到所有同学学过的课程及其对应的老师,然后对应筛选
INSERT INTO teacher(tname) VALUES("叶平");
INSERT INTO course(cname, teacher_id) VALUES("历史",5);
INSERT INTO score(student_id, course_id, number) VALUES(1,5,81);

SELECT student.sid,student.sname,A.tname FROM score
    INNER JOIN student ON score.student_id = student.sid
    INNER JOIN (SELECT DISTINCT course.cid,course.teacher_id,course.cname,teacher.tname FROM
    course INNER JOIN teacher ON course.teacher_id = teacher.tid) AS A ON score.course_id = A.cid
    GROUP BY A.tname HAVING A.tname!="叶平";

# 7、查询学过“1”并且也学过编号“2”课程的同学的学号、姓名;
INSERT INTO student(sname,gender,class_id) VALUES("张三","男",3);
INSERT INTO score(student_id, course_id, number) VALUES(5,2,63);

SELECT B.sid 学号,B.sname 姓名 FROM
    (SELECT student.sid,student.sname FROM score INNER JOIN student ON score.student_id = student.sid
WHERE course_id=1) AS A
    INNER JOIN
    (SELECT student.sid,student.sname FROM score INNER JOIN student ON score.student_id = student.sid
WHERE course_id=2) AS B ON A.sid = B.sid;

# 8、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
SELECT * FROM student;    SELECT * FROM score;
INSERT INTO student(sname, gender, class_id) VALUES("王五","男",3);
INSERT INTO score(student_id, course_id, number) VALUES(8,5,93);

SELECT student.sid 学号,student.sname 姓名 FROM score INNER JOIN student
        ON score.student_id = student.sid AND course_id=5;

# 9、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;
INSERT INTO student(sname, gender, class_id) VALUES("赵六","女",2);
INSERT INTO score(student_id, course_id, number) VALUES(9,2,65),(9,1,70);
UPDATE score SET number = 65 WHERE sid=21;

SELECT * FROM student WHERE
    (SELECT number FROM score WHERE student.sid=score.student_id AND score.course_id=2)<
    (SELECT number FROM score WHERE student.sid=score.student_id AND score.course_id=1);

# 10、查询有课程成绩小于60分的同学的学号、姓名;
SELECT student.sid,student.sname FROM student
    INNER JOIN score ON student.sid = score.student_id WHERE score.number<60 GROUP BY student.sname;
INSERT INTO student(sname, gender, class_id) VALUES("钢镚","男",2);
INSERT INTO score(student_id, course_id, number) VALUES(4,3,48);
# 11、查询没有学全所有课的同学的学号、姓名;
-- 测试
SELECT count(cid) 总课程数 FROM course;
SELECT count(course_id) study_course FROM score GROUP BY student_id;
SELECT * FROM (SELECT count(cid) 总课程数 FROM course) AS A
    INNER JOIN
    (SELECT count(course_id) study_course FROM score GROUP BY student_id) AS B
    ON A.总课程数 = B.study_course;

-- 正确答案
SELECT student.sid,student.sname,count(course_id) 学习课程数 FROM score INNER JOIN student
    ON score.student_id = student.sid GROUP BY student_id
    HAVING count(course_id)=(SELECT count(cid) FROM course);

# 12、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;
#
# 13、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名;
#
# 14、查询和“2”号的同学学习的课程完全相同的其他同学学号和姓名;

# 15、删除学习“叶平”老师课的SC表记录;
#
# 16、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;
#
# 17、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;
#
# 18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
SELECT course_id,max(number) 最高分,min(number) 最低分 FROM score GROUP BY course_id;

INSERT INTO score(student_id, course_id, number) VALUES(2,1,76);
# 19、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
    -- 分析求出平均成绩并计算及格率
SELECT avg(number) 课程平均成绩 FROM score GROUP BY course_id;
SELECT count(student_id) 各科不及格人数 FROM score WHERE number>60 GROUP BY course_id;
SELECT count(student_id) 各科总人数 FROM score GROUP BY course_id;
    -- 答案如下
SELECT A.课程平均成绩,B.各科不及格人数/C.各科总人数 AS 及格率 FROM
    (SELECT course_id,avg(number) 课程平均成绩 FROM score GROUP BY course_id) AS A
        INNER JOIN
    (SELECT course_id,count(student_id) 各科不及格人数 FROM score WHERE number>60 GROUP BY course_id) AS B
        INNER JOIN
    (SELECT course_id,count(student_id) 各科总人数 FROM score GROUP BY course_id) AS C
    ON A.course_id = B.course_id AND A.course_id = C.course_id ORDER BY A.课程平均成绩;

# 20、课程平均分从高到低显示(显示任课老师);
SELECT teacher.tname,course.cname FROM teacher,course WHERE course.teacher_id = teacher.tid;
SELECT avg(number) 课程平均分 FROM teacher,score GROUP BY course_id;
-- 答案如下
SELECT A.tname 任课老师,B.课程平均分 FROM
    (SELECT teacher.tname,course.cname,course.cid FROM teacher,course WHERE course.teacher_id = teacher.tid) AS A
    INNER JOIN
    (SELECT avg(number) 课程平均分,teacher.tname,score.course_id FROM teacher,score GROUP BY course_id) AS B
    ON A.cid=B.course_id ORDER BY B.课程平均分 DESC;

# 21、查询各科成绩前三名的记录:(不考虑成绩并列情况)
SELECT * FROM score ORDER BY course_id;

# 22、查询每门课程被选修的学生数;
    -- 分析  根据成绩根据课程进行分类,按照学生ID统计次数
SELECT score.course_id 课程ID,count(student_id) 课程人次 FROM score GROUP BY course_id;

# 23、查询出只选修了一门课程的全部学生的学号和姓名;
SELECT student.sid,student.sname FROM
    (SELECT * FROM score GROUP BY student_id HAVING count(student_id)=1) AS A
    INNER JOIN student ON A.student_id = student.sid;
# 24、查询男生、女生的人数;
SELECT boy.男,girl.女 FROM
    (SELECT count(gender) 男 FROM student WHERE gender="男") as boy,
    (SELECT count(gender) 女 FROM student WHERE gender="女") as girl;

# 25、查询姓“张”的学生名单;
SELECT sid,sname FROM student WHERE sname LIKE "张%";
# 26、查询同名同姓学生名单,并统计同名人数;
SELECT sname 姓名,count(sname) 同名人数 FROM student GROUP BY sname;
# 27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
SELECT A.cid 课程号,B.课程平均分 FROM
    (SELECT teacher.tname,course.cname,course.cid FROM teacher,course WHERE course.teacher_id = teacher.tid) AS A
    INNER JOIN
    (SELECT avg(number) 课程平均分,teacher.tname,score.course_id FROM teacher,score GROUP BY course_id) AS B
    ON A.cid=B.course_id ORDER BY B.课程平均分 DESC;

# 28、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;
SELECT student.sid 学号,student.sname 姓名,avg(score.number) 平均成绩 FROM
    student INNER JOIN score ON student.sid = score.student_id GROUP BY sname;

# 29、查询课程名称为“生物”,且分数低于60的学生姓名和分数;
SELECT course.cname 课程,student.sname 姓名,score.number 分数 FROM score INNER JOIN student INNER JOIN course
    ON score.student_id = student.sid AND score.course_id=course.cid
    WHERE course.cname="生物" AND score.number<60;

# 30、查询课程编号为3且课程成绩在80分以上的学生的学号和姓名;
SELECT student.sid 学号,student.sname 姓名 FROM score INNER JOIN student
    ON score.student_id = student.sid AND score.course_id=3 AND score.number>80;

# 31、求选了课程的学生人数
INSERT INTO student(sname, gender, class_id) VALUES("李四","男",3);
INSERT INTO student(sname, gender, class_id) VALUES("胜七","女",2);
INSERT INTO score(student_id, course_id, number) VALUES(7,1,74);
-- 答案如下
SELECT count(A.student_id) 选课人数 FROM (SELECT DISTINCT student_id FROM score) AS A;

# 32、查询选修“苍空”老师所授课程的学生中,成绩最高的学生姓名及其成绩;
SELECT student.sname 姓名,max(number) 成绩 FROM score INNER JOIN student INNER JOIN course INNER JOIN teacher
    ON score.student_id=student.sid AND score.course_id=course.cid AND course.teacher_id=teacher.tid
    WHERE teacher.tname="苍空";

# 33、查询各个课程及相应的选修人数;
SELECT score.course_id 课程ID,count(student_id) 课程人次 FROM score GROUP BY course_id;
# 34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
#
# 35、查询每门课程成绩最好的前两名;
#
# 36、检索至少选修两门课程的学生学号;
    -- 分析 根据学生ID进行分组,统计课程出现次数,筛选课程次数大于等于2的学生
SELECT student_id FROM score GROUP BY student_id HAVING count(course_id)>=2;
# 37、查询全部学生都选修的课程的课程号和课程名;
SELECT score.student_id 学生ID,course.cname 所选课程,course.cid 课程ID FROM score INNER JOIN course
    ON score.course_id = course.cid ORDER BY 学生ID;

# 38、查询没学过“叶平”老师讲授的任一门课程的学生姓名;
#
# 39、查询两门以上不及格课程的同学的学号及其平均成绩;
SELECT A.student_id 学号,B.平均成绩 FROM
    (SELECT student_id FROM score WHERE number<60 GROUP BY student_id HAVING count(number)>=2) AS A
INNER JOIN
    (SELECT student_id,avg(number) 平均成绩 FROM score GROUP BY student_id) AS B ON A.student_id=B.student_id;

# 40、检索“1”课程分数小于60,按分数降序排列的同学学号;
SELECT student_id FROM score WHERE course_id=1 AND number<60;
# 41、删除“2”同学的“1”课程的成绩;
    -- 此题已答

  

时间: 2024-10-06 07:45:45

mysql考试总结的相关文章

mysql ocp考试

MySQL OCP概述 Oracle OCP(Oracle Certified Professional)考试或许各位都非常熟悉.然而,自从MySQL被Oracle公司收购后,Oracle公司就推出了MySQL OCP的考试.略有遗憾的是,MySQL并没有OCM(Oracle Certified Master)考试,而OCM认证是可以在技术移民时获得相应的技能认可. MySQL OCP考试可以自行报名,无需参加相关机构的培训.这意味着,如果你能力足够强大,那么完全可以自行报名参加考试.另外一方面

mysql练习题-查询同时参加计算机和英语考试的学生的信息-遁地龙卷风

(-1)写在前面 文章参考http://blog.sina.com.cn/willcaty. 针对其中的一道练习题想出两种其他的答案,希望网友给出更多回答. (0) 基础数据 student表 +-----+--------+------+-------+------------+--------------+ | id  | name   | sex  | birth | department | address      | +-----+--------+------+-------+--

ssm+mysql+jsp打造在线考试系统WeKnow-后端设计

一.登陆模块 前台提交账号和密码传到后台处理控制层 1.1 首先是控制器 @RequestMapping(value="/studentLogin", method=RequestMethod.POST) public ModelAndView studentLogin(StudentInfo student, HttpServletRequest request) { ModelAndView model = new ModelAndView(); StudentInfo login

随机获得MySQL数据库中100条数据方法 驾照题库项目 MVC架构 biz业务层的实现类 根据考试类型rand或order通过dao数据访问层接口得到数据库中100或全部数据

package com.swift.jztk.biz; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Random; import com.google.gson.Gson; import com.swift.jztk.bean.Result

在线考试系统(Online Exam System)--ASP.NET

用户设计 -|学生 -|老师 -|管理员 学生结构设计 -|个人信息管理 -|修改个人信息 -|修改登录密码 -|选课中心 -|显示所有老师所开课的信息可进行选课 -|显示自己已选课程 -|在线考试 -|对已选老师开设的课程选择进行考试 -|成绩查询 -|查看自己考试成绩   老师结构设计 -|个人信息管理 -|修改个人信息 -|修改登录密码 -|课程管理 -|显示学校开设的课程(老师可选择添加课程) -|显示老师开设的课程 -|考试管理 -|显示老师自己开设的课程 -|对课程添加试题(选择.填

初识MySQL——人生若如初相逢

CREATE TABLE `student`(`studentNo` INT (4) NOT NULL PRIMARY KEY COMMENT '学号',`loginPwd` VARCHAR(20) NOT NULL COMMENT '密码',`studentName` VARCHAR (50) NOT NULL COMMENT '姓名',`sex` CHAR(2) NOT NULL DEFAULT '男' COMMENT '性别',`gradeId` INT (4) UNSIGNED COMM

mysql 实用

( 静静 ) DBA 数据库管理员 IDC监控 mysql数据库管理  7 Linux安全        3 Linux监控        3 ---------------------------------------- 在mysql中 我创建一个student库 并 设置字符集:utf8 -- UTF-8 Unicode,排序规则:utf8_general_ci 语句怎样的 mysql> create database if not exists student default chars

《MySQL入门很简单》练习10.9

(1)在查询之前,先按照表10.3和表10.4的内容创建student表和score表 首先我创建一个本章的数据库(名字随便取,主要是每个程序猿都不应该被规矩束缚对不对), 之后对照这student表和score表的设计创建,代码如下: (2)按照表10.5和10.6的内容为student表和score表增加记录 本来想偷点懒的,突然遇到这种问题,也是醉了.等我把数据导入进去的时候才恍然大悟,失了智, 中英文的单引号我留意,逗号反而忘了切换,由此可见,经验是得积累的,遇到问题漠慌,解决问题的途径

Mysql几种索引类型的区别及适用情况

如大家所知道的,Mysql目前主要有以下几种索引类型:FULLTEXT,HASH,BTREE,RTREE. 那么,这几种索引有什么功能和性能上的不同呢? FULLTEXT 即为全文索引,目前只有MyISAM引擎支持.其可以在CREATE TABLE ,ALTER TABLE ,CREATE INDEX 使用,不过目前只有 CHAR.VARCHAR ,TEXT 列上可以创建全文索引.值得一提的是,在数据量较大时候,现将数据放入一个没有全局索引的表中,然后再用CREATE INDEX创建FULLTE