Solution

advertisement
CSCI 5333.2 DBMS
Spring 2015
Suggested Solution to Mid-Term Examination
(1) For example:
Member
*
«unique» -MemberId[1..1]
-LastName[1..1]
-FirstName[1..1]
-EMail[1..1]
-Phone[0..1]
-StartDate[1..1]
0..1
-mentee
Mentor
0..*
0..*
«unique» -MentorId[1..1]
-LastName[1..1]
-FirstName[1..1]
-EMail[1..1]
-Phone[0..1]
*
-referer
1..1
*
*
Hobby
MentorHobby
«unique» -HobbyName[1..1]
-Description[1..1]
-ExpertiseLevel[1..1]
1..1 0..*
(2) For example:
A(P,R) :
 CK: P
 FK: none
 Non-nullable: P
AQ(P,Q):
 CK: PQ
 FK: P references A(P)
 Non-nullable: P, Q
B(BId, S,P)
 CK: BId
 FK: (1) P references A(P)
 Non-nullable: BId, P
y(P, BId):
 CK: {P, BId}
 FK: (1) P references A(P), (2) BId references
B(BId)
 Non-nullable: P, BId
BT(BId, T)
 CK: {BId, T}
 FK: BId references B(BId)
 Non-nullable: BId, T
C(U, P, z_U)
 CK: U
 FK: (1) P references A(P), (2) z_U
references C(U)
 Non-nullable: U
k(U_1,U_2)
 CK: {U_1, U_2}
 FK: (1) U_1 references C(U), (2) U_2
references C(U),
 Non-nullable: U_1, U_2
(3)
(a)
(b)
(c)
(d)
(e)
T
F
F
F
T
(4)
(a)
There is one candidate key. A, B, C, AC and BC cannot be candidate key since their values are not
unique. AB values are unique and thus AB may or may not be a CK. If AB is a CK, ABC cannot be a CK as it
will not be minimal. If AB is not a CK, ABC is a CK. In both cases, there is only one candidate key.
(b)
Minimum: 1; e.g. when the candidate key is ABCDE for R(A,B,C,D,E)
Maximum: 16; e.g. when the candidate key is A
(5)
For example,
(a)
π SNAME (σ Status > 5 (SUPPLIER) |X| (σ PNum = ‘P1’ (SUPPLY)))
or
project [SName] ((select [status > 5] (Supplier)) join (select [pnum='P1'] (supply)));
(b)
π PNum (SUPPLY |X| σSCity=‘Pheonix’ (SUPPLIER)) - π PNum (σSNum=’S3’ (SUPPLY))
Or
(project [PNUM] (supply join (select [SCity='Phoenix'] (supplier))))
minus
(project [PNUM] (select [SNum='S3'] (supply)));
(c)
π SNUM, PNUM (SUPPLY)/ ( π SNUM (σ Status>11 (SUPPLIER)))
Or
(PROJECT [PNUM] (SUPPLY))
MINUS
(PROJECT [PNUM]
(((PROJECT [PNUM] (SUPPLY))
TIMES
(PROJECT [SNUM] (SELECT [STATUS>11] (SUPPLIER))))
MINUS
(PROJECT [PNUM, SNUM] (SUPPLY))));
(6)
(a)
{(sname) | Supplier(snum,sname,_,status), Supply(snum, ‘P1’, _), status>5}
(b)
{(pnum) | (snum, scity,’Pheonix’,_) ε Supplier, (snum, pnum, _) ε Supply, (‘S3’,pnum,_)∉ Supply}
(7)
(a)
select distinct S.SName
from Supplier S join supply U on (S.SNum = U.SNum)
where S.status > 5
and U.PNum = 'P1';
(b)
select distinct U.pnum
from supplier S, supply U
where S.SNum = U.SNum
and S.SCity = 'Phoenix'
and U.pnum not in
(select pnum
from supply
where snum = 'S3');
(c) – Note that MySQL does not support INTERSECT
select distinct p.pname, p.color, p.weight
from part p, supply u1, supplier s1, supply u2, supplier s2
where p.pnum = u1.pnum
and p.pnum = u2.pnum
and u1.snum = s1.snum
and u2.snum = s2.snum
and s1.status >= 13
and s2.status < 5;
-- or
select distinct p.pname, p.color, p.weight
from part p
where pnum in
(select distinct pnum
from supply u, supplier s
where u.snum = s.snum
and s.status >= 13)
and pnum in
(select distinct pnum
from supply u, supplier s
where u.snum = s.snum
and s.status < 5);
Download