πSno(σSno=‘J1’(SPJ))
2)求供应工程J1零件P1的供应商号码SNO:
πSno(σSno=‘J1’∧Pno=‘P1‘(SPJ))
3)求供应工程J1零件为红色的供应商号码SNO:
πSno(σPno=‘P1‘(σCOLOR=’红‘ (P)∞SPJ))
4)求没有使用天津供应商生产的红色零件的工程号JNO:
πJno(SPJ)- πJNO(σcity=‘天津’∧Color=‘红‘ (S∞SPJ∞P)
5)求至少用了供应商S1所供应的全部零件的工程号JNO:
πJno,Pno(SPJ)÷ πPno(σSno=‘S1‘ (SPJ))
sql语句:
( l )求供应工程 Jl 零件的供应商号码 SNO
SELECT SNO FROM SPJ WHERE JNO=’J1’
( 2 )求供应工程 Jl 零件 Pl 的供应商号码 SNO
SELECT SNO FROM SPJ WHERE JNO='J1' AND PNO='P1'
( 3 )求供应工程 Jl 零件为红色的供应商号码 SNO
SELECT SNO FROM SPJ,P WHEREJNO='J1' AND SPJ.PNO=P.PNO AND COLOR='红'
( 4 )求没有使用天津供应商生产的红色零件的工程号 JNO
SELECT JNO FROM SPJ WHERE JNO NOT IN(SELE JNO FROM SPJ,P,S WHERE S.CITY='天津' AND COLOR='红' ANDS.SNO=SPJ.SNO AND P.PNO=SPJ.PNO)。
( 5 )求至少用了供应商 Sl 所供应的全部零件的工程号 JNO
由于VFP不允许子查询嵌套太深,将查询分为两步
A、查询S1供应商供应的零件号
SELECT PNO FROM SPJ WHERE SNO='S1'结果是(P1,P2)
B、查询哪一个工程既使用P1零件又使用P2零件。
SELECT FROM SPJ WHEREPNO='P1'
AND JNO IN (SELECT JNO FROM SPJ WHERE PNO='P2')
CREATE TABLE S (SNO char(2) primary key,
SNAME varchar(20),
STATUS char(4),
CITY char(10)
)
create table P (
PNO char(2) primary key,
PNAME varchar(20),
COLOR char(4),
WEIGHT int
)
create table J (
JNO char(2) primary key,
JNAME varchar(20),
CITY char(10)
)
create table SPJ (
SNO char(2),
PNO char(2),
JNO char(2),
QTY int,
primary key(SNO,PNO,JNO),
foreign key(SNO) references S(SNO) on delete cascade,
foreign key(PNO) references P(PNO) on delete cascade,
foreign key(JNO) references J(JNO) on delete cascade
)
顺便连数据也一起给你好了
insert into S values('S1','供应商一','良好','郑州')insert into S values('S2','供应商二','良好','天津')
insert into S values('S3','供应商三','良好','上海')
insert into S values('S4','供应商四','优秀','郑州')
insert into P values('P1','管材','红色',20)
insert into P values('P2','管线','绿色',30)
insert into P values('P3','螺帽','红色',10)
insert into P values('P4','螺帽','黄色',10)
insert into J values('J1','一建工程','郑州')
insert into J values('J2','二建工程','郑州')
insert into J values('J3','三建工程','天津')
insert into J values('J4','四建工程','天津')
insert into J values('J5','五建工程','上海')
insert into J values('J6','六建工程','北京')
insert into SPJ values('S1','P1','J1',100)
insert into SPJ values('S1','P2','J1',100)
insert into SPJ values('S2','P1','J1',100)
insert into SPJ values('S2','P2','J1',100)
insert into SPJ values('S1','P1','J3',100)
insert into SPJ values('S1','P2','J3',100)
insert into SPJ values('S2','P1','J3',100)
insert into SPJ values('S2','P2','J3',100)
insert into SPJ values('S3','P1','J2',100)
insert into SPJ values('S3','P2','J2',100)
insert into SPJ values('S4','P3','J2',100)
第一题: select jname from Jinner join SPJ on J.Jno=SPJ.Jno
inner join S on S.Sno=SPJ.Sno
inner join P on P.Pno=SPJ.Pno
where S.Sno= 's1' 第二题:没有必要用course表的自连接,像下面这样即可 selecttname from teacher where tno in (select tno from course group by tno having count(cno)>5) 第三题: 答案不对,那么写的话,连部门经理本身也会查出来,题目要求只查员工。应该像下面这样写:select a.ename from emp a,dept bwhere a.did = b.didand a.salary >10000and a.age = (select c.age from emp c where c.eid = b.mgr_id)and a.eid <>b.mgr_id
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)