########
1.2 要求一......用SQL语言完成表的创建以及数据的插入。
#创建student
CREATE TABLE student(
sno INT(10) COMMENT '学号',
sname VARCHAR(10) COMMENT '姓名',
age INT(3) COMMENT '年龄',
sex VARCHAR(1) COMMENT '性别')
#insert插入student数据
INSERT INTO student(sno,sname,age,sex) VALUES(1,'李强',18,'男')
INSERT INTO student(sno,sname,age,sex) VALUES(2,'刘丽',188,'女')
INSERT INTO student(sno,sname,age,sex) VALUES(3,'凤凰',19,'女')
INSERT INTO student(sno,sname,age,sex) VALUES(4,'婕拉',20,'女')
INSERT INTO student(sno,sname,age,sex) VALUES(5,'张友',21,'男')
INSERT INTO student(sno,sname,age,sex) VALUES(6,'孙悟空',500,'男')
#create创建course
CREATE TABLE course(
cno VARCHAR(10) COMMENT '课程代码',
cname VARCHAR(10) COMMENT '课程名称',
teacher VARCHAR(10) COMMENT '教师')
#insert插入course数据
INSERT INTO course(cno,cname,teacher) VALUES('K1','C语言','王华')
INSERT INTO course(cno,cname,teacher) VALUES('K2','C+语言','薛哥')
INSERT INTO course(cno,cname,teacher) VALUES('K3','C++语言','薛哥')
INSERT INTO course(cno,cname,teacher) VALUES('K4','java语言','薛哥')
INSERT INTO course(cno,cname,teacher) VALUES('K5','数据库原理','程军')
INSERT INTO course(cno,cname,teacher) VALUES('K6','linux语言','薛哥')
INSERT INTO course(cno,cname,teacher) VALUES('K7','PHP语言','薛哥')
INSERT INTO course(cno,cname,teacher) VALUES('K8','编译原理','程军')
#create创建表SC
CREATE TABLE sc(
sno INT(10) COMMENT '学号',
cno VARCHAR(10) COMMENT '课程代码',
score INT(10) COMMENT '成绩')
#insert插入sc数据
INSERT INTO sc(sno,cno,score) VALUES(1,'K1',83)
INSERT INTO sc(sno,cno,score) VALUES(2,'K1',85)
INSERT INTO sc(sno,cno,score) VALUES(5,'K1',92)
INSERT INTO sc(sno,cno,score) VALUES(2,'K5',90)
INSERT INTO sc(sno,cno,score) VALUES(5,'K5',84)
INSERT INTO sc(sno,cno,score) VALUES(5,'K8',80)
########
1.3 要求二.....用SQL语言完成如下要求:
(1) 检索至少选修"程军"老师所授全部课程的学生姓名(SNAME);
#检索学生姓名
SELECT sname FROM student
#检索选修 程军 课程的 课程代码
SELECT cno FROM course WHERE `teacher`='程军'
#检索选修课程的代码的学号
SELECT sno FROM sc WHERE cno IN (SELECT cno FROM course WHERE `teacher`='程军')
#结合
SELECT sname FROM student WHERE sno IN (SELECT sno FROM sc WHERE cno IN (SELECT cno FROM course WHERE `teacher`='程军'))
##以上结果为错误的查询方法!正确结果如下(运用exists):
SELECT * FROM student WHERE NOT EXISTS (
SELECT * FROM course WHERE teacher = '程军' AND NOT EXISTS(
SELECT * FROM sc WHERE sno=student.`sno` AND cno=course.`cno`))
(2) 检索"李强"同学不学课程的课程号(CNO);
SELECT cno FROM course WHERE NOT EXISTS(
SELECT * FROM student WHERE sname = '李强' AND EXISTS (
SELECT * FROM sc WHERE sc.`sno`=student.`sno` AND sc.`cno`=course.`cno`))
(2.2)查询所有学生所学的科目编号以及成绩
SELECT st.sno,st.sname,cr.cname,sc.`score`,cr.teacher
FROM student st,sc,course cr
WHERE st.sno=sc.`sno` AND cr.cno = sc.`cno`
(2.3)查询没有选修程军老师课程的学生姓名:
SELECT * FROM student st WHERE NOT EXISTS(
SELECT * FROM course cr WHERE teacher = '程军' AND EXISTS(
SELECT * FROM sc WHERE sc.`cno`=cr.cno AND sc.`sno`=st.sno))
(2.4)找出李强老师学生
SELECT * FROM student st,sc,course cr WHERE sname='李强' AND sc.`sno`=st.`sno` AND sc.`cno`=cr.cno
(3) 检索选修不少于3门课程cno的学生学号(SNO);
#检索学号select sno from sc
SELECT sno FROM sc GROUP BY sno HAVING COUNT(*) >= 3
(4) 检索选修全部课程的学生姓名(SNAME)
#错误写法:SELECT sname FROM student WHERE sno IN (SELECT sno FROM sc GROUP BY sno HAVING COUNT(*) >= 3)
#正确写法:
SELECT sname FROM student WHERE NOT EXISTS (
SELECT * FROM course WHERE NOT EXISTS(
SELECT * FROM sc WHERE sc.sno=student.`sno` AND sc.cno=course.`cno`))
(5) 检索不学"C语言"的学生信息(因所有学生都有学习C语言,所以把C语言改为“编译原理”)
#错误写法:SELECT * FROM student WHERE sno IN (SELECT sno FROM sc WHERE cno IN (SELECT cno FROM course WHERE cname <> 'C语言'))
#正确写法:
SELECT * FROM student WHERE NOT EXISTS(
SELECT * FROM course WHERE cname = '编译原理' AND EXISTS (
SELECT * FROM sc WHERE sc.`sno`=student.`sno` AND sc.`cno`=course.`cno`))
#########
1.4 要求三 请用SQL语言完成如下查询:
(1)查询“程军”老师所教授的所有课程;
SELECT cname FROM course WHERE teacher = '程军'
(2)查询“李强”同学所有课程的成绩;
SELECT course.cname,sc.score FROM course,sc WHERE EXISTS(
SELECT * FROM student WHERE sname = '李强' AND EXISTS (
SELECT * FROM sc WHERE sc.`sno`=student.`sno` AND sc.`cno`=course.`cno`)) GROUP BY course.cname
(3)查询课程名为“C语言”的平均成绩;
#C语言课程标号: select cno from course where cname = 'C语言'
SELECT AVG(score) FROM sc WHERE cno = (SELECT cno FROM course WHERE cname = 'C语言')
(4)查询选修了所有课程的同学信息。
SELECT * FROM student WHERE NOT EXISTS (
SELECT * FROM course WHERE NOT EXISTS(
SELECT * FROM sc WHERE sc.sno=student.`sno` AND sc.cno=course.`cno`))
>>>>>>>>>>
查询学号为1的学生 选修了的课程
#方式一:
SELECT * FROM course WHERE cno = (
SELECT cno FROM sc WHERE sno=1)
#方式二:
SELECT * FROM course WHERE EXISTS (
SELECT * FROM student WHERE sno = 1 AND EXISTS (
SELECT * FROM sc WHERE sc.`sno`=student.sno AND sc.`cno`=course.`cno`))
查询学号为1的学生 是否选修“程军”老师的课程
SELECT * FROM course WHERE teacher = '程军' AND EXISTS (
SELECT * FROM student WHERE sno = 1 AND EXISTS (
SELECT * FROM sc WHERE sc.`sno`=student.sno AND sc.`cno`=course.`cno`))
查询学号为1的学生 是否选修了“程军”老师的课程,如果没有选修,请输出该同学没有选修的课程信息
SELECT * FROM course WHERE teacher = '程军' AND NOT EXISTS (
SELECT * FROM student WHERE sno = 1 AND EXISTS (
SELECT * FROM sc WHERE sc.`sno`=student.sno AND sc.`cno`=course.`cno`))
查看选修了程军老师课程的学生姓名信息:
SELECT * FROM student WHERE EXISTS (
SELECT * FROM course WHERE teacher = '程军' AND EXISTS (
SELECT * FROM sc WHERE sc.`sno`=student.sno AND sc.`cno`=course.`cno`))
查看没有全部选修程军老师课程的学生信息
SELECT * FROM student WHERE EXISTS (
SELECT * FROM course WHERE teacher = '程军' AND NOT EXISTS (
SELECT * FROM sc WHERE sc.`sno`=student.sno AND sc.`cno`=course.`cno`))
查看没有选修程军老师课程的学生姓名信息:
SELECT * FROM student WHERE NOT EXISTS (
SELECT * FROM course WHERE teacher = '程军' AND EXISTS (
SELECT * FROM sc WHERE sc.`sno`=student.sno AND sc.`cno`=course.`cno`))
查看选修了程军老师所有课程的学生姓名信息:
SELECT * FROM student WHERE NOT EXISTS (
SELECT * FROM course WHERE teacher='程军' AND NOT EXISTS(
SELECT cno FROM sc WHERE sc.`sno`=student.`sno` AND course.`cno`=sc.`cno`))
(注:个人所写,并不保证全部正确、写法最优,如有意见,欢迎指出)