CREATE TABLE student( sno VARCHAR(10) PRIMARY KEY, sname VARCHAR(20), sage VARCHAR(2), ssex VARCHAR(5) ); CREATE TABLE teacher( tno VARCHAR(10) PRIMARY KEY, tname VARCHAR(20) ); CREATE TABLE course( cno VARCHAR(10), cname VARCHAR(20), tno VARCHAR(20), CONSTRAINT pk_course PRIMARY KEY (cno,tno) ); CREATE TABLE sc( sno VARCHAR(10), cno VARCHAR(10), score NUMERIC(4,2), CONSTRAINT pk_sc PRIMARY KEY (sno,cno) );
/*******初始化学生表的数据******/ insert into student values ('s001','张三',23,'男'); insert into student values ('s002','李四',23,'男'); insert into student values ('s003','吴鹏',25,'男'); insert into student values ('s004','琴沁',20,'女'); insert into student values ('s005','王丽',20,'女'); insert into student values ('s006','李波',21,'男'); insert into student values ('s007','刘玉',21,'男'); insert into student values ('s008','萧蓉',21,'女'); insert into student values ('s009','陈萧晓',23,'女'); insert into student values ('s010','陈美',22,'女'); commit; /******************初始化教师表***********************/ insert into teacher values ('t001', '刘阳'); insert into teacher values ('t002', '谌燕'); insert into teacher values ('t003', '胡明星'); commit; /***************初始化课程表****************************/ insert into course values ('c001','J2SE','t002'); insert into course values ('c002','Java Web','t002'); insert into course values ('c003','SSH','t001'); insert into course values ('c004','Oracle','t001'); insert into course values ('c005','SQL SERVER 2005','t003'); insert into course values ('c006','C#','t003'); insert into course values ('c007','JavaScript','t002'); insert into course values ('c008','DIV+CSS','t001'); insert into course values ('c009','PHP','t003'); insert into course values ('c010','EJB3.0','t002'); commit; /***************初始化成绩表***********************/ insert into sc values ('s001','c001',78.9); insert into sc values ('s002','c001',80.9); insert into sc values ('s003','c001',81.9); insert into sc values ('s004','c001',60.9); insert into sc values ('s001','c002',82.9); insert into sc values ('s002','c002',72.9); insert into sc values ('s003','c002',81.9); insert into sc values ('s001','c003','59'); commit;
结果显示:
学生表
教师表
课程表
分数表
SELECT a.* ,b.score FROM (SELECT * FROM sc a WHERE a.`cno`='c001') a, (SELECT * FROM sc b WHERE b.`cno`='c002') b WHERE a.sno = b.sno AND a.score>b.score
SELECT sc.`sno`,AVG(sc.`score`) FROM sc GROUP BY sno HAVING AVG(sc.`score`)>60
SELECT b.*,a.`sname` FROM student a, (SELECT sc.`sno`,COUNT(sc.`cno`),SUM(sc.`score`) FROM sc GROUP BY sc.`sno`) b WHERE a.`sno`= b.sno
或者利用内联
SELECT a.*,COUNT(b.`cno`),SUM(b.`score`) FROM student a INNER JOIN sc b ON a.`sno`=b.`sno` GROUP BY b.`sno`
SELECT COUNT(*) FROM teacher t WHERE t.`tname` LIKE '刘%'
SELECT st.`sno`,st.`sname` FROM student st WHERE st.`sno` NOT IN ( SELECT DISTINCT a.sno FROM sc a, (SELECT cs.`cno`,cs.`cname` FROM course cs WHERE cs.`tno`=(SELECT b.`tno` FROM teacher b WHERE b.`tname`='谌燕')) b WHERE a.`cno`=b.cno )
SELECT * FROM sc a JOIN sc b ON a.`sno` =b.`sno` JOIN student stu ON a.`sno`=stu.`sno` WHERE a.cno='c001' AND b.cno='c002'
SELECT d.`sno`,d.`sname`,b.`cname`,c.`tname` FROM sc a LEFT JOIN course b ON a.`cno`=b.`cno` LEFT JOIN teacher c ON b.`tno`=c.`tno` LEFT JOIN student d ON a.`sno` = d.`sno` WHERE c.`tname`='谌燕'
SELECT c.`sno`,c.`sname`,a.`score`,b.`score` FROM sc a LEFT JOIN sc b ON a.`sno`=b.`sno` LEFT JOIN student c ON a.`sno`=c.`sno` WHERE a.`cno`='c001' AND b.`cno`='c002' AND a.`score`>b.`score`
SELECT a.`sname`,c.`cno`,b.`sno` FROM student a INNER JOIN sc b ON b.`sno` = a.`sno` LEFT JOIN course c ON c.`cno`=b.`cno` WHERE b.`score`<60
SELECT a.`sno`,a.`sname`,COUNT(sc.`cno`) FROM student a INNER JOIN sc ON a.`sno`=sc.`sno` GROUP BY a.`sno` HAVING COUNT(sc.`cno`)<(SELECT COUNT(cno) FROM course)
SELECT DISTINCT c.`sname`,c.`sno` FROM sc a LEFT JOIN sc b ON a.`cno` = b.`cno` LEFT JOIN student c ON a.`sno` = c.`sno` WHERE b.`sno`='s001' AND NOT a.`sno`=b.`sno`
SELECT * FROM sc LEFT JOIN student st ON st.sno=sc.sno WHERE sc.sno<>'s001' AND sc.cno IN (SELECT cno FROM sc WHERE sno='s001')
update sc c set score= ( select avg(c.score) from course a,teacher b where a.tno=b.tno and b.tname='谌燕' and a.cno=c.cno group by c.cno ) where cno in( select cno from course a,teacher b where a.tno=b.tno and b.tname='谌燕')