(1) σ A = 10 ( S ) \sigma_{A=10}(S) σA=10(S)
等价于从S表中选出A=10的元组
Select * From S
Where A = 10
(2) Π A , B ( S ) \Pi_{A,B}(S) ΠA,B(S)
投影, 从S中选出A、B列
Select A, B From S
(3) S ⋈ T S \bowtie T S⋈T
S与T的自然连接,S与T的共同列是C与D,所以判断C,D是否相等即可
Select A, B, S.C, S.D, E, F
From S, T
Where S.C = T.C And S.D = T.D
(4) S ⋈ S . C = T . C T S {\underset{S.C=T.C} \bowtie} T SS.C=T.C⋈T
S与T的等值连接
Select *
From S, T
Where S.C = T.C
(5) S ⋈ A = E T S {\underset{A=E} \bowtie} T SA=E⋈T
S与T的非等值连接
Select *
From S, T
Where S.A=T.E
(6) Π C , D ( S ) × T \Pi_{C,D}(S) \times T ΠC,D(S)×T
从S中选出C、D列与T做笛卡尔积。
Select S.C,S.D,T.C,T.D,T.E,T.F
From S,T
建表代码如下:
CREATE TABLE S(SNO CHAR(5), SNAME CHAR(5), STATUS INT, CITY CHAR(5));
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','上海');
CREATE TABLE P( PNO CHAR(5),PNAME CHAR(5), COLOR CHAR(5), WEIGHT INT);
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);
CREATE TABLE J( JNO CHAR(5),JNAME CHAR(10),CITY CHAR(5));
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','半导体厂','南京');
CREATE TABLE SPJ( SNO CHAR(5), PNO CHAR(5), JNO CHAR(5),QTY SMALLINT);
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 SNO From SPJ Where Jno = 'J1'
Select SNO From SPJ Where Pno = 'P1'
SELECT SNO
FROM SPJ,P
WHERE P.PNO=SPJ.PNO AND JNO='J1' AND COLOR='红';
先求出天津商生产的红色零件的Jno,然后在SPJ表中查询即可
Select Jno From Spj Where Jno Not in(
Select Jno From SPJ,P,S
WHERE S.CITY='天津' AND COLOR='红' AND S.SNO=SPJ.SNO AND P.PNO=SPJ.PNO)
)
这意味着工程y不存在一个零件x,S1拥有零件x而y没有拥有
Select Distinct Jno From Spj SpjX Where Not Exists(
Select Pno From SPj SpjY Where Sno='S1' And Not Exists(
Select Pno From SPj SpjZ Where SpjX.Jno=SpjZ.Jno And SpjY.Pno = SpjZ.Pno
)
)
(1)找出所有供应商的姓名和所在城市;
Select Sname,City
From S;
(2)找出所有零件的名称、颜色、重量;
select Pno,Color,Weight from P;
(3)找出使用供应商S1所供应零件的工程号码;
select jno
from SPJ
where sno='S1';
(4)找出工程项目J2使用的各种零件的名称及其数量;
select pname,qty
from SPJ,P
where jno='J2' and P.pno=SPJ.pno;
(5)找出上海厂商供应的所有零件号码;
select pno
from SPJ,S
where city='上海'and S.sno=SPJ.sno;
select jname
from SPJ,S,J
where S.city='上海'and S.sno=SPJ.sno and J.jno=SPJ.jno;
select jno
from SPJ
where jno not in(
select jno
from SPJ,S
where S.sno=SPJ.sno and S.city='天津');
update P set color='红'
where color='蓝';
Select Color From P
(9)由S5供给4的零件P6改为由S3供应,请作必要的修改;Viorel2阿PW(10)从供应商关系中删除S2的记录,并从供应情况关系中删除相应的记录;
update SPJ set sno='S3'
where sno='S5' and jno='J4' and pno='P6';
Select * From Spj
(10)从供应商关系中删除S2的记录,并从供应情况关系中删除相应的记录;
delete from S where sno='S2';
delete from SPJ where sno='S2';
(11)请将(S2,J6,P4,200)插入供应情况关系。
insert into SPJ values('S2','P4','J6',200);
视图创建如下:
create view v_SPJ as
select sno,pno,qty
from SPJ
where jno=(select jno
from J
where jname='三建');
select pno,qty from v_SPJ;
(2)找出供应商S1的供应情况。
select * from v_SPJ where sno='S1';
作业好多, 我人晕了