mysql经典面试题
數據庫腳本文件:?
| /* Navicat MySQL Data Transfer ? Source Server ????????: mysql Source Server Version : 50549 Source Host ??????????: localhost:3306 Source Database ??????: ooxxoo ? Target Server Type ???: MYSQL Target Server Version : 50549 File Encoding ????????: 65001 ? Date: 2019-08-14 21:27:27 */ ? SET FOREIGN_KEY_CHECKS=0; ? -- ---------------------------- -- Table structure for course -- ---------------------------- DROP TABLE IF EXISTS `course`; CREATE TABLE `course` ( ??`cno` int(11) NOT NULL, ??`cname` varchar(50) DEFAULT NULL, ??`tno` int(11) DEFAULT NULL, ??PRIMARY KEY (`cno`), ??KEY `tno` (`tno`), ??CONSTRAINT `course_ibfk_1` FOREIGN KEY (`tno`) REFERENCES `teacher` (`tno`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ? -- ---------------------------- -- Records of course -- ---------------------------- INSERT INTO `course` VALUES ('1', '數學', '1'); INSERT INTO `course` VALUES ('2', '語文', '2'); INSERT INTO `course` VALUES ('3', '英語', '3'); ? -- ---------------------------- -- Table structure for sc -- ---------------------------- DROP TABLE IF EXISTS `sc`; CREATE TABLE `sc` ( ??`sno` int(11) NOT NULL, ??`cno` int(11) NOT NULL, ??`score` int(11) DEFAULT NULL, ??PRIMARY KEY (`sno`,`cno`), ??KEY `cno` (`cno`), ??CONSTRAINT `sc_ibfk_1` FOREIGN KEY (`sno`) REFERENCES `student` (`sno`), ??CONSTRAINT `sc_ibfk_2` FOREIGN KEY (`cno`) REFERENCES `course` (`cno`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ? -- ---------------------------- -- Records of sc -- ---------------------------- INSERT INTO `sc` VALUES ('1', '1', '98'); INSERT INTO `sc` VALUES ('1', '2', '50'); INSERT INTO `sc` VALUES ('1', '3', '66'); INSERT INTO `sc` VALUES ('2', '1', '95'); INSERT INTO `sc` VALUES ('2', '2', '100'); INSERT INTO `sc` VALUES ('2', '3', '86'); INSERT INTO `sc` VALUES ('3', '1', '88'); INSERT INTO `sc` VALUES ('3', '2', '77'); INSERT INTO `sc` VALUES ('3', '3', '57'); INSERT INTO `sc` VALUES ('4', '1', '78'); INSERT INTO `sc` VALUES ('4', '2', '100'); INSERT INTO `sc` VALUES ('4', '3', '78'); INSERT INTO `sc` VALUES ('5', '2', '45'); ? -- ---------------------------- -- Table structure for student -- ---------------------------- DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( ??`sno` int(11) NOT NULL, ??`sname` varchar(50) DEFAULT NULL, ??`sage` int(11) DEFAULT NULL, ??`ssex` varchar(2) DEFAULT NULL, ??PRIMARY KEY (`sno`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ? -- ---------------------------- -- Records of student -- ---------------------------- INSERT INTO `student` VALUES ('1', '李孟冬', '21', '男'); INSERT INTO `student` VALUES ('2', '劉蕾', '23', '女'); INSERT INTO `student` VALUES ('3', '李斌', '21', '男'); INSERT INTO `student` VALUES ('4', '天使', '18', '女'); INSERT INTO `student` VALUES ('5', '魔鬼', '99', '男'); ? -- ---------------------------- -- Table structure for teacher -- ---------------------------- DROP TABLE IF EXISTS `teacher`; CREATE TABLE `teacher` ( ??`tno` int(11) NOT NULL, ??`tname` varchar(50) DEFAULT NULL, ??PRIMARY KEY (`tno`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ? -- ---------------------------- -- Records of teacher -- ---------------------------- INSERT INTO `teacher` VALUES ('1', '李鑫'); INSERT INTO `teacher` VALUES ('2', '董老師'); INSERT INTO `teacher` VALUES ('3', '李老師'); ? |
天黑請,請看題?
?
| ? select * from student select * from course select * from sc select * from teacher ? select * from student,course,sc where student.sno=sc.sno and course.cno=sc.cno ? 查詢課程1的成績比課程2的成績高的所有學生的學號 select a.sno from (select sno,score from sc where cno=1) a, (select sno,score from sc where cno=2) b where a.score>b.score and a.sno=b.sno ? 查詢平均成績大于60分的同學的學號和平均成績 select s.sno,avg(score) avgscore from student s,sc where s.sno=sc.sno group by s.sno having avgscore>60 ? 查詢所有同學的學號、姓名、選課數、總成績 select s.sno,s.sname,count(cno) 選課數,sum(score) 總成績 from student s,sc where s.sno=sc.sno group by s.sno ? 查詢姓“李”的老師的個數 select count(tname) 個數 from teacher where tname like '李%' ? 查詢沒學過“李鑫”老師課的同學的學號、姓名 select sno,sname from student where sno in (select sno from sc,course c,teacher t where sc.cno = c.cno and c.tno = t.tno and t.tname='李鑫') ? 查詢同時學過課程1和課程2的同學的學號、姓名 select c.sno,c.sname from (select sno from sc where sc.cno=1) a, (select sno from sc where sc.cno=2) b, student c where a.sno = b.sno and a.sno = c.sno ? select s.sno,s.sname from student s,(select t.sno from (select distinct sno from sc where sc.cno=1 union all select distinct sno from sc where sc.cno=2) t group by t.sno having count(t.sno)=2) r where s.sno = r.sno ? 查詢學過“李鑫”老師所教所有課程的所有同學的學號、姓名 select stu.sno,stu.sname from student stu, (select sno from sc,course c,teacher t where sc.cno=c.cno and c.tno=t.tno and t.tname='李鑫') t where stu.sno = t.sno ? ? 查詢課程編號1的成績比課程編號2的成績高的所有同學的學號、姓名 select s.sno,s.sname from student s, (select sno,score from sc where cno=1) sc1, (select sno,score from sc where cno=2) sc2 where sc1.score>sc2.score and s.sno = sc1.sno and s.sno = sc2.sno ? ? 查詢所有課程成績小于60分的同學的學號、姓名 逆向思維!!! ? select sno,sname from student where sno not in (select distinct sno from sc where score > 60) ? ? 查詢至少有一門課程與學號為1的同學所學課程相同的同學的學號和姓名 select distinct a.sno, a.sname from student a, sc b where a.sno <> 1 and a.sno=b.sno and b.cno in (select cno from sc where sno = 1) ? ? 把“sc”表中“李鑫”所教課的成績都更改為此課程的平均成績 ? ? 查詢和編號為2的同學學習的課程完全相同的其他同學學號和姓名 ? ? 刪除學習“李鑫”老師課的sc表記錄 delete from sc,course,teacher ? |
?
總結
以上是生活随笔為你收集整理的mysql经典面试题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL 中 delete 语句的子查
- 下一篇: 使用基本工具类和预编译进行对数据库的增删