本帖最后由 爱编码的J 于 2018-10-31 21:53 编辑
MySQL数据做为面试必问的题目,笔者给大家分享一些面试中遇到的题(题目不一定很难,但是一定会涉及很多常见的知识点)
有以下三个表
(1) SQL给王二新增一条分数记录:科目二,70分(5分)
(2)把王二的科目三成绩改为66分(5分)
(3) SQL选出平均分大于等于60分的学生姓名(8分)
(4) 现有三种查询如下:(7分) select * from score where score = 100; select * from score where student_id = 5 and score =100; select * from score where student_id = 5 and subject_id = 2 and score = 100 新建一个索引能够同时提升以上三条SQL的查询性能。
参考答案(参考答案仅代表个人意见,如果有更好的答案或者是想法,在回复中多多交流): [size=14.0000pt](1)INSERT INTO `score`(`student_id`,`subject_id`,`score`) VALUES(6,2,70)
(2)UPDATE `score` SET `score`=66 WHERE `student_id`=(SELECT `id` from `student` WHERE `name`="王二") and `subject_id`=(SELECT `id` from `subject` WHERE `name`="科目三")
(3)SELECT `name` FROM `student` a JOIN `score` b on a.id=b.student_id GROUP BY a.id HAVING AVG(b.score)>60
(4)根据最左原则,简历一个联合索引index(score,student_id,subject_id)
|