赞
踩
这里插入LaTeX公式有点不太美观就直接上题目了。
说实话,关系代数这块有几个星期没碰都快忘光了233.
σ是选择的意思,这里的意思是从S中选择A是10的S.
SELECT *
FROM S
WHERE A='10';
Π是投影的意思,那么就是S上A、B的投影。
SELECT A,B
FROM S;
⋈下面未加条件,是自然连接的意思,它会把重复的属性去掉,而且进行比较的分量必须是相同的属性组。
SELECT A,B,S.D,S.D,E,F
FROM S,T
WHERE S.C=T.C AND S.D=T.D;
这里是一个等值连接,把条件加入到WHERE子句中即可。
SELECT *
FROM S,T
WHERE S.C=T.C;
同上一题差不多。
SELECT *
FROM S,T
WHERE S.A<T.E;
这题的意思是S上C、D的投影和T的笛卡尔积。
SELECT S.C,S.D,T.*
FROM S,T;
emmm,看来我又要把那个题出来一遍了,等会啊。
我在下面敲代码的时候大小写切换手都抽筋了,大家一定不要学我,属性名全大写算了
S表:
CREATE TABLE S
(Sno CHAR(2) UNIQUE,
Sname CHAR(6),
Status CHAR(2),
City CHAR(4));
P表:
CREATE TABLE P
(Pno CHAR(2) UNIQUE,
Pname CHAR(6),
Color CHAR(2)
Weight INT);
J表:
CREATE TABLE J
(Jno CHAR(2) UNIQUE,
Jname CHAR(8),
City CHAR(4));
SPJ表:
CREATE TABLE SPJ
(Sno CHAR(2),
Pno CHAR(2),
Jno CHAR(2),
QTY INT);
插入数据过程略去
SELECT DISTINCT Sno /*去除掉了重复的SNO*/
FROM SPJ
WHERE Jno='J1';
SELECT DISTINCT Sno /*去除掉了重复的SNO*/
FROM SPJ
WHERE Jno='J1' AND Pno='P1';
SELECT Sno
FROM SPJ,P
WHERE Jno='J1'
AND SPJ.Pno=P.Pno
AND Color='红'; /*给的限定条件是J1以及颜色为红*/
SELECT DISTINCT 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);
SELECT Jno
FROM SPJ
WHERE Pno >ALL /*这里我不确定可不可以这样写,因为没有数据让我实践*/
/*S1供应的零件是P1和P2,我这里的意思是查询Pno是P1和P2的JNO*/
(SELECT DISTINCT Pno
FROM SPJ
WHERE Sno='S1')
SELECT Sname,City
FROM S;
SELECT Pname,Color,Weight
FROM P;
SELECT DISTINCT Jno
FROM SPJ
WHERE Sno='S1';
SELECT Pname,QTY
FROM SPJ,P
WHERE P.Pno=SPJ.Pno AND SPJ.Jno='J2';
SELECT Pno
FROM SPJ,S
WHERE S.Sno=SPJ.Sno AND City='上海';
SELECT Jname
FROM SPJ,S,J
WHERE S.Sno=SPJ.Sno AND S.City='上海' AND J.Jno=SPJ.Jno;
SELECT DISTINCT Jno
FROM SPJ
WHERE Jno NOT IN
(SELECT DISTINCT Jno
FROM SPJ,S
WHERE S.Sno=SPJ.Sno
AND S.City='天津';
UPDATE P
SET Color='蓝'
WHERE Color='红';
UPDATE SPJ
SET Sno='S3'
WHERE Sno='S5' AND Jno='S4' AND Pno='P6';
DELETE
FROM S
WHERE Sno='S2';
DELETE
FROM SPJ
WHERE Sno='S2';
INSERT INTO SPJ
VALUES ('S2','J6','P4',200);
三建工程的供应情况视图:
CREATE VIEW SANJIAN
AS SELECT Sno,SPJ.Pno,QTY
FROM SPJ,J
WHERE SPJ.Jno=J.Jno AND J.Jname='三建';
SELECT DISTINCT Pno,QTY
FROM SANJIAN
SELECT DISTINCT *
FROM SANJIAN
WHERE Sno='S1';
这几个题目中我觉得有点问题的就是2中的第5小题,其他的基本看着题目都能写出来。
参考文献:
[1]萨师煊,王珊,数据库系统概论.5版.北京:高等教育出版社,2014.
[2]David老师的PPT.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。