请根据以上内容完成下面题目,写出相应的SQL语句.
2、创建表
create table S ( Sno char(2) primary key, --假设供应商代码的长度为定长2 Sname varchar2(9) ,--假设供应商的名称最多为三个汉字 Status varchar2(3) not null,--假设供应商供应状态为最多有三个数字 City varchar2(15) not null --假设城市最长为5个字符的汉字 );
drop table S;
select * from S;
create table P ( Pno char(2), --假设Pno为2位定长的字符 Pname varchar2(9) not null, --假设Pname最多有三个汉字 Color char(3) check (Color in('红','绿','蓝')), --颜色只能是一个汉字,并且这个字只能为红绿蓝三种中的一种 Weight smallint not null,--重量是个整数,并且不能为空 constraint PK_P primary key (Pno) );
drop table P;
select * from P;
create table J ( Jno char(2) , --假设工程项目代码是由定长的两位字符组成 Jname varchar2(12), --假设工程名称最多为长度为4的汉字组成 City varchar2(15), --假设工程项目所在的城市的名称最多含有5个汉字 constraint PK_J primary key (Jno) );
drop table J;
select * from J;
create table SPJ ( Sno char(2), Pno char(2), Jno char(2), Qty int constraint C1 check(Qty between 0 and 1000), constraint SPJKey primary key(Sno,Pno,Jno), constraint FK_Sno foreign key(Sno) references S(Sno) on delete cascade, constraint FK_Pno foreign key(Pno) references P(Pno) on delete cascade, constraint FK_Jno foreign key(Jno) references J(Jno) on delete cascade );
drop table SPJ;
select * from SPJ;
select userenv('language') from dual;
3、插入数据
insert into S(Sno,Sname,Status,City) values('S1','精益','20','天津');
insert into S(Sno,Sname,Status,City) values('S2','盛锡','10','北京');
insert into S(Sno,Sname,Status,City) values('S3','东方红','30','北京');
insert into S(Sno,Sname,Status,City) values('S4','丰泰盛','20','天津');
insert into S(Sno,Sname,Status,City) values('S5','为民','30','上海');
select * from S;
insert into P(Pno,Pname,Color,Weight) values('P1','螺母','红',12);
insert into P(Pno,Pname,Color,Weight) values('P2','螺栓','绿',17);
insert into P(Pno,Pname,Color,Weight) values('P3','螺丝刀','蓝',14);
insert into P(Pno,Pname,Color,Weight) values('P4','螺丝刀','红',14);
insert into P(Pno,Pname,Color,Weight) values('P5','凸轮','蓝',40);
insert into P(Pno,Pname,Color,Weight) values('P6','齿轮','红',30);
select * from P;
insert into J(Jno,Jname,City) values('J1','三建','北京');
insert into J(Jno,Jname,City) values('J2','一汽','长春');
insert into J(Jno,Jname,City) values('J3','弹簧厂','天津');
insert into J(Jno,Jname,City) values('J4','造船厂','天津');
insert into J(Jno,Jname,City) values('J5','机车厂','唐山');
insert into J(Jno,Jname,City) values('J6','无线电厂','常州');
insert into J(Jno,Jname,City) values('J7','半导体厂','南京');
select * from J;
insert into SPJ(Sno,Pno,Jno,Qty) values('S1','P1','J1',200);
insert into SPJ(Sno,Pno,Jno,Qty) values('S1','P1','J3',100);
insert into SPJ(Sno,Pno,Jno,Qty) values('S1','P1','J4',700);
insert into SPJ(Sno,Pno,Jno,Qty) values('S1','P2','J2',100);
insert into SPJ(Sno,Pno,Jno,Qty) values('S2','P3','J1',400);
insert into SPJ(Sno,Pno,Jno,Qty) values('S2','P3','J2',200);
insert into SPJ(Sno,Pno,Jno,Qty) values('S2','P3','J4',500);
insert into SPJ(Sno,Pno,Jno,Qty) values('S2','P3','J5',400);
insert into SPJ(Sno,Pno,Jno,Qty) values('S2','P5','J1',400);
insert into SPJ(Sno,Pno,Jno,Qty) values('S2','P5','J2',100);
insert into SPJ(Sno,Pno,Jno,Qty) values('S3','P1','J1',200);
insert into SPJ(Sno,Pno,Jno,Qty) values('S3','P3','J1',200);
insert into SPJ(Sno,Pno,Jno,Qty) values('S4','P5','J1',100);
insert into SPJ(Sno,Pno,Jno,Qty) values('S4','P6','J3',300);
insert into SPJ(Sno,Pno,Jno,Qty) values('S4','P6','J4',200);
insert into SPJ(Sno,Pno,Jno,Qty) values('S5','P2','J4',100);
insert into SPJ(Sno,Pno,Jno,Qty) values('S5','P3','J1',200);
insert into SPJ(Sno,Pno,Jno,Qty) values('S5','P6','J2',200);
insert into SPJ(Sno,Pno,Jno,Qty) values('S5','P6','J4',500);
select * from SPJ;
select * from S;
select * from P;
select * from J;
select * from SPJ;
4、请用SQL语句完成下面题目的查询。
(1)求供应工程J1零件的供应商号码SNO:
select Sno from SPJ where Jno='J1';
select distinct Sno from SPJ where Jno='J1';
(2)求供应工程J1零件P1的供应商号码SNO:
select Sno from SPJ where Jno='J1' and Pno='P1';
(3)求供应工程J1零件为红色的供应商号码SNO:
select distinct Pno from SPJ where Jno='J1';
select Pno from P where Color='红' and Pno in (select distinct Pno from SPJ where Jno='J1');
select distinct Sno from SPJ where Pno in (select Pno from P where Color='红' and Pno in (select distinct Pno from SPJ where Jno='J1' ) ) --显然这种方式得到的结果也是正确的: --因为目标列是要得到SPJ.Sno --那么久说明了有时候的多表查询是不能够使用子查询进行替换的, --表面上看上去是相同的,但是可能不是等价的替换 --方式一:使用等值连接实现该查询要求 --这种使用广义的笛卡尔积的方式我们是需要极力避免的方式. --分析关键过程: --1.涉及到的数据表供应商供应情况表SPJ和零件表P --2.将供应商供应情况表SPJ和零件表P表进行内连接 --3.确定连接时的关联字段SPJ表和P表的关联字段为Pno select distinct SPJ.Sno from SPJ,P where SPJ.Jno='J1' and SPJ.Pno=P.Pno and Color='红';
select Sno from P inner join SPJ on SPJ.Pno=P.Pno and Jno='J1' and Color='红';
select distinct Sno from P inner join SPJ on SPJ.Pno=P.Pno and Jno='J1' and Color='红';
select Sno from P natural join SPJ where Jno='J1' and Color='红';
select distinct Sno from P natural join SPJ where Jno='J1' and Color='红';
select Pno from P where Color='红';
select Sno from SPJ where Jno='J1' and Pno in (select Pno from P where Color='红' );
select distinct Sno from SPJ where Jno = 'J1' and exists ( select Pno from P where color = '红' and P.Pno = SPJ.Pno );
(4)求没有使用天津供应商生产的红色零件的工程号JNO:
select distinct Jno from S,P,SPJ where S.Sno=SPJ.Sno and P.Pno=SPJ.Pno and City='天津' and Color='红';
select Jno from J where Jno not in (select Jno from S,P,SPJ where S.Sno=SPJ.Sno and P.Pno=SPJ.Pno and City='天津' and Color='红' );
select Jno from J where Jno not in (select distinct Jno from S,P,SPJ where S.Sno=SPJ.Sno and P.Pno=SPJ.Pno and City='天津' and Color='红' );
select * from S inner join SPJ on S.Sno=SPJ.Sno, SPJ inner join P on P.Pno=SPJ.Pno --并且这个时候我按照这个查询语句进行查询的时候会提示 --:ORA-00918: 未明确定义列 --这里的原因是: --1.将S表和SPJ表进行内连接,-->得到19行记录,这里面包含一个SPJ.Jno --2.将P表和SPJ表进行内连接,-->得到19行记录,这里面包含一个SPJ.Jno --3.将上面的两个内连接的做广义的笛卡尔积,得到19*19=361行记录 --在这种情况下使用select Jno显然是不行的,最后得到的表中有两个Jno --并且这两个Jno的所属都是SPJ表,这时显然是不能确定需要查询出那个字段 --我开始没理解的时候还是用了select SPJ.Jno,发现这样也是不行的 --原因是最后得到的表中的Jno的所属都是SPJ,这时候显然是不能够确定 --需要将那个字段查询出来的. --虽然这种方式是不可行的,但是我觉得还是应该将理由想清楚. select Jno from J where Jno not in (select * from S inner join SPJ on S.Sno=SPJ.Sno, SPJ inner join P on P.Pno=SPJ.Pno where S.City='天津' and P.Color='红' );
select Jno from J where Jno not in (select distinct Jno from S inner join SPJ inner join P on SPJ.Pno=P.Pno and S.Sno=SPJ.Sno where City='天津' and Color='红' );
select distinct Jno from S inner join SPJ on S.Sno=SPJ.Sno inner join P on SPJ.Pno=P.Pno where City='天津' and Color='红';
select Jno from J where Jno not in (select distinct Jno from S inner join SPJ on S.Sno=SPJ.Sno inner join P on SPJ.Pno=P.Pno where City='天津' and Color='红' );
select distinct Jno from S natural join P natural join SPJ where City='天津' and Color='红';
select Jno from J where Jno not in (select Jno from S natural join P natural join SPJ where City='天津' and Color='红');
select * from S natural join P;
select * from S natural join J;
select distinct Jno from J where not exists ( select Jno from SPJ natural join P natural join P where City = '天津' and Color = '红' and SPJ.Jno=J.Jno);
select distinct Jno from J where Jno not in ( select Jno from SPJ where Sno in (select Sno from S where City = '天津' ) and Pno in (select Pno from P where Color = '红' ) );
(5)求至少用了供应商S1所供应的全部零件的工程号JNO:
select distinct jno from spj spjx where not exists (select pno from spj where sno = 'S1' and pno not in ( select pno from spj where jno = spjx.jno ) );
select distinct jno from spj spjx where not exists (select * from spj spjy where sno = 'S1' and not exists (select * from spj where jno = spjx.jno and pno = spjy.pno ) );
5、请用SQL语句完成下面题目的查询。
(1)找出所有供应商的姓名和所在城市。
select Sname,City from S;
(2)找出所有零件的名称、颜色、重量。
select Pname,Color,Weight from P;
(3)找出使用供应商S1所供应零件的工程号码。
select Jno from SPJ where Sno='S1';
(4)找出工程项目J2使用的各种零件的名称及其数量。
select Pname,Qty from P,SPJ where P.Pno=SPJ.Pno and Jno='J2';
select Pname,Qty from P inner join SPJ on P.Pno=SPJ.Pno where Jno='J2';
select Pname,Qty from P join SPJ on P.Pno=SPJ.Pno where Jno='J2';
select Pname,Qty from P natural join SPJ where Jno='J2';
(5)找出上海厂商供应的所有零件号码。
select distinct Pno from S,SPJ where S.Sno=SPJ.Sno and S.City='上海';
select distinct Pno from S inner join SPJ on S.Sno=SPJ.Sno where S.City='上海';
select distinct Pno from S natural join SPJ where S.City='上海';
select Sno from S where S.City='上海';
select Pno from SPJ where Sno in (select Sno from S where S.City='上海' );
select distinct Pno from SPJ where exists (select Sno from S where City = '上海'and S.Sno = SPJ.Sno );
(6)找出使用上海产的零件的工程名称。
select distinct Jname from S,J,SPJ where S.Sno=SPJ.Sno and J.Jno=SPJ.Jno and S.City='上海';
select distinct Jname from S inner join SPJ on S.Sno=SPJ.Sno inner join J on SPJ.Jno=J.Jno where S.City='上海';
select distinct Jname from S join SPJ on S.Sno=SPJ.Sno join J on SPJ.Jno=J.Jno where S.City='上海';
select distinct Sno from S where S.City='上海';
select distinct Jno from SPJ where Sno in (select Sno from S where S.City='上海');
select Jname from J where Jno in (select distinct Jno from SPJ where Sno in (select Sno from S where S.City='上海' ) );
select distinct jname from j where exists (select * from spj natural join s where city = '上海' and jno = j.jno );
(7)找出没有使用天津产的零件的工程号码。
select distinct Jno from S,SPJ where S.Sno=SPJ.Sno and City='天津';
select Jno from J where Jno not in (select Jno from S,SPJ where S.Sno=SPJ.Sno and City='天津' );
select distinct Jno from S inner join SPJ on S.Sno=SPJ.Sno where City='天津';
select Jno from J where Jno not in (select distinct Jno from S inner join SPJ on S.Sno=SPJ.Sno where City='天津' );
select Jno from S natural join SPJ where City='天津';
select Jno from J where Jno not in (select Jno from S natural join SPJ where City='天津' );
select distinct Sno from S where City='天津';
select distinct Jno from SPJ where Sno in (select distinct Sno from S where City='天津' );
select Jno from J where Jno not in (select distinct Jno from SPJ where Sno in (select distinct Sno from S where City='天津' ) );
select distinct Jno from SPJ SPJX where not exists ( select Jno from SPJ natural join s where City = '天津' and Jno = SPJX.Jno );
(8)把全部红色零件的颜色改成蓝色。
update P set Color='蓝' where Color='红';
select * from P;
(9)由S5供给J4的零件P6改为由S3供应。
update SPJ set Sno='S3' where Sno='S5' and Pno='P6' and Jno='J4';
select * from SPJ;
(10)从供应商关系中删除供应商号是S2的记录,并从供应情况关系中删除相应的记录。
delete from S where Sno='S2';
insert into S(Sno,Sname,Status,City) values('S2','盛锡','10','北京');
delete from SPJ where Sno='S2';
select * from S;
select * from P;
select * from J;
select * from SPJ;
(11)请将(S2,J6,P4,200)插入供应情况关系。
insert into SPJ values('S2','P4','J6',200);
6、请为三建工程项目建立一个供应情况的视图,包括供应商代码(SNO)、零件代码(PNO)、供应数量(QTY)。
create view S_P_J(Sno,Pno,Qty) as select Sno,Pno,Qty from SPJ;
select * from S_P_J;
针对该视图完成下列查询:
(1)找出三建工程项目使用的各种零件代码及其数量
create view Project_Infor as select distinct Pno,SPJ.Qty from J,SPJ where J.Jno=SPJ.Jno and Jname='三建';
create view Project_Infor as select distinct SPJ.Pno,SPJ.Qty from J inner join SPJ on J.Jno=SPJ.Jno where Jname='三建';
create view Project_Infor as select distinct Pno,Qty from J natural join SPJ where Jname='三建';
create view Project_Infor as select distinct Pno,Qty from SPJ where Jno in (select Jno from J where Jname='三建' );
select Jno from J where Jname='三建';
select distinct Pno,Qty from SPJ where Jno in (select Jno from J where Jname='三建' );
select * from Project_Infor;
drop view Project_Infor;
(2)找出供应商S1的供应情况
create view S1_Infor as select * from SPJ where Sno='S1';
select * from S1_Infor;
drop view S1_Infor;