有两个关系S(A,B,C,D)和T(C,D,E,F),写出与下列查询等价的SQL表达式:
(1) ∂A=10(S)
SELECT *
FROM S
WHERE A=10;
(2) ∏A,B(S)
SELECT A,B
FROM S;
(3) S▷◁T
SELECT A,B,S.C,S.D,E,F
FROM S,T
WHERE S.C=T.C AND
S.D=T.D;
(4) S▷◁T(S.C=T.C)
SELECT *
FROM S,T
WHERE S.C=T.C;
(5)S▷◁T(A (6)∏C,D(S)xT 用SQL语句建立第二章习题6中的四个表;针对建立的4个表用SQL完成第二章习题6中的查询。 P表: J表: SPJ表: (2) 求供应工程J1零件P1的供应商号码SNO; 或 (3) 求供应工程J1零件为红色的供应商号码SNO; 或 (4) 求没有使用天津供应商生产的红色零件的工程号JNO; 或 (5) 求至少用了供应商S1所供应的全部零件的工程号JNO; 针对习题6中的4个表使用SQL完成以下操作 (2)找出所有零件的名称、颜色和重量 (3)找出使用供应商S1所供应零件的工程号码: (4)找出工程项目J2使用的各种零件的名称及其数量 (5)找出上海厂商供应的所有零件号码 或者: (6)找出使用上海产的零件的工程名称 或者 查询结果: 或者: (8)把全部红色零件的颜色改成蓝色 (9)由S5供给J4的零件P6改为由S3供应,请作必要的修改 (10)从供应商关系中删除S2的记录,并从供应情况关系中删除相应的记录: (11)请将(S2,J6,P4,200) 插入供应情况关系 请为三建工程项目建立一个供应情况的视图,包括供应商代码(SNO)、 零件代码(PNO)、供应数量(QTY) 。针对该视图完成下列查询: (1)找出三建工程项目使用的各种零件代码及其数量 (2)找出供应商SI的供应情况SELECT *
FROM S,T
WHERE S.A<T.E;
SELECT S.C,S.D
FROM S,T
WHERE S.C=T.C AND
S.D=T.D;
二、 习题4
建立四张表
S表:CREATE TABLE S
(SNO CHAR(4) PRIMARY KEY,
SNAME CHAR(20),
STATU INT,
CITY CHAR(10));
CREATE TABLE P
(PNO CHAR(4) PRIMARY KEY,
PNAME CHAR(20),
COLOR CHAR(4),
WEIGHT INT);
CREATE TABLE J
(JNO CHAR(4) PRIMARY KEY,
JNAME CHAR(20),
CITY CHAR(10));
CREATE TABLE SPJ
(SNO CHAR(4),
PNO CHAR(4),
JNO CHAR(4),
CITY CHAR(10),
PRIMARY KEY(SNO,PNO,JNO),
FOREIGN KEY(SNO) REFERENCES S(SNO),
FOREIGN KEY(PNO) REFERENCES P(PNO),
FOREIGN KEY(JNO) REFERENCES J(JNO));
SELECT SNO
FROM SPJ
WHERE JNO='J1';
代码:SELECT SNO
FROM SPJ
WHERE JNO='J1' AND PNO='P1';
SELECT SNO
FROM SPJ
WHERE JNO='J1'
INTERSECT
SELECT SNO
FROM SPJ
WHERE PNO='P1';
代码:SELECT SNO
FROM SPJ,P
WHERE JNO='J1' AND
SPJ.PNO=P.PNO AND
P.COLOR='红';
SELECT SNO
FROM SPJ
WHERE JNO='J1' AND
PNO IN
(SELECT PNO
FROM P
WHERE COLOR='红');
代码:SELECT SPJ.SNO
FROM SPJ,P,S
WHERE JNO='J1' AND
SPJ.PNO=P.PNO AND
P.COLOR='红' AND
SPJ.SNO=S.SNO AND
S.CITY!='天津';
SELECT SNO
FROM SPJ
WHERE JNO='J1' AND
PNO IN
(SELECT PNO
FROM P
WHERE COLOR='红') AND
SNO NOT IN
(SELECT SNO
FROM S
WHERE CITY='天津');
代码:SELECT DISTINCT JNO
FROM SPJ
WHERE SNO='S1';
三、 习题5
(1)找出所有供应商的姓名和所在城市
代码:SELECT SNAME,CITY
FROM S;
代码:SELECT PNAME,COLOR,WEIGHT
FROM P;
代码:SELECT DISTINCT PNO
FROM SPJ
WHERE SNO='S1';
代码:SELECT PNAME,QTY
FROM SPJ,P
WHERE SPJ.PNO=P.PNO AND
SPJ.JNO='J2';
代码:SELECT DISTINCT PNO
FROM SPJ,S
WHERE SPJ.SNO=S.SNO AND
S.CITY='上海';
SELECT DISTINCT PNO
FROM SPJ
WHERE SNO IN
(SELECT SNO
FROM S
WHERE CITY='上海');
代码:SELECT JNAME
FROM J
WHERE JNO IN
(SELECT JNO
FROM SPJ
WHERE SNO IN
(SELECT SNO
FROM S
WHERE CITY='上海'));
SELECT DISTINCT JNAME
FROM J,S,SPJ
WHERE SPJ.SNO=S.SNO AND
SPJ.JNO=J.JNO AND
S.CITY='上海' AND
J.JNO=SPJ.JNO;
(7)找出没有使用天津产的零件的工程号码
代码:SELECT DISTINCT SPJ.JNO
FROM S,SPJ
WHERE S.SNO=SPJ.SNO AND
S.CITY !='天津';
SELECT DISTINCT JNO
FROM SPJ
WHERE SNO NOT IN
(SELECT SNO
FROM S
WHERE CITY='天津');
代码:UPDATE P
SET COLOR='蓝'
WHERE COLOR='红';
代码:UPDATE SPJ
SET SNO='S3'
WHERE SNO='S5' AND
PNO='P6' AND
JNO='J4';
代码:DELETE
FROM SPJ
WHERE SNO='S2';
DELETE
FROM S
WHERE SNO='S2';--注意删除先后顺序
代码:INSERT
INTO S
VALUES('S2','盛锡',10,'北京');
INSERT
INTO SPJ
VALUES('S2','P4','J6',200);
INSERT
INTO S
VALUES('S2','盛锡',10,'北京');
INSERT
INTO SPJ(SNO,JNO,PNO,QTY)
VALUES('S2','J6','P4',200);
四、 习题9
建立视图:
代码:CREATE VIEW T_SPJ(SNO,PNO,QTY)
AS
SELECT SNO,PNO,QTY
FROM SPJ
WHERE JNO IN
(SELECT JNO
FROM J
WHERE JNAME='三建')
WITH CHECK OPTION;
代码:SELECT PNO,QTY
FROM T_SPJ;
SELECT PNO,SUM(QTY) QTY
FROM T_SPJ
GROUP BY PNO;
代码:SELECT *
FROM T_SPJ
WHERE SNO='S1';