3.有两个关系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 S⋈T
自然连接,等值连接的基础上再去掉重复元组和重复列。
SELECT DISTINCT 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.C⋈T
SELECT *
FROM S,T
WHERE S.C=T.C;
(5) S ⋈ A < E T S \underset{A
SELECT *
FROM S,T
WHERE A<E;
(6) Π C , D ( S ) × T \Pi_{C,D}(S) \times T ΠC,D(S)×T
SELECT S.C,S.D,T.*
FROM S,T;
表示笛卡尔积,就去掉WHERE条件语句。
4.用SQL语句建立2.6中的四个表,针对建立的4个表,完成2.6中的查询。
S(SNO,SNAME,STATUS,CITY)
P(PNO,PNAME,COLOR,WEIGHT)
J(JNO,JNAME,CITY)
SPJ(SNO,PNO,JNO,QTY)
表SPJ中主码为SNO,PNO,JNO,这三者又分别为外码,参照于S,P,J表。
建表:
CREATE TABLE S
(SNO VARCHAR(5) PRIMARY KEY,
SNAME VARCHAR(10) UNIQUE,
STATUS INT NOT NULL,
CITY VARCHAR(10) NOT NULL);
CREATE TABLE P
(PNO VARCHAR(5) PRIMARY KEY,
PNAME VARCHAR(10) NOT NULL,
COLOR VARCHAR(5) NOT NULL,
WEIGHT INT NOT NULL);
CREATE TABLE J
(JNO VARCHAR(5) PRIMARY KEY,
JNAME VARCHAR(10) UNIQUE,
CITY VARCHAR(10) NOT NULL);
CREATE TABLE SPJ
(SNO VARCHAR(5) NOT NULL,
PNO VARCHAR(5) NOT NULL,
JNO VARCHAR(5) NOT NULL,
QTY INT NOT NULL
PRIMARY KEY(SNO,PNO,JNO),
FOREIGN KEY(SNO) REFERENCES S(SNO),
FOREIGN KEY(PNO) REFERENCES P(PNO),
FOREIGN KEY(JNO) REFERENCES J(JNO)
);
插入数据:
INSERT INTO S VALUES('S1','精益',20,'天津');
INSERT INTO S VALUES('S2','盛锡',10,'北京');
INSERT INTO S VALUES('S3','东方红',30,'北京');
INSERT INTO S VALUES('S4','丰泰盛',20,'天津');
INSERT INTO S VALUES('S5','为民',30,'上海');
INSERT INTO P VALUES('P1','螺母','红',12);
INSERT INTO P VALUES('P2','螺栓','绿',17);
INSERT INTO P VALUES('P3','螺丝刀','蓝',14);
INSERT INTO P VALUES('P4','螺丝刀','红',14);
INSERT INTO P VALUES('P5','凸轮','蓝',40);
INSERT INTO P VALUES('P6','齿轮','红',30);
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 J VALUES('J7','半导体厂','南京');
INSERT INTO SPJ VALUES('S1','P1','J1',200);
INSERT INTO SPJ VALUES('S1','P1','J3',100);
INSERT INTO SPJ VALUES('S1','P1','J4',700);
INSERT INTO SPJ VALUES('S1','P2','J2',100);
INSERT INTO SPJ VALUES('S2','P3','J1',400);
INSERT INTO SPJ VALUES('S2','P3','J2',200);
INSERT INTO SPJ VALUES('S2','P3','J4',500);
INSERT INTO SPJ VALUES('S2','P3','J5',400);
INSERT INTO SPJ VALUES('S2','P5','J1',400);
INSERT INTO SPJ VALUES('S2','P5','J2',100);
INSERT INTO SPJ VALUES('S3','P1','J1',200);
INSERT INTO SPJ VALUES('S3','P3','J1',200);
INSERT INTO SPJ VALUES('S4','P5','J1',100);
INSERT INTO SPJ VALUES('S4','P6','J3',300);
INSERT INTO SPJ VALUES('S4','P6','J4',200);
INSERT INTO SPJ VALUES('S5','P2','J4',100);
INSERT INTO SPJ VALUES('S5','P3','J1',200);
INSERT INTO SPJ VALUES('S5','P6','J2',200);
INSERT INTO SPJ VALUES('S5','P6','J4',500);
SELECT SNO
FROM SPJ
WHERE JNO='J1';
SELECT SNO
FROM SPJ
WHERE JNO='J1' AND PNO='P1';
SELECT SNO
FROM SPJ,P
WHERE SPJ.PNO=P.PNO AND P.COLOR='红' AND JNO='J1';
SELECT DISTINCT JNO
FROM SPJ
WHERE JNO NOT IN
(SELECT JNO
FROM SPJ,S,P
WHERE S.SNO=SPJ.SNO
AND P.PNO=SPJ.PNO
AND CITY='天津'
AND COLOR='红');
子查询结果如图:
该题结果为:
!(5)求至少用了供应商S1所供应的全部零件的工程号JNO。
等价转换:没有一个S1供应的零件是该工程没有用到的。
SELECT DISTINCT JNO
FROM SPJ SPJ1
WHERE NOT EXISTS
(SELECT *
FROM SPJ SPJ2
WHERE SPJ2.SNO='S1'
AND NOT EXISTS
(SELECT *
FROM SPJ SPJ3
WHERE JNO = SPJ1.JNO
AND PNO = SPJ2.PNO)
);
这个题不太确定,运行出来的结果是J4,感觉不太对,看表里面没有工程是完全使用S1供应的P1和P2的。
5.针对4题中的表做如下问题。
供应商表S(SNO,SNAME,STATUS,CITY)
零件表P(PNO,PNAME,COLOR,WEIGHT)
工程项目表J(JNO,JNAME,CITY)
供应表SPJ(SNO,PNO,JNO,QTY)
(1)找出所有供应商的姓名和所在城市
SELECT SNAME,CITY
FROM S;
(2)找出所有零件的名称,颜色,重量
SELECT PNAME,COLOR,WEIGHT
FROM P;
(3)找出所有供应商S1所供应零件的工程号码JNO
SELECT JNO
FROM SPJ
WHERE SNO='S1';
(4)找出所有工程项目J2使用的各种零件的名称和数量
SELECT PNAME,QTY
FROM P,SPJ
WHERE JNO='J2' AND P.PNO=SPJ.PNO;
(5)找出上海厂商供应的所有零件号码。
用S,SPJ表
SELECT PNO
FROM S,SPJ
WHERE S.SNO=SPJ.SNO AND CITY='上海';
(6)找出使用上海产的零件的工程名称
上海产的零件如(5)所示,将(5)中的语句作为子查询即可。
SELECT JNAME
FROM J
WHERE JNO IN
(SELECT JNO
FROM S,SPJ
WHERE S.SNO=SPJ.SNO AND CITY='上海');
SELECT JNO
FROM SPJ
WHERE JNO NOT IN
(SELECT DISTINCT JNO
FROM S,SPJ
WHERE S.CITY='天津' AND S.SNO=SPJ.SNO);
(8)把全部红色零件的颜色改为蓝色
改数据用UPDATE,该表用ALTER!!!
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';
这里需要先删掉SPJ表中的相应记录,在删S表中的,因为SPJ中的SNO是外码,先删S表中的,无法删除。
(11)请将(S2,J6,P4,200)插入供应情况关系中。
INSERT INTO SPJ
VALUES('S2','J6','P4',200);
9.为三建工程项目建立一个供应情况的视图,包括SNO,PNO,QTY.
CREATE VIEW SAN_JIAN
AS
SELECT SNO,PNO,QTY
FROM SPJ,J
WHERE SPJ.JNO=J.JNO AND JNAME='三建';
(1)找出三建项目使用的各种零件代码PNO和其数量。
SELECT DISTINCT PNO,QTY
FROM SAN_JIAN;
(2)找出供应商S1的供应情况。
SELECT *
FROM SAN_JIAN
WHERE SNO='S1';
做下来,感觉题都还可以,第四题的(5)有些难,还是有些反应不过来,做出来的情况感觉也不太对,也比较耗时。