DB-ComplementaryNotes-II.pdf - Sharif University of Technology

44
4 r lS""' L.J J.> I yA ,-, I..;-'p. L.?lyb ,-, jl4$?lA>Jli.o-f U0 1 p.'-6 llbJL.o ,-6 l5 11b JL.of-6 yl.w.>-r l5 .b: 1 3) '-r- -r- I I o5.:J· l;." ..AJ' - V ..I-- r.:::- -V LSo -A ;// JV) -q

Transcript of DB-ComplementaryNotes-II.pdf - Sharif University of Technology

J~~ 4 ~IJ LSUb~~lii'-'

~ LS~ J~ LS~IJ-r ~I.,b LSoJ4J~-r

lS""' L.J J.> I yA ,-,

I..;-'p. L.?lyb w~~ ,-,

~o~ ~I~ jl4$?lA>Jli.o-f

LSI~IJ ~-!:J

~~ U01p.'-6

llb~ ~lS' l5<u:~,-6

llbJL.o ,-6

~.~ l511bJL.of- 6

LSI~IJ yl.w.>-r

l5 j)~ .b:13) '-r­~JL.o' -r-

~.~ l5~JL.o,-r-

I I o5.:J· ~ l;." ..AJ' - V ~.. ..I--

~~.r... U)~'-V

151~1) r.:::- )~ ~4. ' -V

151~1) ~L> )~ ~4.'-V

~ ~I~ LSJ~' LSo J4J~ -A

~. ~ .

~~)J J ;// JV) -q

@- course(1)

Relations vs. Tables

For purposes of reference, we present in this subsection a list of some of the main differ­ences between (a) the formal object that is a relation as such and (b) the informal object that is a table. which is an informal picture on paper of that formal object:

l. Each attribute in the heading of a relation involves a type name, but those type names are usually omitted from tabular pictures.

2. Each component of each tuple in the body of a relation involves a type name and an attribute name, but those type and attribute names are usually omitted from tabular pictures.

3. Each attribute value in each tuple in the body of a relation is a value of the applicable type, but those values are usually shown in some abbreviated form-for example, 5 I instead of 5#('5 l ')-in tabular pictures.

4. The columns of a table have a left-to-right ordering, but the attributes of a relati on do nol. Note: One implication of this point is that columns might have duplicate names, or even no names at all. For example, consider this 5QL query:

SELECT S.CITY, S.STATUS * 2, P.CITY FROM S, P ;

What arc the column names in the result of this query ?

5. The rows of a table have a top-to-bottom ordering, but the tuples of a relation do not.

6 . A table might contain duplicate rows, but a relation does not contain duplicate tuples.

The foregoing is not an exhaustive list of the differences. Others include:

• The fact that tables are usually regarded as having at least one column, while rela­tions are not required to have at least one attribute (see the subsection "Relations with No Attributes" later in this section)

• The fact that tables-at least in 5QL-are allowed to include nulls, while relations are certainly not (see Chapter 19)

• The fact that tables are "flat" or two-dimensional, while relations are n-dimensional (see Chapter 22)

It follow s from all of the foregoing that, in order to agree t~at a tabular picture can properly be regarded as representing a relation, we have to agree o~ how to "read " such a picture; in other words, we have to agree on certain rules of inter~retation for such pic­tures. To be specific, we have to agree that there is an underlying type for each column; that each attribute value is a value of the relevant type; that row and column orderings are irrelevant; and that duplicate rows are not allowed. If we can agree on all of these rules of interpretation, then-and only then-we can agree that a table is a reasonable picture of a relation .

50 we can now see that a tabl e and a relation are indeed not quite the same thing (eve n though it is often convenient to pretend they arc). Rather, a relation is what the definition says it is, a rather abstract kind of object , and a table is a concrete picture, typically on

j/& TE Of

T-i

paper, of such an (lhstract object. Thcy are not (to repeat) quite the same. Of course. they are very similar . . . and in infonnal contexts, at least, it is usual, and perfectly acceptable, to say they are the same. But when we are trying to be precise-and right now we are try­ing to be precise-then we do have to recognize that the two concepts are not exactly iden­tical. . -'

That said, it i's worth pointing out too that in fact it is a major advantage of the rela-tional mode(that its basic abstract object, the relation, does have such a simple representa­t~on on paper. \t1S that simple representation that makes relational systems easy to use and easy to understand, and makes it easy to reason about the way relational systems behave.

t

Nevertheless, it is unfortunately also the case that that simple representation does suggest some things that are not true (e.g., that there is a top-to-bottom tuple ordering).

Relations with No Attributes

Every relation has a set of attributes; and, since the empty set is certainly a se~, it follows that it must be possible for a relation to have the empty set of att~butes, or in other words no attributes at all. (Do not be confused: We often talk about "empty relations," meaning relations whose body is an empty set of tuples, but here, by contra'st, we \lf~ talking about relations whose heading is an empty set of attributes.) Thus, relations with no attributes are at least respectable from a mathematical point of view. What is perhaps more surpris­ing is that they turn out to be extremely important from a pnictical point of view as well!

In order to examine this notion more closely, we first need to consider the question of whether a re lation with no attributes can contain any tuples . The answer (again perhaps surprisingly) is yes. To be more specific, such a relation can contain at most one tuple:

namely, the O-tuple (i.e ., the tuple with no components; it cannot contain more than one such tuple, because all O-tuples are duplicates of one another). There are thus precisely two relations of degree zero--one that contains just one tuple, and one that contains no tuples at all. So important are these two relations that, following Darwen [6.5], we have pet names for them: We call the first TABLE_DEE and the other TABLE_DUM, or DEE and-DUM for short (DEE is the one with one tuple, DUM is the empty one) . Note: It is hard to d.r1i~ pictures of these relations! Thinking of relations as conventional tables breaks down\ somewhat, in the case of DEE and DUM.

) Why are DEE and DUM so important? There are several more or less interrelated

answers to this question. One is that they playa role in the relational algebra-see Chap­i ter 7-that is akin, somewhat, to the role played by the empty set in set theory or zero in ordinary arithmetic. Another has to do with what the relations mean (see reference 16.5]): essentially, DEE means TRUE, or yes, and DUM means FALSE, or 110. In other words, they have the most fundamental meanings of all. (A good way to remember ~hich is which is that the "E"s in DEE match the "e" in yes.)

In Thtorial D, the expressions TABLE_DEE and TABLE_DUM can be used as short­hand for the relation selector invocations

RELATION { } { TUPLE { } }

and

RELATION { } { }

respectively. It is not possible to go into more detail on this topic at this juncture; suffice it to say

that you will encounter DEE and DUM many times in the pages ahead. For further discus­sion, see reference [6.5].

CREATE DOMAIN domain-name datatype

[default-definition]

[domain-constraint-definition-list]

J~ •

CREATE DOMAIN DEGREE CHAR(3) DEFAULT '???'

CONSTRAINT VALID-DEGREES

CHECK VALUE IN ('bs' ,'ms' ,'doc' ,'???')

" I. t.:.1" ~ .. .. .) • J~ .. ~)~

CREATE ASSERTION constraint-name

[BEFORE COMMIT I AFTER{INSERTIDELETEIUPDATE[OF(column-name)]}

ON table.-name ... ]

CHECK condition(s)

[FOR [EACH ROW OF] table-name]

CREATE ASSERTION GRADECHECK BEFORE INSERT ON STCOT CHECK (O~GRADE~20);

CREATE ASSERTION BFDA CHECK (NOT EXISTS (SELECT * FROM R

GROUP BY A HAVING MAX (B) j MIN (B)));

. ..::........1 J>~;"" R (A, B, (, D) L5~I) : f J~ •

CREATE ASSERTION K CHECK (NOT EXISTS (SELECT * FROM R

WHERE A = B AND C j D));

CREATE ASSERTION C CHECK (( SELECT COUNT ( * ) FROM ( (SELECT STID FROM STT WHERE STDEID =

(SELECT STID FROM STCOT WHERE COlD

CREATE ASSERTION A

'D11') EXCEPT , C 11 ' )) AS N)

:fJ~ •

0) ;

CHECK (NOT EXISTS (SELECT * FROM STT WHERE STJ = 'comp' AND NOT EXISTS (SELECT * FROM STCOT WHERE STCOT.STID = STT.STID AND STCOT.COID = 'C12')));

----

Fof( EI7Cfl ~i.4r£!1ENT

J .J.,' \ . 1/ . I? j" . j' / ,./ .) L; . / L. \.J'D./ ..-1.1,,; /' oAr'"' 0) ;) --'..>-'~ (~ ;J~./ I V --: .

- . "/,/) - I .,/

FOR E/lcH STI9TE MENT ~/;_~~'i-!

V~ C/(lf4 c? /) C,,.(L/)!; if (t..,/ · r~ wj

• _AJ>.> \ ~ r ~ ) CREt9TE -r121&&-[:1( /1VSI)L-r~Ir;.

A F TF~ UPO/)TF OF Es;:;L o/v Er1pL . f?- E FE ;e r IV .. 1 N G·

--- - -

OLD /('3 OEM"PL.~. NEW Ag N~MPL Fait FACH '5 rt9T Et1 [NT

WI-!- EN '01-' > ( SE.Lf cT IfVG (E ss/)L ) FR-r; M (3.l'1pL)

8E& lN o E L E ri~'" F(( 0/'1 E t1 pI.

WHEf(£ ( EMrD ,ENfiME,·· ... ~ £(/08, ~ ~I)L) IN NEr1pL;

INt3E~T INTO FMPL I

~EL r CT * fteor/ OEI\1pL ; ENO i

CREATE TR1GGER -name ­{BEFORE I AFTER I INSTEAD OF }

{ INSERT I DELETE I UPDATE OF Column }

ON Table name [ORDER value)

(REFERENCING {NEW I OLD I NEW- TABLE I OLD- TABLE} AS name) {WHEN Condition (n))

(SQL 3 Procedure

(FOR EACH (ROW I STATEMENT}))

: f'O"'..;-:f l.5" yb ).:J I) ,I j,A> \,)j-::-o , ..I..:..A)lS' w~ LS,l> ~I) : I J t!l.

EMPREL (EMPlD , EMPNAME , ... .. , EMPSAL) C.K.

u-.:.I)"'..j L5L.;La,) . .. ~~~ J.tblS' o~ .l.:...o)5" j,A>" : ..:.-.....1 ~ .;k.:J)}-" ~.:J,...I..:><..o

: .J..:S tS" JL...:II) ~.:J,...I..:><..o

CREATE TRIGGER EMPREL- UPDATE- TRIG

BEFORE ·UPDATE OF EMPSAL ON EMPREL

RC;'ERE~CING OLD · AS OEMPREL , NEW AS NEMPREL ,

(WHEN OEMPREL.EMPSAL> NEMPREL . EMPSAL

SIGNAL. SQ L State ' 7005' ('salary can not be decreased')

FOR EACH ROW) .;

: ~).) ~ I))"'..j ~I), EMPREL ~I) r. o,':>k ~tS" uO} j~ Lr-I).:J . =t' J L!. / -

I

EMPSALAUG ( EMPID ,AUGM) . C. K.

. ..::........,1 .l.:...o)1.) j~ ~I}I )..u..o AUGM ~

. .J..:StS" ~ L5jL., 1"4. ~) ~ I) o~4 L5ld.:J L5)tfjL.,)"'..j LSL.;La,)

CREATE TRIGGER DBUPDATETRIG

AFTER UPDATE OF EMPSAL ON EMPREL

REFERENCING OLD AS OEMPREL, NEW A S NEMPREL

(UPDATE EMPSALAUG SET AUGM = AUGM + NEMPREL.EMPSAL - OEMPREL.EMPSAL

WHERE EMPSALAUG.EMPID = EMPREL.EMPID

FOR EACH ROW) ;

: ~,}-O )"'..j LSLa,~I) . ..::........,1 [ELMA 2000] jl Q yf r. Jl:... Lr-I : r J d.

EMPLOYEE (NAME, SSN , SALARY , DNO , SUPERVISOR - SSN)

DEPARTMENT (DNAME , DNO , TOTAL-SAL, MANAGER-SSN)

(a)

Rl : CREATE TRIGGER TOTALSALl

AFTER INSERT ON EMPLOYEE

FOR EACH ROW

WHEN (NEW.DNO IS NOT NULL)

UPDATE DEPARTMENT

SET TOTAL_SAL = TOTAL_SAL + NEW.SALARY

R2: CREATE TRIGGER TOTALSAL2

AFTER UPDATE OF SALARY ON EMPLOYEE

FOR EACH ROW

WHEN (NEW.DNO IS NOT NULL)

UPDATE DEPARTMENT

SET TOTAL_SAL = TOTAL_SAL + NEW.SALARY- OLD.SALARY

WHERE DNO = NEW.DNO ;

R3 : CREATE TRIGGER TOTALSAL3

AFTER UPDATE OF DNO ON EMPLOYEE

FOR EACH ROW

BEGIN

UPDATE DEPARTMENT

SET TOTAL_SAL = TOTAL_SAL + NEW.SALARY

WHERE DNO = NEW.DNO ;

UPDATE DEPARTMENT

SET TOT.\L_SAL = TOTAL_SAL - OLD.SALARY

WHERE DNO = OLD.DNO ;

END;

IN : CREATE TRIGGER TOTALSAL 4

AFTER DELETE ON EMPLOYEE

FOR EACH ROW

(b)

WHEN (OLD .DNu IS NOT NULL)

UPDATE DEPARTMENT

SET TOTAL_SAL = TOTAL_SAL - OLD.SALARY

WHERE DNO = OLD.DNO ;

RS: CREATE TRIGGER INFORM_SUPERVISORI

BEFORE ii'~SERT OR UPDATE OF SALARY, SUPERVISOR_SSN ON EMPLOYEE

FOR EACH ROW WHEN

, -(NEW. SALARY > (SELECT SALARY (ROM EMPLOYEE

WHERE SSN = NEW.SUPERVISOR_SSN)) INFORM_SUPERVISOR (NEW.SUPERVISOR_SSN, NEW.SSN) ;

[0)); o'.f ~ /) ~.) )) .:::;;~/.> L·~.1 ,>/~ ~ . . ; , ~, 0,1;: .. • , \ ~

~ ::) ~~4lj J(j. J - / - .---

: ~.(,~~~/~ v::,-> ~Uo~

,,4~ "3 £t.£ C.r grID;J SIN /THE J flOE&- J ~r£)EIO

F£f}1'1 S7r , I

WHE/tE <srHI~= C~fi ;

I • - • / () 1// I •. I \ ~ • l! // 'STI ((~ ~J//.) I ~ (j-> to) )0 b;"' ~ ./.> ~..:> ~/ /~ ~ ~ AJDJ/ ~/ ./) -) //cr.)/)~ \I 1/)) tV ~/)~ 'I L..:' ,. /) ~/, r ~-r/vtJI? '" .... ~)~

' -~ - -?

~ - II' /} . ~ )- j~ Uh~ ~/ . r V.}-J.)/J~~/~/.> ~ ~~ \'c~/t '/ O)v<j-:/~" -?

:(;::-;J"':u ~ OJ ~J ~' L.,/ ;,) Pi'~)-( ->,::j~Jj- )/oj;/ ~ ... ->

C f-fr9 ;c Tit! G-G fR- rT(JtJ:/.

ItJ BT£/)1? Of IN'fEte. ! ON -V

(?£FJ?;eEtvCING NEvi /tow;9'3 ;e Fo;e E/lC# f(r;v/

rtJgE~T t"NT(l r r r V ilL liE ~ ( re. ~ T:!P,I /C. .. ~T ,;..M?r1E .1 te .. g1/JE ~ JO ,~f.. I , R·CSIlJE1P).

I / / ;; - L .//- j <- '}<j9J y t1 y' b3 f D/~ > > ~ (~~ ;?' t,/)r ~ i! ~

-~ ~}/tr <577- ~ /",,,,) -t.)J}-. (: /.).;J~ ,V ~.)/)

< J'" S t., " .b j ,r -' " CCJrrK~ ' 1 D t.9 > '~,AP>J.T/)~..J (V ::) /)<-1 J;.f~/.) ( _/)~ J&~ ~/~..:'

I? ,-

CREATE TRIGGER STCOTITRIG

. BEFORE INSERT ON STCOT

REFERENCING NEW AS NSTCOT

CREATE TRIGGER COTDELTRIG BEFORE DELETE ON COT

REFERENCING OLD AS OLCO (DELETE FROM STCOT

WHERE STCOT.COID = OLCO.COLD FOR EACH ROW) ;

:?'J~II! (WHEN (NOT EXISTS (SELECT * FROM COT WHERE COT.COID =

NSTCOT. COID)) C.L0JL~- ' J (;:. ':iJ/ .)

AB ORT TRANSACTION FOR EACH ROW ); ~

·->/(f'J~l c/> y ./>

I@.

Hs = {B"B2 ,···,BJ

HT = {C"C2 ,···,Cn }

JJ~ I/~ I@l

-?' ~?c.r/7-f

..:.r..1 j;) • ...t....::. d.i:. ..... 1 ..::.,.t...i...o ~ ~ ~.; ~ rAIlS J q Ails 'PAIlS .J .l:...-.o r, q • f : ~ ~.} :r.; .J

• ...L:...!.~ ..::.,.u...., jI ~ ~ ,.S: ... Js:....y. .N .JM2 .MI .L3 ,L2 ,LI .J ...J.:....:.~ u.....l:.......,.

:[CONN2002]..t;)]r. tS l ~IJ.r.:- LSLt. ~ j;) J'~ LJ""'1y>-

1) O"pl\qM(R) = O"p(O"q (O"r (R»

5) R r>.<J p S = S t><J p R

RxS=SxR

6) 0-p (R r>-<J r S) = (0-p (R) C>-<J r S)

O"p(R x S) = (0" p(R»x S

7) o-pl\q(R C>-<lr S) = (0- peR) C>«]r O"q(S))

0" pl\q (R x S) = (0-p (R) x 0" q (S))

.L r.:: .k r- j.) J ~~ Hs jI ~l,W. J-L.:. .h.O L2 J H R jI ~li,p J-L.:. .b.A..; L) :6 ~ r- ~

. ..l..!.~ ci j jl5" ~ L) U L2 jI ~l,W. .h.O

. ..:.-1 /i.r. ;,;. S..,R 0-' -Y jl5" ~.rP ~l>- j.) ~\.;.:. j: I

~ ~ ')\.;..A ,..t.:.~ ci j jl5" ~ L) U L2 ul,W. jI [.j\.;.:. c.S~.) ul,W. .L.r.:: .k.r- j.) }'I Lei

Hs jI ~li,p ~L.:..h.O M2 J HR jI ~lL,., J-L.:. .h.O M( 4$ c.S~ ~M( U M2 ulL,.,

:Uj..rP j:1 j.) ..l..!.~

IT<L,UL..z> (R [>.<]r S) = I1<L, UL..z > (I1<L,UM1> (R» t>-<lr ([J<L,UM

2> (S»

9) (R t>-<l S) !>«l T = R C>-<l (S [><] T)

. ..:.-1 ~ ~.r.:: ~ j)2:.. ~I.J.) !~ Y

(RxS)xT=Rx(SxT)

:r-'~b Uj~ j:1 j.) ..l..!.~ HT J Hs jI ~lL,., ~L.:. ~ ~ '.L~ .k.r- }"I

(R t>-<l p S) C>-<l qM T = R r><J pM (S [>«J q T)

10) RUS = SUR RnS=SnR

11) O'p(RUS) = O'p(S)Uap(R)

0' p (R n S) = 0' p (S) n 0' p (R)

a p (R-- S) = a p (R) - a p (S)

12) IT<L> (R U S) = IT<£.> (S) U IT<L> (R)

13) (R US) U T = S U (R U T)

(R n S) n T = S n (R n T)

• . MIN UJ

.. X TrMES

':..J '~)/VV::J I!>L..!_ Y

• () : rtE ~T )t1.c. r on' PROd ECT

o U : UNToN

n< 5N,4.a(,,J 9f'f' :/'Z' ~ S P l><l '3 ~ - ./ - 0" ~ - / - / . . 1/ .. /-. )' D . A-J ~ ~ --" ~/ rdv -::! J'J~ ,J jtAP ~ jt" (5 '6'v:7 (''''. '\ 2.. •

((( p ~ LC'Lo[{ =-'U') 'J OZ,v s p )['3#] (JOUJ g )rS#t9I'/E]

: ;:.r;.rj' J/ ~ Wv (C/"'3'/I')/dJlf:Jr-j' (r : 'Q3

(( ~p[ 5#-;?'p#] DIVIPE 8Y -P[f'#J) JOIN' g )[~N#I1E] / .

;Jif'~)/ \~; .1v;-D":/~=-~(/'--jl-"" "~/I'J/d~':Y.A::' : ttJ~ •

~P[~#-/I1i-] PZVZOE 8/ ('5r~ !5tfc'.s·{) [p#] / -- "- / I -,-/ j , IP· /-. r

',~fi'"~)/~ ~,.J~(V/db·Q~( :Qi.

(( 'S [ 'S 11 J t1.rrJvS ( g r tahv- f'~ =~ ~)[g#] );:JOIIJ3)[stIHlI~ #'

EXT'NP~® 1) [3. X T EN [:) g /1 P P "<$u/<jz0-it / /1q, T/J G .

:z..) (EXTEAlD S /?Pp cz 1/ /lrs <S CLT/ )['3*f~S"';J)"'E.7 S> T/fTU '3~ <g (!~r>'J .

~ R~/J'/1/'? E C I 7/ /9<3 <S C.zT)' : ~~..J J-,~

3) Ex-rENe> (P J(JUJ 'SP) /'lPD C V/EIG-I+T-X-6?TY)

,4S ~HIFwT·

"y $lJHM#/fIX E ~ @

{) '3uNNrl~I~~ 'SP B~ (PyL) /JIJO ~()11 (4f?T)I) /?-!5 ToT~T)'.

2) 5u/1n/l;eI~E

3) ~t//f4N#;eI~E

C-lTY N'3P

c., 7"

c-:L t5 c~ 1.

'SP B;'(3#) /?pp COU/f/T /lg tip.

(p J01N SF) 01 (~IT7) /!Pt? C::OUAlr

/IS NSf·

: ~,,~ ;"')J'(J4 -1.7J~P . -!:> .,. ..

4) 5Vjt1H/!/Ct3E '5p Ell ( f#-) rrPO guff (62r 7) /}g To'[a V / /l-V~ ( (i2 TY) (? <3 ,4V~ 12 V·"

/ /

, 1!/?o-/cJ;.. ofA~ ~Yk-e . , , , ' .J 11 VC; ( Hf)X r t1JN. :ruN to CouNT ; ~))

t " r It J) :,,~ . ~ O? /YlLJ<.--m. ... > ( ~. C(}tf_ ~ '"'\. e.~'? . ) L "1 < a -, 'Yt(,(.qllt.. ~ /' . //

J • ~ ~ ( L ';' tJf;'~ c ,.,UlvT ~..;~ ,

cg U t1 ( g P W H FE ItE '5 -# -:: 3-/. , ~ T Y ) ~ J ~ •

~ ;t,/h ({~ UN G(i!o :.:l , -' GteQUP ((~ I) (re, -:- Ie,., ) x R. ;z ~ ~ \ : tv>:;"... " . "

L .· • •

RDB: S TT (STU? ; STNrJrl'IE; '3TD£r:;"STf1J((' 5 TPEIP) JID" CoT ( ~5Z1P :; CoTITL~ 1 CaT/PIE ;? eRE PIT :J CO P El f;»

5TCoT (ST1,? J COlD, 7,1( ~)';e/;e ~ GRADE)

. ' ,// " (if1~i/ (f») ~ ~ ) (5) J;:J/ J!: ~~((u~~ f

..::-,; / .- ' . : J o~ -:,"" vI I\'r-A't' J;) (;.,~ 't/ Q/~~ if?J)/ ~~J/~~~ ci;J/ -Q:1

n/ (b, / ~ . , ,( 3TcoT) M COT) '" CoTrTL E > ~TID=- N 1\ TIe== 1. /\ 'I~'I~ =. 82.- 8 3'

. fo) o,Z//J}/ 1\1 - 1\'( r--,-> (//J \C/,~~ ,:(;,...) , . .r/ ~,,:J/cJ~~JJ r~ - ~i. n (6 (CSTCOT ~ ~ co T )~ srT) .' ' , < ~TN~HE> co-rrTLE =c./" I~ =-' i/\Y~/K: "BI-€( 1\ G-RflOE <,{Q

. ;' j o~"7" ~j I\,-I\'f J~I (///} cP- ~.J ~ fJ~ ~ ~~)/ ~~?I) ~/--' rV - Q 3

n ((G (COT) ~ 6 . ( S T COT ) ) t>4 STr) < gTNlf ME) <? T rlJ'f<.> CoTYfE~ '-ft' · TI?:;1."'1l?y~::.~g~-8 ·?/ ·

It; J • (. . -. \ ~ t' ~ II . " . /' (.. '. Q 0); -;,l,J M~-I\ '( J L,..",) Di (,)'; ;0 I ~/) U ~~JJ ,J ~tJ/J.r (f ~/) UI~ - L\

n ((n (5 ( ~qTJ) - (n (6 ( ST Tt><l, ~TCOT)\~~ coT) <COlD> COTYP£ - It < $TPf ID;;:'Di'A ') ') < COTITLf) Co l D> Y~YR :: 'Si- f62. '

IV r~ Al-/lr r"~ (;» ; /tl/ ~/ ()~ P b (1.7 ,.J~ -:-J., vJr'// u/ft-- Qc; - - . ,t'? -· ~~IJ~Jr.

n (n (5TR"'2/A7t<y,,~~T-~~J))-T(n(6STtfl~:;:C_f)' <CoTITh) <~rID'COI D > A r;.~ flDf*~O <~r rD> J)

1><\ COT)

: cr// .(f/gl..~/~ if;/) r ~J//-~ • FCltf17-1l.- T (4-) ;:= /J or ~XI$T3 T (NOT -r) • £ )(15T3 T (1-) == NoT ( FoK..4)j,T (!Jor ~))

• F(7reIJJL r ( If) ~/t/[) (f)) = (JoTE'XzrsT5T( NoT(!) oR. /JoT(t)})

• rC'/!l-M- T ( (/-) o£ (1/)) ==- NoTCXE'JT3T(1Jor (I-) /IN'O IJ~T (I'J)

• £XIJrJ 'T ((/) (/~ (fJ) = tJoTPoIYI-LL 7 ( JJoTtl-) /1-#0 /JoT(#V

• EXl1r5 T( (!) /l/l/tJ 1/1)) E /JoT"ct7Rll-iL T ( /JoT (I) (?/? !JoT l,V ., FO/!,L)jL T ( f) =17 EXI~T3 T (I) : /~

. • (If oT '£: XI c; T~ T ( I) ===i7 !JoT Fo IE.I9Lh T (.f-)

.. /

: ru"~ 'l2>'J ] U· .... k~ '-< . ......-/ . ~

\\ ,1-' J / Q ..... ~ ~ .... ~,,J // eXIST! . J t,)J,rIcr .. l b. .. ~c(..iJ. t-t 0,,-- _

\\ i.t tdc-:l fJtJD if / =--..,r; J / Fof<;~LL .#r

. .

l( <sX/ fX, 5fx :7 ~ r;l. cs;,L«~ : (5) ~)/ --:-' LP.;J d IV \:'.,,-f, . , . / P. / -;:J

: ~~)-,£o /)~~." c? JL--- ubJ~ .. /~ : <2,. • S,x .. g #-~ '31( .. f!.rV=" c.~ / /lAID '5x.. 5TOZTuS> 2,0 .

) , ./ 1 - . l~ ~/.... { Q : ~-:v -' P%. ~Vt?,cJ~r: ~. • ~ X· '5IfI7I'1E tt(~ EXrgTS 1$ PX ( 'f PX· ~ #- == 5 X _ gil-

/I/f./p " . 3PX" P #- -:::: Pc." J .

./ //. ~ 1- L -- / 1// / . : 0 tr~~/.//,e€/~ j'1J'P',J dlP~ ~ ,I, : 'tS(~ •

• ~X'~NItM~ ~ EXZg/3 ISfX (~X-~# =- ~P«·.f# /lA/I?

EXIST!? PX ( pX- P:p. = 'S PX- Ptf

/1/f1P )) px .. CoL ore -=-" fk// ~

/ .... I I ' ' J a ~ , ~ / ~ /. ~ /" . : ;:JcF~// S:z. ~ "~r:V~~\J)~,.J d~,L.:.J:.V (t : q:Lt • .

EXl. ~ TS

• :>x- 5N'I1HE ~--VSPx (EXIgT3 '5fY (SX-J1/ -==- 'SPx..g,#­

/tND

:srx-f:p. -=- 3 fi- f# /lAID

SP?, .. g '#- - ~~./ ))

'/-j ,- ~/.I//·/ " . AJtrrl/ -' '::::-bh\~,J d",J ~ (l '. 4Zt;i.

• ~X· fNIJNE tLL~ j-o;t'l2ii Fx (EX~T.J ~?X (~f')("31-=:~X .. Vf /fliP

'$j7x.. p-p =" px .,1'-)

• ~X - g,vIlP113 tttP?:- NoT£X7g-rs FX ( NoTEXL8T3 3FX : t..,.

( '5PX' g-;f- = ~X·g# ##0

, rsrx· Pi/- -== PX· til)) . /. I / ' / ,- /. I /.. /' '" , .

: ;.;; c/ r-j/// fr. reV,J if J?0 "'1! I v : clF6' -

I • ~X· 'Slilit/'1E ~ tJoTEXI'3T~ 'So rX ('!fX. If#- = '5X' f#

I' ~NO ,

j SPX· PI!- -:=-Pt') . 1 C- J..

___ "'~~"'_"..,.A __ ~,-- --

r ~Tr VIJ;J.~ (J~/ ~/ -:oV~~ SICO r CO r ~T ~L-U' ~~~ . . - -. ~ ~ STCo{ -' C't:'T

. ~ ~ j/ c.r).P'j~~:> <.f ~/.J ..;,....;Jj~~/ ~/ ~ - 411 (co-corD' C{},.COTITLE) W/lpv. CO'C/(Er?IT= '2;°;

. =-1 t~<_ .... &'/ 1\'(-1\ f J~} r;/) 'N' 0" ~ ~ if?}; ~p)/ ~ 1;....Jl,u ....:...;//& - G?~ Co. C()TITl~ W/tYb- E X I ~TS s r C.r; . ( 'STC1? ~~I P -; CO· c"rp

F)tlO STc<?· STID -::;' N; liAlP ~7Ct(/ • fie :; '{ .. /?#'O STc<:?· y~>,f(= ~2-g3)

.' ~)o>/-;-" &'; J/ 'c" o/~~ ~..J/~~ I~~i?/) r~ - <?3 ~T· STllflf'1E;. tJ.l.f,Pb- tJ oTEXI~ T$ ~TC'o( Src(?" gT;J)=~T.JrlP

~/!IP

'$ TC 0" C" 1 t;) ':. ' t I )

';')0>./' . - ./ - ,/ . z) - I') • r.1

-: &vI "'(-A 'r' J ~I (/'./) c}l-v1) ~ J' p ,oJ ~~ J" ~ _i ~ }) rv-//.J'li - ~~

'5;. '5Td!9I"1E J ST· 3iHJ{( (.tL~ EXIST::; ~Tco (~T. ')TID= gTC'C"'STI{;)

1Ito. ' " "

/J-N D ~ Teo ·TI'f: :::. 1. t4-rVO 'STCo ~ y;e,Yt< = .g 2- 8"5 r1 tJ 0 E X 15 Tg co

( Co' Col D == :STeo ~ C-o IO AND Co' CoT )'.PE ;:: '1>' ))

Co. COT[71.E 1AU}u,u... Fotef1A. t... ST (.E XI ~T~ ~ Teo C STC~" 'S TIO::: sore 'frIO t1NO ~TC1?' CorD = CO .. COLO

\

AN'£) ~ Teo • T,I( -::: '~~ ,AND -'fTc,(?" y;eYI< == 'i 2 _ i's'/

/IN D '5 T CO, G1'.4P£ ?-1';/,4/,/0 '5T. '5 TH,j If = C"-""'fi'))

~

U/·

C-3

··-~jf)!fb ··

'1'~ ~ ;' ~I ,; 0">/_~ ER ;1..>";

i DBMS" ~I !:'; ~--------------------t DBMS ~ .:.--IJ

r----L.----,

,. I · ..? ; > <.S~,

~----------- --------------------

~----- ..,.hI..>..5~

\)-\

J I -"(

~ '" -.. t.~ <:-

C) '-r 1 ·f·

. -'\:)

l' ~ 1-~ ~ \~~ (, '( ~. \ c­..... \

c. . .~

_____ w_.~ :..!U!.!6i!.t.

'. ~ '. l' \ .: ~L ~. .£, r. J v C. l:' :: t· L~ f ,. ,r Y 'r v ~ c; , v ., ~- , l . .r , t '" ~ I ['\ t , ~- :r - t r--, ~ ,"Vi .[ ~l, l ,,, r (l - 'r. L~ · 'i. . t, 'b-= 'r: ~

l \. r' t ~ l ~ 'l. t,. r .." fi:j

: ' .Ii. , ~~ {. ~. J '"c;~' ~'. . ~ [; ' t t,. 't .[ . . e. (! l' - ~ l r c· V c- C' r-~r r ~~ .... ~ ~ f .r f'~ l: :l 'L ;< f '" . .c:. t ,I:' "l-'- . ., l-'-" ' G. 't .1:' . .. . G,

, . C- r - -, ~ b -, ~ (. ~_ 0 G. 0 • '" , c · [ . t.

\ ~~ ~ f ~-:~ i j~ l l' ~ ~ r r. ,"b 'e. ~- !.. c · ~ v '"v 'l. '"v·t '"v .l r "-. 1-. ~.r ..

r ;. '"... ~,~ 'I . t · c L · G ~ ~- l ~ Co t Y, , 'r (. '. v [- i' '[-' y .t u - - , '" - l · , . c · t -', 'r l' I , ~ l " r t v ~< c:. . C-. -. , t, (. - , l ·r I , . .r · , '" . . [ , ( . , ,~ - , ' . " ~ [ '" t (;" (" y Vi . ' y , ,b 'l. 'L. ~ I:' ~ l .. r" ~ ,I :. ~ .~ I'- v l ,' G. 'e l· i-~ c; c; l" [' r. ~ v · - y 'V r "\ ~ .C- f --l .r

C'" '" I'- '. . tr G....." l ' 't . . 'i-::-' '" c '"-, II "'\ - - - c- ~ , -, ' c ," . u ~. , . , . I:- -'-, t . (, '" . ('Y> I:- .r l' r ~ ;.. ~ ~ -... ~ ,,, ', 'P 'f ( [ 't ' '" '" ". '" ," . ( ~,,,, - 'r: l ,. ,r "'I ' , . ... 0 C'" ~ I C

' , C t . . - c ~ , "C- ·V c. . ,

F' C;. 'I.. t. ,: ' f ,b 't. ,. - .t x- ~ ), ~ t. Y t ,f:" .t v ,,:- 'f ,c' \ ('" , . '\\ b ~ t" tl - " . '1, '" ,..., C7 ''1 r. \ ''1 ~ -( .r r ~ .t ·, '2' ..r. -" (- -i" ~ ~ . n. . "

L ' V ' !) c. r ~ . tl ' v" - v " V '" (,~ 'b-~' ~~. r 0 s: 'r '"-'- ~ 'b. f !) r ~ t \ . 1- '\. 1. G; ' 'Co ~l t , . .' ~:- C-.. • :. ~ V.~ . f'_' L Ii ;a t' '-...:.. . r 'i (, '~ (L' ,~ .- '. " t· !.· ~ c, .- - ':.:.. .t, '!... 'l ' ,t "..:.. ; C- 1 '"..:..:c- -

• (t,... _ '-_ ( , ( . "-" I '-b 1 '..... ~f r- ',,- ,~(~ ~l . t, ' [ , ~.- , '-:- ~\ G ~- " 't , rc " ' () -( 1-, r c" ... ' l • 'l 'l' , .. r ~ L · ~. ( 'r . '~ 7 (" v ( . : . ~ ( . '",:1:- ' " '" r 'i- r. t, ' v r ",- Y ' ' ~ f . r I l ... b- ,l r.. ( . ( . ( . l.' r' 'f . " u ,.!-: _ -... '- . ,l , L ,(. C \I) ' ~.~ (,~ .~ '"[0 .t ~ .t r;'~ ~ '&: r. ') , 't, '), r'· '}; t. [ T}- . },. }- f. '{

'".c... ~ I ~ ... v '" , I [. - ~ ~ ~ '. .C"', t.,. r ~ r r ~ .

~ - -- t I II I I I 11 '--1 ~. ~ >- < '(I C> -11 -t -1~"

. ' ~ I -"t

--.-.. -~. ,---- - .-~.--.- .-- -- ~ .. _-_._- --------_._----... - - '--" .. _---- -------, ,',., ---.. , ..... -_. - - --.... - ---

~S\i;(:J:.gr"' .. I'.\..m!~~:~,:.:~ ".~. : .! t·.'~·.;y -ttt · .... 5iJJ1'*i 5 ..... '.' ..... 6(. .. 11. J. i25!U_W iA

Query Exercises

The remaining exercises are all based on the suppliers-parts-projects database (see Fig. 4.5 in the "Exercises" section in Chapter 4 and the answer to Exercise 5.4 in Chapter 5). In each case you are asked to write a relational algebra express ion for the indicated query. (By way of an interesting variation, you might like to try looking at ome of the answers first and stating what the given ex­pression means in natural language.) For convenience we show the structure of the database (in out­line) below:

S {S/, SNAME, STATUS, CITY} PRIMARY KEY { SI }

P {PI, PNAME, COLOR, WEIGHT, CITY} PRIMARY KEY { PI }

J {J/, JNAME, CITY} PRIMARY KEY { JI }

SPJ {S/, PI, J/, QTY } PRIMARY KEY { S/, PI, J# } FOREIGN KEY { S# } REFERENCES S FOREIGN KEY { PI } REFERENCES P FOREIGN KEY { J# } REFERENCES J

6.13 Get full details of all projects.

6.14 Get full detail s of all projects in London.

6.15 Get supplier numbers for suppliers who suppl y project J I.

6.16 Get all shipments where the quantity is in the range 300 to 750 inclusive.

6.17 Get all part-color/part-city combinations. Note: Here and subsequently, the term "all" is to be taken to mean "all currently represented in the database," not "all poss ible."

6.18 Get all supplier-number/part-number/project-number triples such that the indicated supplier, part, and project are all co located (i .e., al l in the same city).

6.19 Get all supplier-number/part-number/project-number triples such that the indicated suppli er, part, and project are not all colocated.

6.20 Get all supplier-number/part-number/project-number triples such that no two of the indicated supplier, part, and project are colocated.

6.21 Get full details for parts supplied by a supplier in London.

6.22 Get part numbers for parts supplied by a supplier in London to a project in London.

6.23 Get all pairs of city names such that a supplier in the first city supplies a project in the second city.

6.24 Get part numbers for parts supplied to any project by a supplier in the same city as that project.

6.25 Get project numbers for projects supplied by at least one supplier not in the same city.

6.26 Get all pairs of part numbers such that some supplier supplies both the indicated parts.

6.27 Get the total number of projects supplied by supplier S I.

6.28 Get the total quantity of part PI supplied by supplier S I .

6.29 For each part being supplied to a project, get the part number, the project number, and the cor­responding total quantity.

6.30 Get part numbers of parts supplied to some project in an average quantity of more than 350.

6.31 Get project names for projects supplied by supplier S I .

6.32 Get colors of parts supplied by supplier S I .

6.33 Get part numbers for parts supplied to any project in London.

6.34 Get project numbers for projects using at least one part available from supplier S 1.

Q -i.

· ,

~ ·i~··'"

~ i

~ ~

6.35 Get supplier numbers for suppliers supplying at least one part supplied by at least one supplier who supplies at least one red part.

6.36 Get supplier numbers for suppliers with a status lower than that of supplier S 1.

6.37 Get project numbers for projects whose city is first in the alphabetic list of such cities .

6.38 Get project numbers for projects supplied with part PI in an average quantity greater than the greatest quantity in which any part is supplied to project J I .

6.39 Get supplier numbers for suppliers supplying some project with part PI in a quantity greater than the average shipment quantity of part PI for that project.

6.40

6.41

6.42

6.43

6.44

6.45

Get project numbers for projects not supplied with any red part by any London supplier.

Get project numbers for projects supplied entirely by supplier S l.

Get part numbers for parts supplied to all projects in London.

Get supplier numbers for suppliers who supply the same part to all projects.

Get project numbers for projects supplied with at least all parts available from supplier S 1.

Get all cities in which at least one supplier, part, or project is located.

6.46 Get part numbers for parts that are supplied either by a London supplier or to a London project.

6.47 Get supplier-number/part-number pairs such that the indicated supplier does not supply the in­dicated part.

6.48 Get all pairs of supplier numbers, Sx and Sy say, such that Sx and Sy supply exactly the same set of parts each. (Thanks to a correspondent, Fatma Mili of Oakland University, Rochester, Michi-

gan , for this problem. For simplicity, you might want to use the original suppliers and parts database for this exercise, instead of the expanded suppliers-parts-projects database.)

6.49 Get a "grouped" version of all shipments showing, for each supplier-number/part-number pair, the corresponding project numbers and quantities in the form of a binary relation.

6.50 Get an "ungrouped" version of the relation produced in Exercise 6.49.

Q-:L

6.13 J

6.14 J WHERE CITY = • London •

6.15 ( SPJ WHERE J# = JI • J1 • ) ) { sl }

6.16 SPJ WHERE QTY ~ QTY 300 ) AND QTY ~ QTY ( 750 )

6.17 P { COLOR, CITY }

~18 ( S JOIN P JOIN J ) { S/, PI, JI }

6.19 ( ( ( S RENAME CITY AS SCITY TIMES ( P RENAME CITY AS PCITY TIMES ( J RENAME CITY AS JCITY )

WHERE SCITY -+ PCITY OR PCITY -+ JCITY OR JCITY -+ SCITY ) { S/, PI, JI }

6.20 ( ( ( S RENAME CITY AS SCITY TIMES ( P RENAME CITY AS PCITY TIMES ( J RENAME CITY AS JCITY )

WHERE SCITY -+ PCITY AND PCITY -+ JCITY AND JCITY -+ SCITY ) { S/, pi, JI

6.21 P SEMIJOIN ( SPJ SEMIJOIN ( S WHERE CITY = 'London' ) )

6.22 Just to remind you of the possibility, we show a step-at-a-time solution to this exercise:

WITH ( S WHERE CITY = 'London' ( J WHERE CITY = 'London' ( SPJ JOIN T1 ) AS T3, T3 { PI, JI } AS T4, ( T4 JOIN T2 ) AS T5 : T5 { P# }

Here is the same query without using WITH:

AS T1, AS T2,

( ( SPJ JOIN ( S WHERE CITY = 'London' ) ) { PI, J# } JOIN ( J WHERE CITY = 'London' ) ) { P# }

We will give a mixture of solutions (some using WITH, some not) to the remaining exercises.

6.23 ( ( S RENAME CITY AS SCITY ( J RENAME CITY AS JCITY

JOIN SPJ JOIN ) { SCITY, JCITY }

6.24 J JOIN SPJ JOIN S ) { PI }

6.25 ( ( J RENAME CITY AS JCITY ) JOIN SPJ JOIN S RENAME CITY AS SCITY ) )

WHERE JCITY f SCITY } { JI }

6.26 WITH SPJ { S/, PI } RENAME SI AS xs/, PI AS XP/· AS T1, SPJ { SI, PI } RENAME SI AS YS/, PI AS YPI AS T2, T1 TIMES T2 ) AS T3,

( T3 WHERE XSI = YSI AND xpi < YPI ) AS T4 T4 { XP/, YPI }

6.27 ( SUMMARIZE SPJ { s#, JI } PER RELATION { TUPLE { s# SI ( 'Sl' ) } } ADD COUNT AS N ) { N }

We remind you that the expression in the PER clause here is a relation selector invocation (in fact, it is a relation literal) .

Q-~

1

1

6.28 ( SUMMARIZE SPJ { sl, PI, QTY } PER RELATION { TUPLE { 51 5# ( '51' ), P# P# ( 'P1' ) } } ADD SUM ( QTY ) AS Q ) { Q }

6.29 SUMMARIZE SPJ PER 5PJ { P#, JI } ADD SUM ( QTY ) AS Q

6.30 WITH ( SUMMARIZE SPJ PER SPJ { PI, JI } ADD AVG ( QTY ) AS Q ) AS T1,

( T1 WHERE Q > QTY ( 350 ) ) AS T2 T2 { PI }

6.31 J JOIN SPJ WHERE SI = S# 'Sl' { JNAME

6.32 P JOIN SPJ WHERE SI = SI 'Sl' { COLOR }

6.33 SPJ JOIN J WHERE CITY = 'London' ) ) { P# }

6.34 SPJ JOIN SPJ WHERE S# = SI ( 'Sl' { P#

6.35 SPJ JOIN P WHERE COLOR COLOR ( 'Red' ) ) { PI } )

JOIN 5PJ ) { PI } JOIN SPJ

6.36 WITH (S SI, STATUS RENAME SI AS xS/, STATUS AS XSTATUS

( S { S/, STATUS} RENAME SI AS YS/, STATUS AS YSTATUS

T1 TIMES T2 ) AS T3, T3 WHERE XSI = SI ( 'Sl' ) AND

XSTATUS > YSTATUS ) AS T4 : T4 { YSI } .

{ J#

S# } { SI

AS T1,

AS T2,

6.37 ( ( EXTEND J ADD MIN ( J, CITY ) AS FIRST ) WHERE CITY FIRST ) { JI }

What does this query return if relvar J is empty?

6.38 WITH SPJ RENAME J# AS ZJI ) AS T1, T1 WHERE ZJI = JI AND PI = PI ( 'P1' ) ) AS T2, SPJ WHERE PI = pi ( 'P1' ) ) AS T3,

( EXTEND T3 ADD AVG ( T2, QTY ) AS QX AS T4, T4 { J#, QX } AS T5, ( SPJ WHERE JI = JI ( 'J1' ) ) AS T6, ( EXTEND T6 ADD MAX ( T6, QTY ) AS QY ) AS T7, ( T5 TIMES T7 { QY } ) AS T8, ( T8 WHERE QX > QY ) AS T9 : T9 { JI }

6.39 WITH ( SPJ WHERE P# = PI ( 'P1' ) ) AS T1 , T1 { SI, J/, QTY } AS T2,

6.40 WITH

6.41 J {

( T2 RENAME JI AS XJ/, QTY AS XQ ) AS T3, ( SUMMARIZE T1 PER SPJ { JI }

ADD AVG ( QTY ) AS Q AS T4, ( T3 TIMES T4 ) AS T5, ( T5 WHERE XJI = JI AND XQ > Q ) AS T6 : T6 { SI }

( S WHERE CITY = 'London' ) { SI } AS T1, ( P WHERE COLOR = COLOR ( 'Red ' ) ) AS T2, ( T1 JOIN SPJ JOIN T2 ) AS T3 J { JI } MINUS T3 { J I }

J# MINUS ( SPJ WHERE S# 1- SI ( , Sl' ) ) { J# }

6.42 WITH ( ( SPJ RENAME P# AS X ) WHERE X = P# ) { J# } AS T1, ( J WHERE CITY = 'London' ) { J# } AS T2, ( P WHERE T1 ~ T2 ) AS T3 : T3 { P# }

6.43 S { S#, PI } DIVIDEBY J { JI } PER SPJ { s#, PI , J# }

Q-f

6.44 ( J WHERE ( ( SPJ RENAME JI AS Y ) WHERE Y = J# ( SPJ WHERE SI SI ( 'Sl' ) ) { P# }

P# J#

6.45 S { CITY } UNION P CITY UNION J { CITY }

6.46 SPJ JOIN UNION SPJ JOIN

S WHERE CITY

J WHERE CITY

'London'

'London'

{ P#

{ P#

6.47 S TIMES P ) { S/, P# } MINUS SP { S# , P# }

6.48 We show two solutions to this problem.

WITH ( SP RENAME SI AS SA ) { SA, P# } AS T1, 1* T1 {SA,P#} : SA supplies part P# *1

( SP RENAME S# AS SB ) { SB, P# } AS T2, 1* T2 {SB,P/} : SB supplies part PI *1

T1 { SA } AS T3, 1* T3 {SA} : SA supplies some part *1

T2 { SB } AS T4, 1* T4 {SB} : SB supplies some part *1

( T1 TIMES T4 ) AS TS, 1* TS {SA,SB,P#} : SA supplies some part and

SB supplies part P# */

( T2 TIMES T3 ) AS T6, 1* T6 {SA,SB,P/} : SB supplies some part and

SA supplies part P# *1

( T1 JOIN T2 ) AS T7, 1* T7 {SA,SB,P/} : SA and S8 both supply part PI *1

AS Ta, ( T3 TIMES T4 1* T8 {SA,SB} SA supplies some part and

SB supplies some part *1 SP { PI } AS T9, 1* T9 {PI} : part PI is supplied by some supplier *1

( T8 TIMES T9 ) AS T10, 1* T10 {SA,SB,P/} :

SA supplies some part, S8 supplies some part, and part PI is supplied by some supplier *1

( T10 MINUS T7 ) AS T11, 1* T11 {SA,SB,P/} : part PI is supplied,

but not by both SA and S8 *1

( T6 INTERSECT T11 ) AS T12, 1* T12 {SA,S8,P/} : part PI is supplied by SA

but not by SB *1

( TS INTERSECT T11 ) AS T13, 1* T13 {SA,SB,P/} : part PI is supplied by SB

but not by SA */

T12 { SA, 58 } AS T14, /* T14 {SA,SB}

SA supplies some part not supplied by S8 *1

T13 { SA, S8 } AS T1S, /* T1S {SA,SB}

S8 supplies some part not supplied by SA *1

( T14 UNION T1S ) AS T16, /* T16 {SA, 58} : some part is supplied by SA or SB

but not both * /

Q-~

I

I

~

..

TI9

T7 { SA, SB } AS T17, /* T17 {SA,SB} :

some part is supplied by both SA and SB */

( TI7 MINUS TI6 ) AS TIS , /* TIS {SA,SB} :

some part is supplied by both SA and SB , and no part supplied by SA is not supplied by SB, and no part supplied by SB is not supplied by SA -- so SA and SB each supply exactly the same parts */

( T1S WHERE SA < SB ) AS TI9 : /* tidy-up step */

The second solution- which is much more straightforward !-makes use of the relational comparisons introduced in Section 6.9.

WITH S RENAME SI AS SA ) { SA } AS RA , S RENAME SI AS SB ) { SB } AS RB : RA TIMES RB)

WHERE ( SP WHERE SI SA ) { PI ( SP WHERE SI = SB ) { PI

AND SA < SB

6.49 SPJ GROUP ( J/, QTY ) AS JQ

6.50 Let SPQ denote the result of the expression shown in the answer to Exercise 6.49. Then:

SPQ UNGROUP JQ

Q-Y

7.13.13 JX

7.13.14 JX WHERE JX. CITY = 'London'

7.13.15 SPJX. S# WHERE SPJX. J# = JI ( 'J1'

7.13.16 SPJX WHERE SPJX.QTY ~ QTY SPJX.QTY $ QTY

7.13.17 PX.COLOR, PX.CITY

300 750

AND

7.13.18 SX.SI, PX.PI, JX.J# WHERE SX.CITY AND PX.CITY AND JX.CITY

PX.CITY JX.CITY SX.CITY

7.13.19 ( SX. SI, PX. P#, JX. JI ) WHERE SX. CITY f- PX. CITY OR PX.CITY f- JX.CITY OR JX.CITY f- SX.CITY

7.13.20 ( SX. S#, PX ;P#, JX. J# ) WHERE SX. CITY f- PX. CITY AND PX.CITY f- JX.CITY AND JX.CITY f- SX.CITY

7.13.21 SPJX. PI WHERE EXISTS SX ( SX. SI = SPJX. SI AND SX.CITY = 'London' )

7.13.22 SPJX. PI WHERE EXISTS SX EXISTS JX (SX.S# SPJX.SI AND SX.CITY

JX.J# = SPJX.JI AND JX.CITY 'London' AND 'London' )

7.13.23 ( SX. CITY AS SCITY, JX. CITY AS JCITY ) WHERE EXISTS SPJX ( SPJX.S# = SX.SI AND SPJX.J#

7.13.24 SPJX. PI WHERE EXISTS SX EXISTS JX (SX.CITY JX.CITY AND

SPJX.SI SX.SI AND SPJX.J# = JX.J# )

7.13.25 SPJX.J# WHERE EXISTS SX ( SX.CITY f­

SPJX.SI SPJX.JI =

EXISTS JX JX.CITY AND SX.SI AND JX.JI )

7.13.26 ( SPJX. PI AS XP/, SPJY. PI AS YP# ) WHERE SPJX.SI = SpJY.S/ ' AND ' SPJX.p/ ' < , spJy.pl

.I

,7.13.27 COUNT ( SpJX.J# WHERE ' SPJX.S# , = IS# ;C '51' ) ) AS N

7.13.28 SUM ( SPJX WHERE SPJX. SI = SI ( 'Sl' ) AND SPJX.P# = P# ( 'PI' ), QTY ) AS Q

Note: The following "solution" is not correct (why not?) :

SUM ( SPJX.QTY WHERE SPJX.SI = SI ( 'Sl' AND SpJx.PI = PI ,( '~1' ) AS Q

JX.J# )

Answer: Because duplicate QTY values will now be eliminated before the sum is computed.

7.13.29 ( SPJx.P#, SPJx.J#, SUM ( SPJY WHERE spJY.PI spJX.PI

AND spJY.JI SpJX.J#, QTY ) AS Q )

7.13.30 SPJX.PI WHERE AVG ( SPJY WHERE SPJY.P# SPJX.p#

AND SPJY.J# SPJX.J#, QTY ) > QTY ( 350 )

7.13.31 JX.JNAME WHERE EXISTS SPJX SPJX.J# = JX.J# AND SPJX.S# = S# ( 'Sl' ) )

Q-V

7.13.32 PX. COLOR WHERE EXISTS SPJX ( SPJX. PI SPJX.SI

PX.P# AND S# ( 'Sl' ) )

7.13.33 SPJx.P# WHERE EXISTS JX ( JX.CITY = 'London' AND JX.J# = SPJX.J# )

7.13.34 SPJX. J I WHERE EXISTS SPJY SPJX.PI SPJY.PI AND SPJY. SI = SI ( 'Sl' ) )

7.13.35 SPJX. SI WHERE EXISTS SPJY EXISTS SPJZ EXISTS PX ( SPJX.P# = SPJY.P# AND

SPJY.SI = SPJZ.SI AND SPJZ.PI = PX.PI AND PX.COLOR = COLOR ( 'Red' ) )

7.13.36 sX.SI WHERE EXISTS SY ( SY.SI = SI ( 'Sl' ) AND SX.STATUS < SY.STATUS

7.13.37 JX. J I WHERE FORALL JY ( JY. CITY ~ JX. CITY

Or: JX.JI WHERE JX.CITY = MIN ( JY.CITY )

7.13.38 SPJX.JI WHERE SPJX.P# = PI ( 'P1' ) AND AVG ( SPJY WHERE SPJY.P# = PI ( 'P1' )

AND SPJY.JI = SPJX.J#, QTY ) > MAX ( SPJZ.QTY WHERE SPJZ.J# = J# ( 'J1' ) )

7.13.39 SPJX. S# WHERE SPJX.PI = PI ( 'P1' AND SPJX.QTY >

AVG ( SPJY WHERE SPJY.P# AND SPJY.JI

PI ( 'P1' ) SPJX.J#, QTY

7.13.40 JX.J# WHERE NOT EXISTS SPJX EXISTS SX EXISTS PX ( SX.CITY = 'London' AND

PX. COLOR = COLOR ( 'Red' ) AND SPJX.SI SX.SI AND SPJX.PI PX.PI AND SPJX.JI = JX.JI )

7.13.41 Jx.JI WHERE FORALL SPJY ( IF SPJY.J# JX.JI

7.13.42 PX. PI WHERE FORALL JX

THEN SPJY.SI = SI ( 'Sl' ) END IF )

( IF JX.CITY = 'London' THEN EXISTS SPJY (SPJY.PI PX.PI AND

SPJY.JI = JX.J# ) END IF )

7.13.43 SX. SI WHERE EXISTS PX FORALL JX EXISTS SPJY (SPJY.SI SX.SI AND

SPJY.PI PX.PI AND SPJY-.JI = JX.JI )

7.13.44 JX.JI WHERE FORALL SPJY

7.13.45 RANGEVAR VX RANGES OVER

IF SPJY. sl = SI ( 'Sl' ) THEN EXISTS SPJZ SPJZ.JI = JX.J# AND SPJZ.PI = SPJY.P# ) END IF )

( SX.CITY ), ( PX.CITY ), ( JX.CITY )

VX.CITY

7.13.46 SPJX. PI WHERE EXISTS SX

OR EXISTS JX

Q-A

sX.SI = SPJX.SI AND SX.CITY = 'London' ) JX.JI = SPJX.J# AND JX.CITY = 'London' )

7.13.47 ( SX . S/, PX.PI ) WHERE NOT EXISTS SPJX ( SPJX.SI SX.SI AND

SPJX.PI PX.PI )

7.13.48 ( SX.SI AS XS/, SY.S# AS YS# ) WHERE FORALL PZ

( ( IF EXISTS SPJX SPJX.S# SX.S# AND SPJX.P# PZ.PI )

THEN EXISTS SPJY SPJY.SI sY.sl AND SPJY.P# pz.pl )

END IF) AND

( IF EXISTS SPJY SPJY.S# SY.S# AND SPJY.PI PZ.PI )

THEN EXISTS SPJX SPJX.SI SX.SI AND SPJX.PI PZ.PI )

END IF ) )

7.13.49 ( SPJX.S/, SPJX.P/, ( SPJY.J/, SPJY.QTY WHERE SPJY.SI SPJX.SI AND SPJY.PI = SPJX.PI ) AS JQ

'"

7.13.50 Let R denote the result of evaluating the expression shown in the previous solution. Then: , ,'f" j ,I •

RANGEVAR RX RANGES OVER R , . RANGEVAR RY RANGES OVER RX.JQ

( RX.S/, RX.P#, RY.J#, RY.QTY

,', I

;

We are extending the syntax and semantics of <range var definition> slightly. The idea is that the definition of R Y depends on that of RX (note that the two definitions are separated by a comma, not a semicolon, and are thereby bundled into a single operation). See reference [3.3] for further discus­sion.

. ,. '. Y $ - L \ ~JJU/P b/. /.J \.

// _ r /-.: FD .///,..) _.-J.)~ } 4f~~J)JLJ-" -I .

• ~~f' B c II ~ 1I.-:,f3 .' .. u-/'~J'/ b~l; -{ ft)~ff :~

· 4 fI- B _.L 8-~ c" ~ fi ~ C : (a'"J!;;) if ~ "I"-\'- _ r

• .L/ t9 -? B ~ (n, c) -->r (B ,e): cP;;;; 6~ \,- -'f .. 1 Ii - ($,c) ~ II~B ~ h- C : 1";/ .",,\,- -'I'

II if ,4--7 8 '.~ C-7D ~t_ (fl1C)4 (8;D) . =0o#t,- ·~· ~ .. • 4 /I--tB ~ .4'- c: 7ii-- '11--+ '( 6" c) : C ~J,A--f' • -4 fI~8 a-d (B, C)-..?D j~ (fi;C) -'? D :if~~"",,~-,:.~

. . . . .. .' . . . . : .. ~ . . . .

,: (~)(>~ .. .. ~ ~~j HUlA- DIlf(VlEIV ~~)) .. cr~....f~~~b' ·~1\ ".l-f f7~8 a;d C-D 7h<-~ flU(c-fJ)-T (B-, D) j

/ - p /. .... / ) - ') I - • ,..J.-' '--!: u--/J..; ~· -J LM..yJ ~ ' ~ J. \, -?,/ y-". !' ~ ! ' ~ ~ f .

?~JJ.~- ~ ~~~£)~ (",-, F r ~~-cJf~J.J?~j t~~ /. • ..L .' / • r-'

A ) ~"; cr: L"J.-~ 1-, t-- ) & I if' ~J ':' 0 ,,", ~ - """, ~ ~ ~ F :-)

'-!" (C/~':~. (~0 F :)~~)J&-

. ;, lr;.::::.-0 c;, /.;r> W / ; J f:J.L irJ ;., J .::- t;J J,t; J c.. t:- J';, .,<£ t. -"", : "!";

5 ~(;.£j; «~).(),j -11

~+ ~ ~~~~~. ".// ". . ./' , .

/' pot : ~ L~>~ cp £, ,:.j" -,:"f~J / W. V' ~ o .

( .. -:J" ~:~,~ .) ~ ~., j/. / '/ A,\l- ) .{': ~ ·'~ I ~ ~~ -~c: "::' · b.~ ·7J u:) ~ G ,.....,.. ~ ., .;l r«!/ ! ./ _v \.) .# V ; , 0 f- _ .. • _ '" ; v', ,. , .- ' . -' ~ / ("} , - ' I

---!~ ) t-- J~ L.r''J ~ •

\ ) . \ I .· ' - !> t ~ / / , •• - .. ) '1....-1.0 V _ "

" . , J

~)/ "./ j )-?' -:'" 1 -.:-~) t>

(fit) 2"'-f:l 4 =-"-)/v- <Tt~/~ rJu//--r:;ur:(lfpp )1;;" : ..... ~ " .. ' - '

: ~)~ \'~~'f'l r~~ J: )/~~.) V FD~/;'cr; ~) - ~ -g~C -' f?( Il)e.~c , ... ) :~)

C/(Efl7 E ~:sfff2TJoN cTOBFD C- HE C k ( NoTExrgTg ( g, F.L F(" 7' B

rRo/-' f? /'0 P Qt. ... R Cr /I.. Ot) /) / i ·':

I-/lt V IN 6· t1 It X (C):f t1 IfJ ( ( ) )) /

CON&rf?ftItlT CT08FD

fO~ fJLt- R1 ( fO~IiLt. R2 (

IF R~· 8 = Ri· 8 TifEN

Rl · C -= f(i · C )) /

Ju J,, /) .' "-~ II (/h ~~ I /'; ; t"',9 ( )0) U

F :; i 19 ->P (8, c.) ~ B-"'C

{ It -~ B / B --,. C 't9~P (r

f /t~B ~ (A-,8)"-+ C J (rl 1(')-;oD1

: :::"---llj~ ·:"FP /'i~'~!/,<'~ - .

( ,4,C) -D

)3 -..:;,.p E~ C

(t9,c..)~P

(C,p) -->/i

(BJ E)~tJ

I / '"' ) 13 . '-- ~V ~

: 1/1 -:,oj - I

~J - 'I'

. c"/ ) )

I

"v .... (.,., /,,1 /.1';'" l'~j t ' -<' :r:-.;;J . -,/. .v. /' /)

,- . - • - ::t , _ 19 .-.. ( ' C, o? EJ

.r= . 8--,,~1 /" .. ;:= 1 J ;2..' I\, _r_' ( ;4 , D)-'-' E

,'" " c ,__ .f .;, , J - -- -7 I

I ,

~ •• :. ,1./ " .) ~

: ~J:!:,~~ r~' ( /.:) ~'D) fj )~u1( . ( /~' ·,0 fl' DT-3

.... 1 - D ' . ".-, ---;. ..

(ot/grRfflN'T CTOBFD

fO~ fiLl- R1 ( Fof(/itJ. R2 (

IF Ri·a =f(i·B TiiEN RJ.C -= {(i- e )) /

_ J: I' // -+ ,- . 1;;;/ f; : r 'I"? • '~///;;?>J F12_~~(/}JJ-' 11 ~! \;F ~/"

@ F := { !9 -"p (8, c).J B-"' C

{!1 -'lP.B J B -'" C , ;:9~P~

J...Jt,;I) .' \..~11 ; (/ h ~: I l~ ; t"'.9 (' > 0) u

,;9->tB 1 (A-'f3)~-+C I (,47(").-,D1

: =--·· lj~(f.,FP /'~t'~b>/t<'~ - .

: \/ /

/J-)- C / /)~>B : (~/) 11-" I'g?cj /J - I

~ ~D

E~C

( t9,c.)-..+.D - ,

(C,.o)-->/i

(8J£)~ fJ

/ /' (';) Q ( .... ·1 ,, \ . r<'; . ( '- 1 V --",v . /";. 'JV)" --) '- ' I /.,)

: ~~~Ip~ r~' (r=J ~I D) c.J )~iff(· ( ,4 "D f~' DT"'3

( A,c) - D

COHgrR ftltl T CTOBFO

f t)1f f)Lt- ((1 ( f0rr./iU R2 (

1 F Ri· 8 = Ri· B TfiEtI

RJ· C =,Ri- C )) /

: ::"---" /j~·r.,FP fit'~"'/,<'~

J3~D E~C

( t9,c.)~P (c'.D)-~ /1 ( B;E) --.",tl

- .

: 1/) -

(/)) Ii ~c ~J - ~

/4~ P . (/J)

I

: -\.~..1 (,.." /,.. /.I~ t '~. b ' . ..:' r~~ . . :y ....... /' /) . .. :J f f

,' - - ' I. I? -'> (' c,o?t;J -l= . q~1 1",, ;::) ) ~. C' 1\.- .'-'

( ;4 7 D) ~ E , -

i C f t .... I) _.- -"7 ,--

. ,

if/' : ~ J~? ~ r~' t /·f ~1 D) f J ) /-' iff.( · ( .4':i D r- , .. j \r ~~, ,...:} --"y D ' ·

DT"3

1)

:l)

3)

~)

'i

6)

1)

~)

I 9) I

l f

./ DT-4

d _ ,J I~,~ , -

tlYJ? uf~ --'-)~/)(;~)A(.JY" R ( x ,Y) X , ... ) ,.1}) / I)

X -4 -+ Y , y ~ -+ 1- ~ X --~ - .~ A -1'

X~~7 ~ ><~( R(H)-(XIY))

X ~ Y ~ x-",~y

X --..,.~ 'I J -x ~~ ~ -:::;:p X ~-+ ( y, x)

X _~ Y j (Y' ~ ) 0-+-' W -=\>' (~ I X ) -.-'> ( IN' - ( y, ;c ))

X ~~ Y ~ x. £ VI ~ (X, \AI ) -..,......" ( '( I X )

X ~.,,> ( 'I ~ 1- ) ~~ ·X -r )I n Z j X~"" Y - x., X ~-7 Z - 'I

X-+-?' Y j X -J' VI , we!, ' ')I n X == Ii 7~ X---->' VI

: MVDJ~)~~' )~ : .;-fl.))~ .)p.j X ~~ Y LS)...l .. A...o~ ~l; ~I,. R( x, Y, Z) ~I)),)

• tz , t, <.S~L..; ~ ",l-.; ~ljI ~ , (..:.,l..JQ;,J rL..; ),) R 0,) r--f ),) R ~I) ) )..l.io y:>

: cS LS?~ ~ ~~ ~I,) .)p'"j r.; tot j t3 <.S~l; ol{;i , ...G~ t,[X] = t2[X] ~~

tl[X] = t2[X] = t3[X] = 4[X]

qy] = t,[Y]

t,[R( H) - Y] = tz(R(H) - Y]

t.t[Y] = tz[YJ

t.t[R(H) - YJ = t, [R(H) - Y]

t. Y -~NF ~J/ /)h ~//.> ~ •

.,..-A LSIr. .,.sl ..::..-..14NF ),). F LS)L.L~~ , ~l; LS~e- :1, ~~ ~ R ~I) •

) ~ J9I...1.:> (Y c R(H) , X c R(H)) X ~~ Y u)~ ~ F+ ),) ~l; ~Ij

: [RICA 90] ...G~ )) r. .I.J..j c-Oj ,,)

. ~~ ~l.; LS)~~ ~l; ~I, ..5.... X ~~ Y •

+ . F ),) X ~~ Y LS)...;.j..,~ ~I, .)p.", ..:.),.....",),) J I -.:-..14NF ),) R ~I) •

[SIMO 95] ~~ R ~..).jl) ~ X . (Y c R(H) , X c R(H))

~J;Y!J'J)J (~d~./'> IS!?. ()r~.)

(Lf>~y?~>~;j) (

i ~ J~< I) . I • I ~ (I ... u:.~ ' (.)/./.1 U ~/ J/~v ..J --~ t.. -,

( . G . ~ 'lI) -, ,/ J -, - - ~ t- 0 \'- . /. /-"'if,! :&JF /yb'1~-"y/)~J~ /.>o~oJ\a-./J~_\"

I ,pJb 1 J/I r t/' I. Q / 1/ ~ ) 1/ "" ' . , ... \ l r.> / \V \u~ t;~v:~{" ~/j ~ {J.///o (-::-";vJ;~...J> / ~ -, - .-(. .. CfJ,,:J/ ~)L>~·G)~/G.~ ~ )J)OJ I) / w: V ~ ~ _ ~

~ ~ 1.Jr> .-'~

~(!({I/J ~uPj)1;J~:~)/ r:' (v ~)~ U ~ ~) - ~

r: .. if-;))Jh~; l#-~)") b;j; )JE !/£)y~'; -v

-=--- (d, V [~1-.. - J\

~~ (JJ~ ~./'> ~ V -1 ~ ~

(~~ (;; f ~ :'J) J 0 v ( C/J;;/d/ J/. OJ )~/J rjb p.)~ ~ l~ .,. //....-/

( .. ~. (/j~~"(5//.~:"jJ~1~)tSJ;~_H

( I L ~J /) L ~ ~ . ~')' ( )" ~·,6 -I .L ~Y:A/ ~_ ~.)ci-!~ (Q.J .);J~~ ~~.,I)/ - "

(.~/o )~) f) .. · -! !; ,L ._df ~2Jt-rJ ~--sA/ " ~;'rJ;;?(-~J~.//..,.J r1u~J)~J~ ~;>J~~tif.>~ ~/)~ 5~0(J/v;JL/'-U(l ' .-" ~

.~)z;&;~~/;~j,;;J--;-::~ : ~) • t;) • -

5~~tr~~r!jv~G.~) if/\?~ y'

L - / J ' V d. 0!i;./~ ~~/;~J-f;'J ; ifYY)~~

. . .,.; U!) «; vJ/.'i~!J -.J\f-.) I 1/ .. . /

LJ,.\2' L. L~t.; _ j.; : ;.,;. L . ~ /:' J_u' ':({ ,\Isl ~-l

(8)~ ~ourse(1) Query Exercises

The remaining exercises are all based on the suppliers-parts-projects database (see Fig. 4.5 in the "Exercises" section in Chapter 4 and the answer to Exercise 5.4 in Chapter 5). In each case you are asked to write a relational algebra expression for the indicated query. (By way of an interesting variation, you might like to try looking at some of the answers first and stating what the given ex­pression means in natural language.) For convenience we show the structure of the database (in out­line) below:

5 {5#, 5NAME, STATUS, CITY} PRIMARY KEY { SI }

P {PI, PNAME, COLOR, WEIGHT, CITY} PRIMARY KEY { PI }

J {J#, JNAME, CITY } PRIMARY KEY { JI }

SPJ {5#, PI, J#, QTY } PRIMARY KEY { S#, PI, J# } FOREIGN KEY { S# } REFERENCES S FOREIGN KEY { PI } REFERENCES P FOREIGN KEY { JI } REFERENCES J

6:13 Get full details of all projects.

6.14 Get full details of all projects in London.

6.15 Get supplier numbers for suppliers who supply project J I.

6.16 Get all shipments where the quantity is in the range 300 to 750 inclusive:

6.17 Get all part-color/part-city combinations. Note: Here and subsequently, the term "all" is to be taken to mean "all currently represented in the database," not "all possible."

6.18 Get all supplier-numberLpart-number/project-number triples such that the indicated supplier, part, and project are all co located (i.e., all in the same city).

6.19 Get all supplier-nur11ber/part-number/project-number triples such that the indicated supplier, part, and project are not.all C:610cated.

6.20 Get all supplier-number/part-number/project-number triples such that no two of the indicated supplier, part, and project are colocated.

6.21 Get full details for parts supplied by a supplier in London.

6.22 Get part numbers for parts supplied by a supplier in London to a project in London.

6.23 Get all pairs of city names such that a supplier· in the fi·rst city suppl ies a project in the second city.

6.24 Get part numbers for parts supplied to any project by a supplier in the same city as that project.

6.25 Get project numbers for projects. supplied by at least one supplier not in the same city.

6.26 Get all pairs of part numbers such that some supplier supplies both the indicated parts.

6.27 Get the total number of projects supplied by supplier S 1.

6.28 Get the total quantity of part PI supplied by supplier S 1.

6.29 For each part being supplied to a project, get the part number, the project number, and the cor­responding total quantity.

6.30 Get part numbers of parts supplied to some project in an average quantity of more than 350.

6.31 Get project names for projects supplied by supplier S I.

6.32 Get colors of parts supplied by supplier S 1.

6.33 Get part numbers for parts supplied to any project in London.

6.34 get project numbers for projects using at least one part available from supplier S 1.

, -:,' II ..,. .

· . 6.35 Get supplier numbers for suppliers supplying at least one part supplied by at least one supplier who supplies at least one red part.

6.36 Get supplier numbers for suppliers with a status lower than that of supplier S 1.

6.37 Get project numbers for projects whose city is first in the alphabetic list of such cities.

6.38 Get project numbers for projects supplied with part PI in an average quantity greater than the greatest quantity in which any part is supplied to project J 1.

6.39 Get supplier numbers for suppliers supplying some project with part PI iri a quantity greater than the average shipment quantity of part PI for that project.

6.40 Get projec~ numbers for projects not supplied with any red part by any London supplier.

6.41 Get project numbers for projects supplied entirely by supplier Sl.-

6.42 Get part numbers for parts supplied to all projects in London . .

6.43 Get supplier numbers for suppliers who supply the same part toall projects.

6.44 Get project numbers for projects supplied with at least all paris' available from supplier S 1.

6.45 Get all cities in which at least one supplier, part, or project is located.

6.46 Get part numbers for parts that are supplied either by a London supplier or to a London project.

6.47 Get supplier-number/part-number pairs such that the indicated supplier does not supply the in­dicated part.

6.48 Get all pairs of supplier numbers, Sx and Sy say, such that Sx and Sy supply exactly the same set of parts each. (Thanks to a correspondent, Fatma Mili of Oakland University, Rochester, Michi-

gan, for this problem. For simplicity, you might want to use the original suppliers and parts database for this exercise, instead of the expanded suppliers-pacts-projects database.)

6.49 Get a "grouped" version of all shipments showing, for each supplier-number/part-number pair, - the corresponding project numbers and quantities in the form of a binary relation.

6.50 Get an "ungrouped" version of the relation produced in Exercise 6.49.

l

7.15 We have numbered the followin :' ' u lutions as 7. IS .n, where 6.1l is [he number of the original

exercise in Chapter 6.

7.15.13 SELECT * FROM J

Or simpl y:

TABLE J i

7.15.14 SELECT J. * FROM J WHERE J.CITY 'London' i

7.15.15 SELECT DISTINCT SPJ. 5# -FROM SPJ

.. 7.15.16

WHERE SPJ.J# 'Jl' ~ -

SELECT FROM WHERE AND

SPJ.* SPJ SPJ.QTY >= 300 SPJ.QTY <= 750

7.15.17 SELECT DISTINCT P.COLOR, P.CITY FROM P i

7.15.18 SELECT FROM WHERE AND

5.5/, P.P/, J.JI 5, P, J S . CITY P.CITY P.CITY = J.CITY

7.15.19 SELECT S.5/, P.P/, J.J# FROM s, P, J WHERE NOT ( S.CITY = P.CITY AND

P.CITY = J.CITY ) ;

7.15.20 SELECT FROM WHERE AND AND

7.15.21 SELECT V FROM '\ WHERE

7.15.22 SELECT FROM WHERE

AND

S.S/, P.P/, J.JI S, _ P, J S.CITY <> P.CITY P.CITY <> J.CITY J.CITY <> P.CITY

DISTINCT SPJ

SPJ.P#

S.CITY S

( SELECT FROM WHERE S.S# = SPJ.sl )

DISTINCT SPJ.P# SPJ ( SELECT S.CITY

FROM S WHERE S.S# = SPJ.S# SELECT J.CITY FROM J WHERE J.J# = SPJ.JI )

~eA(>_c-f 'P .• r;.-o-,,-,- 'S. .; 2, <::'?1

'London'

'London'

'London'

w4.re ~?~ _. 3 ~ =~. s -w­

fikcl S?-I" ?-:4 = P'?47 I -

fh-d ~cJ1::: L9NDDN ,\ '

7.15.23 SELECT FROM WHERE

DISTINCT S.CITY AS SCITY, J.CITY AS JCITY 5, J

( EXISTS SELECT FROM WHERE AND

SPJ SPJ.S# SPJ.J#

S.S# J.J#

I '

t ••.

7.15.24 SELECT FROM WHERE

7.15.25 SELECT FROM WHERE

DISTINCT SPJ ( SELECT

FROM WHERE SELECT FROM WHERE

DISTINCT SPJ ( SELECT

FROM WHERE SELECT FROM WHERE

SPJ.P#

S.CITY S S.S# = J.CITY J J.J# =

SPJ.J#

S.CITY S S.S# = J.CITY J J.J# =

SPJ.S#

SPJ.J# )

SPJ.S# <>

SPJ.J#

7.15.26 SELECT FROM WHERE AND

DISTINCT SPJX.P# AS SPJ AS SPJX, SPJ AS SPJX.S# SPJY.S# SPJX.P# < SPJY.P# ;

PA, SPJY.P# AS PB SPJY

7.15.27 SELECT COUNT ( DISTINCT SPJ. JI ) AS N FROM SPJ WHERE SPJ.SI = '51'

7.15.28 SELECT ' SUM ( SPJ.QTY FROM SPJ

AS X

WHERE SPJ.SI 'Sl' AND SPJ. pi = 'P1' . i

7.15.29 SELECT SPJ. P#, SPJ. J/, SUM ( SPJ. QTY ) AS Y FROM SPJ GROUP BY SPJ.P/, SPJ.J#

7.15.30 SELECT FROM GROUP HAVING

DISTINCT SPJ.P# SPJ BY SPJ.P#, SPJ.J# AVG ( SPJ.QTY ) >

7.15.31 SELECT DISTINCT J. JNAME FROM J, SPJ

7.15.32

WHERE J.JI = SPJ.J# AND SPJ.SI = '51' ;

SELECT FROM WHERE AND

DISTINCT P.COLOR P, SPJ P.PI = SPJ.PI SPJ.SI = 'Sl' ;

7.15.33 SELECT DISTINCT SPJ. P# FROM SPJ, J WHERE SPJ.J# J.J# AND J.CITY 'London'

7.15.34 SELECT DISTINCT SPJX.J#

350

FROM SPJ AS SPJX, SPJ AS SPJY WHERE SPJX.P# SPJY.P#

7.15.35

AND SPJY.S# = '51' ;

SELECT FROM WHERE AND AND

DISTINCT SPJX.S# SPJ AS SPJX, SPJ AS SPJY, SPJ AS SPJZ SPJX . P# = SPJY.P# SPJY.S# = SPJZ.S# SELECT P.COLOR FROM P WHERE P.P# = SPJZ.P# 'Red'

.~

7.15.36 SELECT S. S# FROM S

..

WHERE S.STATUS < ( SELECT S.STATUS FROM S WHERE S.S# = 'Sl'

7.15.37 SELECT J.J# FROM J WHERE J.CITY SELECT MIN

FROM J) J.CITY )

7.15.38

7.15.39

7.15.40

7.15.41

7.15.42

SELECT FROM WHERE AND (

SELECT FROM WHERE AND

SELECT FROM WHERE

(

SELECT FROM WHERE

(

SELECT FROM WHERE

(

DISTINCT SPJX.J# SPJ AS SPJX SPJX.PI = 'PI' SELECT AVG ( SPJY.QTY FROM SPJ AS SPJY WHERE SPJY.JI = SPJX.J# AND SPJY.P# = 'Pl' ) > SELECT MAX ( SPJZ.QTY ) FROM SPJ AS SPJZ WHERE SPJZ.JI = 'J1'

DISTINCT SPJX.SI SPJ AS SPJX SPJX.PI = 'Pl' SPJX.QTy > ( SELECT

FROM WHERE AND

AVG ( SPJY.QTY ) SPJ AS SPJY SPJY.PI 'P1' SPJY.J# = SPJX.J#

J.JI J NOT ·EXISTS SELECT * FROM SPJ, P, S WHERE SPJ.JI = J.J# AND SPJ.P# = P.P# AND SPJ.S# = S.SI AND. P.COLOR = 'Red' AND S.CITY = 'London'

J.JI J NOT EXISTS SELECT * FROM SPJ· WHERE SPJ.JI = J.JI AND - NOT .f .SPJ.S#

P.PI P NOT EXISTS SELECT * . FROM J

'Sl' ) )

WHERE J.CITY = 'London' AND NOT EXISTS

SELECT * FROM SPJ WHERE SPJ.P# AND SPJ.J#

P.P# J.J# ) )

, .

7.15.43

7.15.44

SELECT FROM WHERE

(

SELECT FROM WHERE

(

S.S# S EXISTS SELECT * FROM P WHERE NOT EXISTS

( SELECT * FROM J WHERE NOT EXISTS

J.J# J NOT EXISTS SELECT *

( SELECT * FROM SPJ WHERE SPJ.S# AND SPJ.P# AND SPJ.J#

FROM SPJ AS SPJX WHERE SPJX.S# = 'Sl' AND NOT EXISTS

SELECT * FROM SPJ AS SPJY

S.S# P.P# J.J# ) ) )

WHERE SPJY.PI SPJX.P# AND SPJY.J# = J.J# ) )

7.15.45 SELECT UNION SELECT UNION SELECT

S.CITY FROM S

P.CITY FROM P

J.CITY FROM J

7.15.46 SELECT FROM WHERE

OR

DISTINCT SPJ ( SELECT

FROM WHERE SELECT FROM WHERE

7.15.47 S.SI, P.PI S, P

SPJ.P#

S.CITY S S.S# = J.CITY J J.J# =

SELECT FROM EXCEPT SELECT FROM

SPJ.S/, SPJ.P# SPJ ;

7.15.48 Solution omitted.

7.15.49-7:15.50 Cannot be done.

SPJ.S# 'London'

SPJ.J# ) 'London'

j _' '> L ' . ........ =.u

? &i~L/.j/dt-~~ SQL L j);<.f'~J -I v : " 1) ;) , J. ~ ~~

;/ • 0 ~./ Lv-. /~~o/ (;: 0

10J J?~~J. ectY/J!", COP£ECO(COID~ PRECOIO) J-'~! --J~. - // Jj (~ . /" 'oj V • _~ - ~ " I' 'L-2-----'" _ ,oJ': .fi.l. iq .. , ~ • ~..>(/ -.....,.) ~, ,,<./).Y J V-/ ,) ---::- ..>!"./ CyJ <-:! if" _ _ '7

:~.JU~ ' _~.ftJ/ \ Co AJ'10 fjJ (J I j~ . ./ -I ' ,., ./,,(-/ ' - . j v -

'-(Lt:.~ O/V-U-/">/~V (/ /..>( l/(/"..> O/ ~ ~ ~l

11

: ()/J~~J ~QL /) ~-) if ~~; ~~)~/ \I~ ~~_/~

I)/.rTH . .'!E£cu;f.SL V£

P£ECoR'3 ( .coro.J Pf(ECOLC)) AiS ( S £L~CT ColD .7 P((ECoIO

~Rot1 CoPteECO

v/ HERE CO 1D ==- \ Cof1??? I

UN10tl fiLL 'S I=LEC r CoPRFCO (Cor))) / CoPREc<J{PRE{Orq)

Fttoi'1 Pf(Eco !23 ~ L o PRE CO

ulHER.£ C D? /:E CO 0 ColO==: PRFlo£g(PKECOIej)

'S ELECT ' * Ffol1 ptfEcoR S O tV D .. r:: rp 8 v .' P t..') or-/ 1..- _ f\.. / Ca! D p , , ,, r:;;Co Tt) /'

, /

P1

\ ~

•..•.......••.•• • •. .. .. ...... .. .. . •. ..........•... ....•. . .. .•.. ......•... ... .......•... ;,; ..........•. ... .... ..... -;.;.1') ..... 1.1 ..... ... . :";/cI u.N~ u--' ~t=--~

...... "01 tT fP ' {{E'C' iJ~~ ~'J i,["£ " ~;fi"'f . c~ tl ~ j)' ./{"$ ............ .

·_·······(-- ·····S··EA··f:···c··r········Ccef?;J)· ...... .

. FRo M · i-o<b.~_ .

·················-u N ·t ·v ·N ·················· ········ ..................................................... .. ............ ............ ................................................... .

··· .. ······ .. ······ .. ··· .. ·· .. · .. g.ELiECl· ...... ····bd·· .. ····tof···r ····P ·-f.4 .... -(;.~~-............ ........ ·-... · ............ .......... ..

.. - ···-····-· .. ··-...... · .. -.. -F·f-Bo·iV/ ........... t-~tt. ....... ~ .... }6-fft.I1'----

............................ .............. :. ·· ...... W·-Jf£ft;;1f5:. .. .. · .. . - ... - ... ~.- ..... ) .. -.......... .......................................................................... ..

... .... .. ·········g·5·li~~·i··· ·········iI;-· · ············ · ·····F·;;,·;;········T~:;t·· ·· .... ; .................. .. ..... ........... ... .. ....... .. ................. .

............ p .. .... ...... ............................ .... ........ ...... .. .................. .. ........... ...... .. . .. .r..':. ... .. .. ............. ........ .... ..

( ~~ ... ~L···7·.· ... ~) ... ~~ ..... J~. ;.r.t'. ~J. .8-£.L.P.L .. r. ... ... ... ...... . r: ... ~!.~,.( ... ( .. r;j/) ,0 If, ,?'-

_mmmm :~F-~~~J:;~;.;j ·· ;;5 · J) · sgZ£Zj .. .. ...... ..... ..... .... ... ............. -... -........... --- ....................... _-. .. ------- ........ -.. -- ...... -... -...... -..... -....... ---.......... -... . -... ........ .... ..... ....... ..... ... ....... .

";JLi

-_ ........... ... ................ . -...... __ .... -- .- ... -...... --- ............ _-- ... ..... ......... - .. __ .... __ .............. ... ... __ .-... __ .. ... .... .. ... .... ----.. _ ... .. . _._ .. _ .. _ ..... ... .. ..... -... _ .............. .

-_._- _ . . .... _-------.. _---_ .. -._---_ .. _ ... ..... _ ..... -.. ---_ .... - . ... . _ ...... -.. .. . ...... .. ......... .. __ ._ ... __ .... __ ._ .. __ ... .... . -... -.--- .... . _-- ... __ .-_ .......... ........... .... -... _--_ ...... -

----_._._-_ ... _ ---_ .. _-_._-_ ..... _-_ ._--- ... ..... _-_.---.. -- .---........ _. . . . . .. ..... . .... .