1.查询每个工程项目及其零件使用情况。(两表连接)
命令:select J.*,SPJ.* from J,SPJ where J.jno = SPJ.jno
或者:select J.jno,J.jname,J.city,SPJ.sno,SPJ.pno,SPJ.qty from J,SPJ where J.jno = SPJ.jno(自然连接)
2.查询每个工程项目及其零件使用情况,包括没有使用零件的工程项目。(两表外连接)
命令:select J.jno,J.jname,J.city,SPJ.sno,SPJ.pno,SPJ.qty from J LEFT OUTER JOIN SPJ on( J.jno = SPJ.jno )
3.查询使用供应商S1供应的零件P1的工程号和工程名(两种方法:连接,嵌套)
命令:select J.jno,jname from J,SPJ where J.jno = SPJ.jno and SPJ.sno = 'S1' and SPJ.pno = 'P1'(连接)
select jno,jname from J where jno in (select jno from SPJ where sno = 'S1' and pno = 'P1')(嵌套)
4.查询查询每个工程项目的工程号、工程名、使用的零件名、零件的供应商名及数量(多表连接)
命令:select J.jno,J.jname,SPJ.pno,S.sname,SPJ.qty from J,SPJ,S where J.jno = SPJ.jno and S.Sno = SPJ.sno
5.查询与供应商“东方红”在同一城市的供应商名(同表嵌套)
命令:select sname from S where city in (select city from S where sname = '东方红') and sname <> '东方红'
6.查询与供应商“精益”在同一城市的工程项目名(异表嵌套)
命令:select jname from J where city in (select city from S where sname = '精益')
7.查询供应了“螺丝刀”的供应商号和供应商名(两种方法:连接,嵌套)
命令:select DISTINCT S.sno,S.sname from S,P,SPJ where P.pname = '螺丝刀' and P.pno = SPJ.pno and SPJ.sno = S.sno(连接)
select sno,sname from S where sno in (select sno from SPJ where pno in (select pno from P where pname = '螺丝刀'))(嵌套)
8.查询使用零件P1且数量超过300的所有工程的工程号和工程名(带group by的两表嵌套)
命令:select jno,jname from J where jno in (select jno from SPJ where pno = 'P1' group by jno having SUM(qty) > 300)
9.查询每种颜色的零件中重量大于该颜色零件平均重量的零件信息(两种方法:相关子查询,基于派生表的查询)
命令:select * from P x where weight > (select AVG(weight) from P y where x.color = y.color)(相关子查询)
select pno,pname,color,weight from P,(select color,AVG(weight) from P group by color) as avg_p(avg_color,avg_weight) where P.color = avg_color and P.weight > avg_p.avg_weight (基于派生表的查询)
10.查询使用了S2供应的P3的工程号与使用了S3供应的P3的工程号的并集。(两种方法:集合查询,非集合查询)
命令:select jno from SPJ where sno = 'S2' and pno = 'P3' union select jno from SPJ where sno = 'S3' and pno = 'P3'(集合查询)
select DISTINCT jno from SPJ where sno = 'S2' and pno = 'P3' or ( sno = 'S3' and pno = 'P3')(非集合查询)
11.查询使用了S2供应的P3的工程号与使用了S3供应的P3的工程号的交集。
命令:select jno from SPJ where sno = 'S2' and pno = 'P3' intersect select jno from SPJ where sno = 'S3' and pno = 'P3'
12.查询使用了S2供应的P3的工程号与使用了S3供应的P3的工程号的差集。
命令:select jno from SPJ where sno = 'S2' and pno = 'P3' EXCEPT select jno from SPJ where sno = 'S3' and pno = 'P3'
13.找出没有使用天津供应商生产的红色零件的工程号。(减法查询)?
命令:select jno from J where jno not in ( select jno from SPJ where pno in ( select pno from P where color = '红') and sno in (select sno from S where city = '天津') )
14.查询至少使用了供应商S1供应的全部零件的工程号。(除法查询)?
命令:
select DISTINCT jno
from SPJ SPJ_1
where not exists
(select *
from SPJ SPJ_2
where SPJ_2.sno = 'S1' and
not exists
(select *
from SPJ SPJ_3
where SPJ_3.jno = SPJ_1.jno and
SPJ_3.pno = SPJ_2.pno and SPJ_3.sno = 'S1'
)
)
15.查询给所有工程都供应了零件的供应商号。(除法查询)
命令: select sno
from S
where not exists
(select *
from J
where not exists
(select *
from SPJ
where SPJ.sno = S.sno and SPJ.jno = J.jno))
16.查询使用了所有零件的工程号。
命令:select jno from J where not exists ( select * from P where not exists ( select * from SPJ where pno = P.pno and jno = J.jno ))
17.查询所有工程都使用了的零件号。
命令:select pno from P where not exists ( select * from J where not exists (select * from SPJ where jno = J.jno and pno = P.pno))
18.查询供应了所有零件的供应商号。
命令:select sno from S where not exists (select * from P where not exists ( select * from SPJ where pno = P.pno and sno = S.sno))
19.查询所有供应商都供应了的零件号。
命令:select pno from P where not exists ( select * from S where not exists (select * from SPJ where sno = S.sno and pno = P.pno))
20.查询使用了所有供应商的零件的工程。
命令:select * from J where not exists (select * from S where not exists (select * from SPJ where sno = S.sno and jno = J.jno))
21.把全部红色零件的颜色改成蓝色。
命令:update P set color = '蓝' where color = '红'
22.由S5供给J4的零件P6改为由S3供应。
命令:update SPJ set sno = 'S3' where sno = 'S5' and pno = 'P6' and jno = 'J4'
23.从供应商关系中删除S2的记录,并从供应情况关系中删除相应的记录;?
命令:delete from SPJ where sno = 'S2'
delete from S where sno = 'S2'
24.请将(S2,J6,P4,200)插入供应情况关系。
命令:insert into SPJ(sno,jno,pno,qty) values('S2','J6','P4',200)
25.请为三建工程项目建立一个供应情况的视图,包括SNO,PNO,QTY。针对该视图完成下列查询:
- 找出三建工程项目使用的各种零件代码及其数量;
- 找出供应商S1的供应情况。
建立视图:
create view IS_Sanjian(sno,pno,qty)
as
select sno,pno,qty
from SPJ
where jno in
(select jno
from J
where jname = '三建'
)
with check option
命令:(1):select pno as '零件号码',SUM(qty) as '数量' from IS_Sanjian group by pno
(2):select * from IS_Sanjian where sno = 'S1'
来源:CSDN
作者:namesocool
链接:https://blog.csdn.net/namesocool/article/details/103648503