select c.cno,cname from course c left join sc on c.cno = sc.cno
where sc.cno is null
14ѯϵЩѧûѡΣгѧ
select sname from student s left join sc on s.sno = sc.sno Where sdept = 'ϵ' and sc.sno is null
15Ӳѯʵ²ѯ
1ѯѡC001γ̵ѧϵ
select sname,sdept from student where sno in( select sno from sc where cno = ' C001')
2ѯͨŹϵɼ80ϵѧѧź select sno,sname from student where sno in( select sno from sc where grade > 80) and sdept = 'ͨŹϵ'
3ѯϵԳɼߵѧ
select sname from student s join sc on s.sno = sc.sno where sdept = 'ϵ'
and grade = (select max(grade) from sc join student s on s.sno = sc.sno
where sdept = 'ϵ')
4ѯѧ䡣 select sname,sage from student
Where sage = (select max(sage) from student and ssex = '') and ssex = ''
5ѯC001γ̵ĿԳɼڸÿγƽɼѧѧźͳɼ select sno,grade from sc where cno = ' C001'
And grade > (select avg(grade) from sc where cno = ' C001')
16һ±Ϊtest_tṹΪCOL1, COL 2, COL 3У COL1ֵͣ
COL2ַͣΪ10ֵ COL3ַͣΪ10ֵ
двݵ䣨հ״ʾֵ
COL1 1 2 COL2 B1 B2 B3 COL3 C2 create table test_t ( COL1 int,
COL2 char(10) not null,
9
COL3 char(10) )
insert into test_t values(NULL, 'B1', NULL) insert into test_t values(1, 'B2', 'C2')
insert into test_t(COL1, COL2) values(2, 'B3')
17ɾγɼС50ֵѧѡμ¼ delete from sc where grade < 50
18ѡC001γ̵ѧijɼ10֡
update sc set grade = grade + 10 where cno = 'C001'
19ϵѡޡĻѧγ̵ѧijɼ10֡ update sc set grade = grade + 10 where sno in(
select sno from student where sdept = 'ϵ') and cno in(
select cno from course where cname = 'Ļѧ')
20˵ʹͼĺô
ʹͼܹºô
? ݲѯ䣺ûԽӵIJѯװͼУԺûʹ
ͬIJѯʱֻͼвѯɡ
? ʹûܴӶǶȿͬһݣͼʹͬûԲͬķʽͬһ
ݣͬûͬһݿʱǷdzҪġ ? ݵİȫԣʹͼԶû鿴Щݲεе
ݣӶݿݵİȫԡ
? ṩһ̶ȵԣͼӦݿģʽеģʽˣԽ
ûݵIJͼϣֱӶģʽвģʽ仯ʱͼԲ䡣
21ʹͼԼӿݵIJѯٶȣ仰Ϊʲô
𣺲ԡʹͼӿݵIJѯٶȣһήݲѯٶȡΪͨ
ͼѯʱҪȽѯתΪԻIJѯʱתDZȽϸӵġˣͨͼѯݱֱӶԻѯҪ
22дҪͼSQL䡣
1ѯѧѧšϵγ̺šγγѧ֡ CREATE VIEW V1
AS
SELECT S.Sno, Sname, Sdept, C.Cno, Cname, Ccredit FROM Student S JOIN SC ON S.Sno = SC.Sno JOIN Course C ON C.Cno = SC.Cno
2ѯѧѧšѡγͿԳɼ
10
CREATE VIEW V2
AS
SELECT S.Sno, Sname, Cname, Grade
FROM Student S JOIN SC ON S.Sno = SC.Sno
JOIN Course C ON C.Cno = SC.Cno
3ͳÿѧѡͼҪгѧѧźѡ CREATE VIEW V3
AS
SELECT Sno, Count(*) AS total FROM SC GROUP BY Sno
4ͳÿѧѧֵͼҪгѧѧźѧ֣˵Գɼڵ60ſɻôſγ̵ѧ֣ : CREATE VIEW V4
AS
SELECT Sno, SUM(Ccredit) sum_credit
FROM SC JOIN Course C ON C.Cno = SC.Cno
WHERE Grade >= 60 GROUP BY Sno
23õ22⽨ͼ²ѯ
1ѯԳɼڵ90ֵѧγͳɼ SELECT Sname, Cname, Grade FROM V2 WHERE Grade >= 90
2ѯѡ3ŵѧѧźѡ SELECT Sno, total FROM V3 WHERE total > 3
3ѯϵѡ3ŵѧѡ SELECT Sname, total FROM V3 JOIN Student S on S.Sno = V3.Sno
WHERE total > 3 and Sdept = 'ϵ'
4ѯѧֳ10ֵѧѧšϵѧ֡ SELECT S.Sno, Sname, Sdept, sum_credit
FROM Student S JOIN V4 ON S.Sno = V4.Sno WHERE sum_credit > 10
5ѯڵ20ѧУѧֳ10ֵѧ䡢ϵѧ֡
SELECT Sname, Sage, Sdept, sum_credit
FROM Student S JOIN V4 ON S.Sno = V4.Sno WHERE sum_credit > 10 and Sage >= 20
2422⣨4ͼʹѯÿѧѧšϵѡȫγ
11
ѧԼܵѡ CREATE VIEW V4_1
AS
SELECT Sno, SUM(Ccredit) sum_credit, COUNT(*) Total FROM SC JOIN Course C ON C.Cno = SC.Cno
GROUP BY Sno
ALTER VIEW V4 AS
SELECT S.Sno, Sname, Sdept, sum_credit, Total
FROM Student S JOIN V4_1 ON S.Sno = V4_1.Sno
12