数据库作业答案(更新中)

第一章

第二章

1、

Q:试述关系模型的完整性规则。在参照完整性中,什么情况下外码属性的值可以为空值?

A:

  • 实体完整性规则:关系R的主属性不能是空或未知。
  • 参照完整性规则:关系R的外码F要么是空值,要么是被参照关系的某个码值。
  • 用户定义的完整性:某一具体应用的数据必须满足语义要求

在尚未给该关系设置参照时,此时外码F的值可以全部为空。

2、

第二小题图

A:

(1)

R1=TYcdefdeR_1=\begin{array}{|c|c|} \hline T & Y \\ \hline c & d\\ \hline e & f\\ \hline d & e\\ \hline \end{array}

(2)

R2=PQTYqaef7gefR_2= \begin{array}{|c|c|c|c|} \hline P & Q & T & Y\\ \hline q & a & e & f\\ \hline 7 & g & e & f\\ \hline \end{array}

(3)

R3=PQTYB2bcdm2bcdn7gcdm7gcdnR_3= \begin{array}{|c|c|c|c|c|} \hline P & Q & T & Y & B\\ \hline 2 & b & c & d & m\\ \hline 2 & b & c & d & n \\ \hline 7 & g & c & d & m \\ \hline 7 & g & c & d & n\\ \hline \end{array}

(4)

R4=QPYb2fa9fg7fR_4= \begin{array}{|c|c|c|} \hline Q & P & Y\\ \hline b & 2 & f\\ \hline a & 9 & f\\ \hline g & 7 & f \\ \hline \end{array}

(5)

R5=PQ2b7gR_5= \begin{array}{|c|c|} \hline P & Q \\ \hline 2 & b\\ \hline 7 & g\\ \hline \end{array}

3、

设有一个SPJ数据库,包括S,P,J及SPJ4个关系模式:

S(SNO, SNAME, STATUS, CITY);

P(PNO, PNAME, COLOR, WEIGHT);

J(JNO, JNAME, CITY);

SPJ(SNO, PNO, JNO, QTY)。

供应商表S由供应商代码(SNO)、供应商姓名(SNAME)、供应商状态(STATUS)、供应商所在城市(CITY)组成。

零件表P由零件代码(PNO)、零件名(PNAME)、颜色(COLOR)、重量(WEIGHT)组成。

工程项目表J由工程项目代码(JNO)、工程项目名(JNAME)、工程项目所在城市(CITY)组成。

供应情况表SPJ由供应商代码(SNO)、零件代码(PNO)、工程项目代码(JNO)、供应数量(QTY)组成,表示某供应商供应某种零件给某工程项目的数量为QTY。

试用关系代数完成如下查询:

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

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

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

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

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

A:

(1)

πSNO(σJNO=J1(SPJ))\pi_{SNO}(\sigma_{JNO=J_1}(SPJ))

(2)

πSNO(σJNO=J1PNO=P1(SPJ))\pi_{SNO}(\sigma_{JNO=J_1\bigwedge PNO=P_1}(SPJ))

(3)

πSNO(σJNO=J1(SPJ)σCOLOR=red(P))\pi_{SNO}(\sigma_{JNO=J_1}(SPJ)\Join\sigma_{COLOR=red}(P) )

(4)

πJNO(SPJ)πJNO(SPJσCITY=天津(S)σCOLOR=red(P))\pi_{JNO}(SPJ)-\pi_{JNO}(SPJ\Join\sigma_{CITY=天津}(S)\Join\sigma_{COLOR=red}(P))

(5)

πJNO(SPJ÷πPNO(σSNO=s1(SPJ)))\pi_{JNO}(SPJ\div \pi_{PNO}(\sigma_{SNO=s_1}(SPJ)))

4、

题目同上,但是要求用元组演算完成

(1)

{tu(SPJ(u)u[3]=J1t[1]=u[1])}\{t\vert\exist u( SPJ(u)\wedge u[3]=J_1\land t[1]=u[1] )\}

(2)

{tu(SPJ(u)u[3]=J1u[2]=P1t[1]=u[1])}\{t\vert\exist u ( SPJ(u)\wedge u[3]=J_1\wedge u[2]=P_1\land t[1]=u[1])\}

(3)

{tut(SPJ(u)P(a)u[3]=J1a[1]=u[2]a[3]=redt[1]=u[1])}\{t\vert\exist{u}\exist{t}(SPJ(u)\wedge P(a) \wedge u[3]=J_1\wedge a[1]=u[2]\wedge a[3]=red\land t[1]=u[1])\}

(4)

{tabc(¬(SPJ(a)S(b)P(c)b[1]=a[1]b[4]=天津c[1]=a[2]c[3]=red)t[1]=a[3])}\{t\vert\exist{a}\exist{b}\exist{c}(\lnot(SPJ(a)\land S(b)\land P(c)\land b[1]=a[1]\land b[4]=天津\land c[1]=a[2]\land c[3]=red)\land t[1]=a[3]) \}

(5)

{ta(SPJ(a)b(SPJ(b)(b[1]=S1c(SPJ(c)a[3]=c[3]c[2]=b[2])))t[1]=a[3])}\{t\vert\exist{a}( SPJ(a)\land \forall b(SPJ(b)\rightarrow(b[1]=S_1\rightarrow\exist c(SPJ(c)\land a[3]=c[3]\land c[2]=b[2])))\land t[1]=a[3])\}

第三章

1、

第一题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
create table S(
Sno int primary key,
Sname char(10),
Sstatus char(10),
City char(10)
);

create table P(
Pno int primary key,
Pname char(10),
Color char(5),
weight int
);

create table J(
Jno int primary key,
Jname char(10),
city char(10)
);

create table spj(
sno int,
pno int,
jno int,
qty int,
foreign key (sno) references S(Sno) on update cascade on delete cascade,
foreign key (pno) references P(Pno) on update cascade on delete cascade,
foreign key (jno) references J(jno) on update cascade on delete cascade,
primary key (sno,pno,jno)
);

2、

第二题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- 1
select distinct sno from spj where jno=J1;
-- 2
select distinct sno from spj where jno=j1 and pno=p1;
-- 3
select sno from spj,p where jno=32 and p.pno=spj.pno and color='red';
-- 4
select jno from j where not exists (
select * from s,p,spj where s.sno=spj.sno and p.pno=spj.pno and j.jno=spj.jno and color='red' and s.City='天津'
);
-- 5
select distinct jno from spj t1 where not exists (
select * from spj t2 where t2.sno=s1 and not exists (
select * from spj t3 where t3.jno=t1.jno and t3.pno=t2.pno
)
)

3、

第三题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
-- 1
select sname,city from s;
-- 2
select pname,color,weight from p;
-- 3
select distinct jno from spj where sno=S1;
-- 4
select pname,qty from spj,p where p.pno=spj.pno and jno=J2;
-- 5
select distinct pno from spj,s where s.City='上海' and s.sno=spj.sno;
-- 6
select distinct jname from j,spj,s where s.City='上海' and spj.sno=s.Sno and j.jno=spj.jno;
-- 7
select distinct jno from spj where not exists(
select * from s where spj.sno=s.sno and s.city='天津'
);
-- 8
update p set color='blue' where color='red';
-- 9
update s set sno=S3 where sno in(
select sno from spj where sno=S5 and jno=J4 and pno=p6
);
-- 10
delete from s where sno=S2;
-- 11
insert into spj values (S2,J6,P4,200);

4、

第四题

--
create view myview (sno,pno,qty) as
(
	select sno,p.pno,qty from spj,p where s.pno=spj.pno and jname='三建工程'
);
-- (1)
select pno,sum(qty) from myview  group by pno;
-- (2)
select * from myview where sno=S1;

数据库作业答案(更新中)
http://zhouhf.top/2022/10/11/数据库作业答案(更新中)/
作者
周洪锋
发布于
2022年10月11日
许可协议