数据库第三章课后题(2020.3.23作业)

1、有两个关系S(A,B,C,D)和T(C,D,E,F),写出与下列查询等价的SQL表达式:
(1) σ A = 10 ( S ) \sigma_{A = 10}(S) σA=10(S)

SELECT *
FROM S
WHERE A=10

(2) Π A , B ( S ) \Pi_{A,B}(S) ΠA,B(S)

SELECT DISTINCT A,B
FROM S

(3) S ⋈ T S \Join T ST

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}\Join T SS.C=T.CT

SELECT A,B,S.C,S.D,T.C,T.D,E,F
FROM S,T
WHERE S.C=T.C

(5) S ⋈ A < E T S \underset{A<E}\Join T SA<ET

SELECT A,B,S.C,S.D,T.C,T.D,E,F
FROM S,T
WHERE A<E

*(6) Π C , D ( S ) × T \Pi_{C,D}(S) \times T ΠC,D(S)×T

SELECT S1.C,S1.D,T.C,T.D,E,F 
FROM T,(SELECT DISTINCT C,D 
			   FROM S) 
			   AS S1

错误点:第六小题,我写的是

SELECT DISTINCT S.C,S.D,T.*
FROM S,T

这样一开始就把T里面的重复行去掉了,错误

2、用SQL语句建立第二章习题6中的4个表;针对建立的4个表用SQL完成第二章习题6中的查询。

S:

CREATE TABLE S(SNO CHAR(10), 
               SNAME CHAR(10), 
               STATUS SMALLINT,
        	   CITY CHAR(10), 
      		   ); 
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,'上海');

P:

CREATE TABLE P(PNO CHAR(10),
			   PNAME CHAR(10),
			   COLOR CHAR(10),
			   WEIGHT SMALLINT
			   );
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);

J:

CREATE TABLE J(JNO CHAR(10),
	           JNAME CHAR(10),
	           CITY CHAR(10)
	           );
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','半导体厂','南京');

SPJ:

CREATE TABLE SPJ(SNO CHAR(4),
	             PNO CHAR(4),
	             JNO CHAR(4),
	             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);

(1)求供应工程J1零件的供应商号码SNO;

SELECT SNO
FROM   SPJ
WHERE  JNO='J1';

(2)求供应工程J1零件P1的供应商号码SNO;

SELECT SNO
FROM   SPJ
WHERE  JNO='J1' AND PNO ='P1';

(3)求供应工程J1零件为红色的供应商号码 SNO;

SELECT SNO
FROM   SPJ,P
WHERE  P.PNO=SPJ.PNO AND JNO='J1' AND COLOR='红';​​​​​​​​​

(4)求没有使用天津供应商生产的红色零件的工程号JNO;

SELECT JNO
FROM   J
WHERE NOT EXISTS(
		SELECT *
		FROM SPJ,S,P
		WHERE S.CITY='天津' AND P.COLOR='红' AND SPJ.JNO=J.JNO AND SPJ.SNO=S.SNO AND SPJ.PNO=P.PNO​​​);​​​​​

(5)求至少用了供应商S1所供应的全部零件的工程号JNO。

SELECT DISTINCT JNO
FROM SPJ SPJX
WHERE NOT EXISTS 
	(SELECT *FROM SPJ SPJY 
	 WHERE SPJY.SNO='S1'AND NOT EXISTS(SELECT *
		  FROM SPJ SPJZ 
		  WHERE SPJZ.PNO=SPJY.PNO AND SPJZ.JNO=SPJX.JNO​​​));​​​

第五小题做了半天又去翻了一下以前的例题才写出来,有点费劲

3、针对习题4中的4个表使用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  JNO='J2' AND P.PNO=SPJ.PNO

(5)找出上海厂商供应的所有零件号码;

SELECT DISTINCT PNO
FROM   SPJ,S
WHERE  S.SNO=SPJ.SNO AND CITY='上海'

SELECT DISTINCT PNO
FROM   SPJ
WHERE  SNO IN(SELECT SNO
			  FROM   S
		      WHERE  CITY='上海'
			  );

(6)找出使用上海产的零件的工程名称;

SELECT DISTINCT JNAME 
​FROM  SPJ,S,J
WHERE S.SNO=SPJ.SNO AND S.CITY='上海' AND J.JNO=SPJ.JNO

SELECT JNAME 
FROM   J 
WHERE  JNO IN(SELECT JNO
		      FROM   SPJ, S 
		      WHERE  SPJ. SNO=S.SNO AND S.CITY='上海'); 

(7)找出没有使用天津产的零件的工程号码;

SELECT JNO
FROM   J
WHERE NOT EXISTS(SELECT *
                 FROM  SPJ,S
				 WHERE SPJ.JNO=J.JNO AND SPJ.SNO=S.SNO AND S.CITY='天津')

SELECT JNO 
FROM   J 
WHERE NOT EXISTS (SELECT *
                  FROM  SPJ
				  WHERE SPJ.JNO=J.JNO AND SNO IN(SELECT SNO
				                                 FROM   S
												 WHERE  CITY='天津'))

(8)把全部红色零件的颜色改成蓝色;

UPDATE P
SET    COLOR='蓝'
WHERE  COLOR='红'

(9)由S5供给J4的零件P6改为由S3供应,请作必要的修改;

UPDATE SPJ
SET    SNO='S3'
WHERE  SNO='S5' AND JNO='J4' AND PNO='P6';

(10)从供应商关系中删除S2的记录,并从供应情况关系中删除相应的记录;

DELETE
FROM  SPJ
WHERE SNO='S2';
DELETE
FROM  S
WHERE SNO='S2'

(11)请将(S2,J6,P4,200)插入供应情况关系;

INSERT INTO SPJ VALUES('S2','P4','J6',200);

INSERT INTO SPJ(SNO,PNO,JNO,QTY) VALUES('S2','P4','J6',200);

4、请为三建工程项目建立一个供应情况的视图,包括供应商代码(SNO)、零件代码(PNO)、供应商数量(QTY)。针对该视图完成下列查询:

CREATE VIEW VIEW_SPJ
AS
SELECT SNO,PNO,QTY
FROM   SPJ
WHERE  JNO=(SELECT JNO
            FROM   J
			WHERE  JNAME='三建')

(1)找出三建工程项目使用的各种零件代码及其数量;

SELECT PNO,QTY
FROM   VIEW_SPJ

(2)找出供应商S1的供应情况;

SELECT *
FROM SPJVIEW
WHERE SNO='S1'

时间:做题-对答案-看错题-完成博客,两个小时

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值