3.8 对于教学数据库的三个基本表:
s(
学号
‾
\underline{学号}
学号,姓名,年龄, 性别)
sc(
学号
,
课程号
‾
\underline{学号, 课程号}
学号,课程号, 成绩)
c(
课程号
‾
\underline{课程号}
课程号,课程名, 任课教师姓名)
select cno, cname
from c
where not exists
(
select *
from s
inner join sc on s.sno = sc.sno
where c.cno = sc.cno
and s.sname = '张小飞'
);
select s.sno, sname
from s
inner join sc on s.sno = sc.sno
group by s.sno, sname
having count(sno) >= 3;
select c.cno, cname
from c
where not exists
(
select *
from s
where not exists
(
select *
from sc
where c.cno = sc.cno
and s.sno = sc.sno
)
);
delete
from sc
where grade is null;
update sc
set grade = 60
where grade < 60
and cno in
(
select cno
from c
where cname = '高等数学'
);
update sc
set grade = grade * 1.05
where sno in
(
select sno
from sc
inner join s on sc.sno = s.sno
where gener = '女'
and grade < avg(grade)
);
insert into c values('c8', 'vc++', '王昆');
3.9有下面四个关系模式
product (maker, model, type)
pc(model, speed, ram, hd, cd, price)
laptop(model, speed, ram, screen, price)
printer(model, color, type, price)
注:product表中type属性列的取值为pc或laptop或printer;printer表中的color取值为TRUE,false表示彩色还是单色
select *
from laptop
where price > 15000
and speed <
(
select avg(speed)
from lpc
where laptop.price = pc.price
);
select avg(price)
from product
inner join pc on product.model = pc.model
where maker = 'hp'
group by type
union
select avg(price)
from product
inner join laptop on product.model = laptop.model
where maker = 'hp'
group by type;
select maker
from product
where model in
(
select model
from printer
where color = true
and price <= all
(
select price
from printer
)
);
select maker, avg(screen)
from product
innner join laptop on product.model = laptop.model
group by maker;
select maker, max(price)
from product
inner join pc on product.model = pc.model
group by maker;
select maker, avg(hd)
from product
inner join pc on product.model = pc.model
where product.type = printer
group by maker;
答案自己写的,谨慎参考