Вы находитесь на странице: 1из 10

CHAPTER 7

3. Write the query that will generate a combined list o customers !rom tables C"#T$%ER and
C"#T$%ER&'( that do not include the du)licate customer records. !*ote that only the customer
named +uan $rtega shows u) in both customer tables.(
SELECT CUST_LNAME, CUST_FNAME FROM CUSTOMER
UNION
SELECT CUST_LNAME, CUST_FNAME FROM CUSTOMER_2;
,. Write the query that will generate a combined list o customers to include the du)licate customer
records.
SELECT CUST_LNAME, CUST_FNAME FROM CUSTOMER
UNION ALL
SELECT CUST_LNAME, CUST_FNAME FROM CUSTOMER_2;
-. Write the query that will show only the du)licate customer records.
We have shown both Orace an! MS Access "#er$ %or&ats'
$racle
SELECT CUST_LNAME, CUST_FNAME FROM CUSTOMER
INTERSECT
SELECT CUST_LNAME, CUST_FNAME FROM CUSTOMER_2;
%# Access
SELECT C(CUST_LNAME, C(CUST_FNAME
FROM CUSTOMER AS C, CUSTOMER_2 AS C2
W)ERE C(CUST_LNAME*C2(CUST_LNAME AN+ C(CUST_FNAME*C2(CUST_FNAME;
,eca#se Access !oesn-t s#..ort the INTERSECT S/L o.erator, $o# nee! to 0st on$ the rows 0n wh0ch
a the attr0b#tes &atch(
.. Write the query that will generate only the records that are unique to the C"#T$%ER&' table.
We have shown both Orace an! MS Access "#er$ %or&ats'
$racle
SELECT CUST_LNAME, CUST_FNAME FROM CUSTOMER_2
MINUS
SELECT CUST_LNAME, CUST_FNAME FROM CUSTOMER;
%# Access
SELECT C2(CUST_LNAME, C2(CUST_FNAME
FROM CUSTOMER_2 AS C2
W)ERE C2(CUST_LNAME 1 C2(CUST_FNAME NOT IN
2SELECT C3(CUST_LNAME 1 C3(CUST_FNAME FROM CUSTOMER C34;
,eca#se Access !oesn-t s#..ort the MINUS S/L o.erator, $o# nee! to 0st on$ the rows that are 0n
CUSTOMER_2 that !o not have a &atch0n5 row 0n CUSTOMER(
7. Write the query to show the in/oice number0 the customer number0 the customer name0 the in/oice
date0 and the in/oice amount or all the customers with a customer balance o 120333 or more.
Th0s co&&an! w0 r#n 0n Orace an! 0n MS Access'
SELECT IN6_NUM, CUSTOMER(CUST_NUM, CUST_LNAME, CUST_FNAME, IN6_+ATE, IN6_AMOUNT
FROM IN6OICE INNER 7OIN CUSTOMER ON IN6OICE(CUST_NUM*CUSTOMER(CUST_NUM
W)ERE CUST_,ALANCE8*3999;
4. Write the query that will show the in/oice number0 the a/erage in/oice amount0 and the dierence
between the a/erage in/oice amount and the actual in/oice amount.
There are at east two wa$s to !o th0s "#er$(
SELECT IN6_NUM, A6:_IN6, 2IN6_AMOUNT ; A6:_IN64 AS +IFF
FROM IN6OICE, 2SELECT A6:2IN6_AMOUNT4 AS A6:_IN6 FROM IN6OICE4
:ROU< ,= IN6_NUM, A6:_IN6, IN6_AMOUNT; A6:_IN6
Another wa$ to wr0te th0s "#er$ 0s'
SELECT IN6_NUM, IN6_AMOUNT,
2SELECT A6:2IN6_AMOUNT4 FROM IN6OICE4 AS A6:_IN6,
2IN6_AMOUNT;2SELECT A6:2IN6_AMOUNT4 FROM IN6OICE44 AS +IFF
FROM IN6OICE
:ROU< ,= IN6_NUM, IN6_AMOUNT;
The .rece!0n5 co!e e>a&.es w0 r#n 0n both Orace an! MS Access(
5. Write the query that will write $racle sequences to )roduce automatic customer number and
in/oice number /alues. #tart the customer numbers at 2333 and the in/oice numbers at -333.
The %oow0n5 co!e w0 on$ r#n 0n Orace'
CREATE SE/UENCE CUST_NUM_S/ START WIT) 3999 NOCAC)E;
CREATE SE/UENCE IN6_NUM_S/ START WIT) ?999 NOCAC)E;
23. %odiy the C"#T$%ER table to included two new attributes6 C"#T&7$8 and C"#T&A9E.
Customer 2333 was born on %arch 2-0 25.5 and customer 2332 was born on 7ecember ''0 2577.
In Orace'
ALTER TA,LE CUSTOMER A++ 2CUST_+O, +ATE4 A++ 2CUST_A:E NUM,ER4;
The S/L co!e re"#0re! to enter the !ate va#es 0s'
U<+ATE CUSTOMER
SET CUST_+O, * -3?;MAR;3@A@-
W)ERE CUST_NUM * 3999;
U<+ATE CUSTOMER
SET CUST_+O, * B2;+EC;3@CC-
W)ERE CUST_NUM * 3993;
22. Assuming you com)leted )roblem 230 write the query that would list the names and ages o your
customers.
In Orace'
SELECT CUST_LNAME, CUST_FNAME, ROUN+22S=S+ATE;CUST_+O,4DEA?,94 AS A:E
FROM CUSTOMER;
In MS Access'
SELECT CUST_LNAME, CUST_FNAME, ROUN+22+ATE24;CUST_+O,4DEA?,94 AS A:E
FROM CUSTOMER;
2'. Assuming that the C"#T$%ER table contains a C"#T&A9E attribute0 write the query to u)date
the /alues in this attribute. Hint6 use the results o the )re/ious query.
In Orace'
U<+ATE CUSTOMER
SET CUST_A:E * ROUN+22S=S+ATE;CUST_+O,4DEA?,94;
In MS Access'
U<+ATE CUSTOMER
SET CUST_A:E * ROUN+22+ATE24;CUST_+O,4DEA?,94;
23. Write the query that would list the a/erage age o your customers. !Assume that the C"#T$%ER
table has been modiied to include the C"#T&7$8 and the deri/ed C"#T&A9E attribute.(
SELECT A6:2CUST_A:E4 FROM CUSTOMER;
2,. Write the trigger to u)date the C"#T&8A:A*CE in the C"#T$%ER table when a new in/oice
record is entered. !Assume that the sale is a credit sale.( Test the trigger using the ollowing new
;*<$;CE record6
433-0 23320 ='7>APR>3,=0 ''-.,3
*ame the trigger trg&u)datecustbalance(
CREATE OR RE<LACE TRI::ER TR:_U<+ATECUST,ALANCE
AFTER INSERT ON IN6OICE
FOR EAC) ROW
,E:IN
U<+ATE CUSTOMER
SET CUST_,ALANCE * CUST_,ALANCE 1 'NEW(IN6_AMOUNT
W)ERE CUST_NUM * 'NEW(CUST_NUM;
EN+;
To test the tr055er $o# !o the %oow0n5'
SELECT F FROM CUSTOMER;
INSERT INTO IN6OICE 6ALUES 2G99?,3993,-2C;A<R;9H-,22?(H94;
SELECT F FROM CUSTOMER;
2-. Write a )rocedure to add a new customer to the C"#T$%ER table. "se the ollowing /alues in the
new record6
233'0 ?Rauthor=0 ?Peter=0 3.33
*ame the )rocedure )rc&cust&add. Run a query to see i the record has been added.
CREATE OR RE<LACE <ROCE+URE <RC_CUST_A++
2W_CN IN NUM,ER, W_CLN IN 6ARC)AR, W_CFN IN 6ARC)AR, W_C,AL IN NUM,ER4 AS
,E:IN
INSERT INTO CUSTOMER 2CUST_NUM, CUST_LNAME, CUST_FNAME, CUST_,ALANCE4
6ALUES 2W_CN, W_CLN, W_CFN, W_C,AL4;
EN+;
To test the .roce!#re'
EIEC <RC_CUST_A++23992,-Ra#thor-,-<eter-,9(994;
SELECT F FROM CUSTOMER;
CHAPTER 5
2. #u))ose that you are a manuacturer o )roduct A8C0 which is com)osed o )arts A0 80 and C.
Each time a new )roduct is created0 it must be added to the )roduct in/entory0 using the
PR$7&@$H in a table named PR$7"CT. And each time the )roduct A8C is created0 the )arts
in/entory0 using PART&@$H in a table named PART0 must be reduced by one each o )arts A0 80
and C. The sam)le database contents are shown in Table P5.2
Table P9.1 The Database for Problem 1
Table name6 PR$7"CT Table name6 PART
PR$7&C$7E PR$7&@$H PART&C$7E PART&@$H
A,C 3,29? A ?AC
, H@G
C ?H@
9i/en this inormation0 answer questions a>e.
a. How many database requests can you identiy or an in/entory u)date or both PR$7"CT and PARTA
There are two correct answers H or 2( +e.en!0n5 0n how the S/L state&ents are !one(
b. "sing #@:0 write each database request you ha/e identiied in )roblem 2.
The !atabase re"#ests are shown 0n the %oow0n5 tabe(
Bour #@: statements Two #@: statements
U<+ATE <RO+UCT
SET <RO+_/O) * <RO+_O/) 1 3
W)ERE <RO+_CO+E * BA,C-
U<+ATE <ART
SET <ART_/O) * <ART_O/) ; 3
W)ERE <ART_CO+E * BA-
U<+ATE <ART
SET <ART_/O) * <ART_O/) ; 3
W)ERE <ART_CO+E * B,-
U<+ATE <ART
SET <ART_/O) * <ART_O/) ; 3
W)ERE <ART_CO+E * BC-
U<+ATE <RO+UCT
SET <RO+_/O) * <RO+_O/) 1 3
W)ERE <RO+_CO+E * BA,C-
U<+ATE <ART
SET <ART_/O) * <ART_O/) ; 3
W)ERE <ART_CO+E * BA- OR
<ART_CO+E * B,- OR
<ART_CO+E * BC-
c. Write the com)lete transaction!s(.
The transact0ons are shown 0n the %oow0n5 tabe(
Bour #@: statements Two #@: statements
,E:IN TRANSACTION
U<+ATE <RO+UCT
SET <RO+_/O) * <RO+_O/) 1 3
W)ERE <RO+_CO+E * BA,C-
U<+ATE <ART
SET <ART_/O) * <ART_O/) ; 3
W)ERE <ART_CO+E * BA-
U<+ATE <ART
SET <ART_/O) * <ART_O/) ; 3
W)ERE <ART_CO+E * B,-
U<+ATE <ART
SET <ART_/O) * <ART_O/) ; 3
W)ERE <ART_CO+E * BC-
COMMIT;
,E:IN TRANSACTION
U<+ATE <RO+UCT
SET <RO+_/O) * <RO+_O/) 1 3
W)ERE <RO+_CO+E * BA,C-
U<+ATE <ART
SET <ART_/O) * <ART_O/) ; 3
W)ERE <ART_CO+E * BA- OR
<ART_CO+E * B,- OR
<ART_CO+E * BC-
COMMIT;
d. Write the transaction log0 using Table 5.2 as your tem)late.
We ass#&e that .ro!#ct BA,C- has a <RO+_/O) * 2E at the start o% the transact0on an! that the transact0on 0s
re.resent0n5 the a!!0t0on o% 3 new .ro!#ct( We aso ass#&e that <ART co&.onents JAK, J,K an! JCK have a
<RO+_/O) e"#a to ?A, 32, an! H? res.ect0ve$(
TR:
;7
TRC
*"%
PRE<
PTR
*ECT
PTR $PERAT;$* TA8:E
R$W
;7 ATTR;8"TE
8EB$RE
<A:"E
ABTER
<A:"E
3 3AE NULL 2 START FFSTART
TRANSACTION
2 3AE 3 E U<+ATE <RO+UCT BA,C- <RO+_/O) 2E 2H
E 3AE 2 H U<+ATE <ART BA- <ART_/O) ?A ??
H 3AE E ? U<+ATE <ART B,- <ART_/O) 32 33
? 3AE H A U<+ATE <ART BC- <ART_/O) H? HH
A 3AE ? NULL COMMIT FF EN+
TRANSACTION
e. "sing the transaction log you created in #te) d0 trace its use in database reco/ery.
The te>t-s Tabe @(3E 0s the te&.ate %or the .robe& so#t0on( Use the so#t0on to .robe& 3! as the 0n.#t se5&ent(
CHAPTER 23
!2( At #ite C6
a. #E:ECT D
BR$% C"#T$%ERE
Th0s S/L se"#ence re.resents a remote request.
b. #E:ECT D
BR$% ;*<$;CE
WHERE ;*<&T$TA: F 2333E
Th0s S/L se"#ence re.resents a remote request(
c. #E:ECT D
BR$% PR$7"CT
WHERE PR$7&@$H G 23E
Th0s S/L se"#ence re.resents a distributed request( Note that the !0str0b#te! re"#est 0s re"#0re! when a s0n5e re"#est &#st
access two +< s0tes( The <RO+UCT tabe 0s co&.ose! o% two %ra5&ents, <RO_A an! <RO+_,, wh0ch are ocate! 0n s0tes A
an! ,, res.ect0ve$(
d. 8E9;* W$RHE
"P7ATE C"#T$%ER
#ET C"#&8A:A*CE I C"#&8A:A*CE J 233
WHERE C"#&*"%IK2353.KE
;*#ERT ;*T$ ;*<$;CE!;*<&*"%0 C"#&*"%0 ;*<&7ATE0 ;*<&T$TA:(
<A:"E# !K54.352K0 K2353.K0 ?2->BE8>'33'=0 233(E
;*#ERT ;*T$ ;*<:;*E!;*<&*"%0 PR$7&C$7E0 :;*E&PR;CE(
<A:"E# !K54.352K0 K23'3K0 233(E
"P7ATE PR$7"CT
#ET PR$7&@$H I PR$7&@$H > 2
WHERE PR$7&C$7E I K23'3KE
C$%%;T W$RHE
Th0s S/L se"#ence re.resents a distributed request(
Note that U<+ATE CUSTOMER an! the two INSERT state&ents on$ re"#0re re&ote re"#est ca.ab00t0es( )owever, the ent0re
transact0on &#st access &ore than one re&ote +< s0te, so we aso nee! !0str0b#te! transact0on ca.ab00t$( The ast U<+ATE
<RO+UCT state&ent accesses two re&ote s0tes beca#se the <RO+UCT tabe 0s !0v0!e! 0nto two %ra5&ents ocate! at two
re&ote +< s0tes( There%ore, the transact0on as a whoe re"#0res !0str0b#te! re"#est ca.ab00t$(
e. 8E9;* W$RHE
;*#ERT C"#T$%ER!C"#&*"%0 C"#&*A%E0 C"#&A77RE##0 C"#&8A:(
<A:"E# !K3,'23K0K<ictor E)hanorK0 K2'3 %ain #tK0 3.33(E
;*#ERT ;*T$ ;*<$;CE!;*<&*"%0 C"#&*"%0 ;*<&7ATE0 ;*<&T$TA:(
<A:"E# !K54.,3,K0 K3,'23K0 ?23>A"9>2555=0 '.33(E
C$%%;T W$RHE
Th0s S/L se"#ence re.resents a distributed transaction( Note that, 0n th0s transact0on, each 0n!0v0!#a re"#est re"#0res on$
re&ote re"#est ca.ab00t0es( )owever, the transact0on as a whoe accesses two re&ote s0tes( There%ore, !0str0b#te! re"#est
ca.ab00t$ 0s re"#0re!(
At #ite A6
. #E:ECT C"#&*"%0 C"#&*A%E0 ;*<&T$TA:
BR$% C"#T$%ER0 ;*<$;CE
WHERE C"#T$%ER.C"#&*"% I ;*<$;CE.C"#&*"%E
Th0s S/L se"#ence re.resents a distributed request( Note that the re"#est accesses two +< s0tes, one oca an! one re&ote(
There%ore !0str0b#te! ca.ab00t$ 0s nee!e!(
g. #E:ECT D
BR$% ;*<$;CE
WHERE ;*<&T$TA: F 2333E
Th0s S/L se"#ence re.resents a remote request, beca#se 0t accesses on$ one re&ote +< s0te(
h. #E:ECT D
BR$% PR$7"CT
WHERE PR$7&@$H G 23E
Th0s S/L se"#ence re.resents a distributed request( In th0s case, the <RO+UCT tabe 0s .art0t0one! between two +< s0tes, A an!
,( Atho#5h the re"#est accesses on$ one re&ote +< s0te, 0t accesses a tabe that 0s .art0t0one! 0nto two %ra5&ents' <RO+;A an!
<RO+;,( A single request can access a )artitioned table only i the 78%# su))orts distributed requests.
At #ite 86
i. #E:ECT D
BR$% C"#T$%ERE
Th0s S/L se"#ence re.resents a remote request(
L. #E:ECT C"#&*A%E0 ;*<&T$TA:
BR$% C"#T$%ER0 ;*<$;CE
WHERE ;*<&T$TA: F 2333 A*7
C"#T$%ER.C"#&*"% I ;*<$;CE.C"#&*"%E
Th0s S/L se"#ence re.resents a distributed request(
M. #E:ECT D
BR$% PR$7"CT
WHERE PR$7&@$H G 23E
Th0s S/L se"#ence re.resents a distributed request( 2See e>.anat0on %or .art h(4
'. The CUSTOMER tabe &#st be .art0t0one! hor0Lonta$ b$ state( 2We show the .art0t0ons 0n the answer to Ec(4
3. 9i/en the scenario and the requirements in Problem '0 answer the ollowing questions6
a( What reco&&en!at0ons w0 $o# &aMe re5ar!0n5 the t$.e an! character0st0cs o% the re"#0re! !atabase
s$ste&N
The Ma5aL0ne <#b0sh0n5 Co&.an$ re"#0res a !0str0b#te! s$ste& w0th !0str0b#te! !atabase ca.ab00t0es( The !0str0b#te!
s$ste& w0 be !0str0b#te! a&on5 the co&.an$ ocat0ons 0n So#th Caro0na, :eor50a, For0!a, an! Tennessee(
The ++,MS &#st be abe to s#..ort !0str0b#te! trans.arenc$ %eat#res, s#ch as %ra5&entat0on trans.arenc$, re.0ca
trans.arenc$, transact0on trans.arenc$, an! .er%or&ance trans.arenc$( )etero5eneo#s ca.ab00t$ 0s not a &an!ator$ %eat#re
s0nce we ass#&e there 0s no e>0st0n5 +,MS 0n .ace an! that the co&.an$ wants to stan!ar!0Le on a s0n5e +,MS(
b. What ty)e o data ragmentation is needed or each tableA
The !atabase &#st be hor0Lonta$ .art0t0one!, #s0n5 the STATE attr0b#te %or the CUSTOMER tabe an! the RE:ION
attr0b#te %or the IN6OICE tabe(
c. What must be the criteria used to )artition each databaseA
The %oow0n5 %ra5&entat0on se5&ents re%ect the cr0ter0a #se! to .art0t0on each !atabase'
HoriNontal Bragmentation o the C"#T$%ER Table 8y #tate
Bragment *ame :ocation Condition *ode name
C3 Tennessee CUS_STATE * OTNO NAS
C2 :eor50a CUS_STATE * O:AO ATL
CE For0!a CUS_STATE * OFLO TAM
CH So#th Caro0na CUS_STATE * OSCO C)A
HoriNontal Bragmentation $ the ;*<$;CE Table 8y Region
Bragment
*ame
:ocation Condition *ode name
I3 Tennessee RE:ION_CO+E * OTNO NAS
I2 :eor50a RE:ION_CO+E * O:AO ATL
IE For0!a RE:ION_CO+E * OFLO TAM
IH So#th Caro0na RE:ION_CO+E * OSCO C)A
d. 7esign the database ragments. #how an eOam)le with node names0 location0 ragment names0 attribute names0 and
demonstration data.
Bragment C2 :ocation6 Tennessee *ode6 *A#
C"#&*"% C"#&*A%E C"#&A77RE## C"#&C;TP C"#&#TATE C"#&#"8&7ATE
39GGH 7a&es +( ,#r5er 32E Co#rt Aven#e Me&.h0s TN G;+EC;93
39@@E L0sa ,( ,arnette @39 Ea5e Street Nashv0e TN 32;MAR;92
Bragment C' :ocation6 9eorgia *ode6 AT:
C"#&*"% C"#&*A%E C"#&A77RE## C"#&C;TP C"#&#TATE C"#&#"8&7ATE
33GGC :0nn$ E( Stratton EE? Ma0n Street Atanta :A 33;AU:;93
3E??G Anna )( Ar0ona A?C Mason Ave( +aton :A 2E;7UN;93
Bragment C3 :ocation6 Blorida *ode6 TA%
C"#&*"% C"#&*A%E C"#&A77RE## C"#&C;TP C"#&#TATE C"#&#"8&7ATE
3993H 7ohn T( Ch0 H?A ,rent Aven#e M0a&0 FL 3G;NO6;93
3?@@G L0sa ,( ,arnette 2EH Ra&aa Street Ta&.a FL 2E;MAR;92
Bragment C, :ocation6 #outh Carolina *ode6 CHA
C"#&*"% C"#&*A%E C"#&A77RE## C"#&C;TP C"#&#TATE C"#&#"8&7ATE
23?A2 Tho&as F( Matto H? N( <ratt C0rce Chareston SC 2;+EC;93
3GCCA Mar$ ,( S&0th ?2A ,oone <0Me Chareston SC 2G;OCT;93
Bragment ;2 :ocation6 Tennessee *ode6 *A#
;*<&*"% RE9;$*&C$7E C"#&*"% ;*<&7ATE ;*<&T$TA:
23EEH2 TN 39GGH 3;NO6;93 H?(@?
29@@GC TN 39@@E 3?;FE,;92 H?(@?
Bragment ;' :ocation6 9eorgia *ode6 AT:
;*<&*"% RE9;$*&C$7E C"#&*"% ;*<&7ATE ;*<&T$TA:
3@GG@E :A 33GGC 3?;AU:;93 C9(H?
22HEH? :A 3E??G 3;7UN;93 H?(@?
Bragment ;3 :ocation6 Blorida *ode6 TA%
;*<&*"% RE9;$*&C$7E C"#&*"% ;*<&7ATE ;*<&T$TA:
299@3? FL 3993H 3;NO6;93 H?(@?
2E33HG FL 3?@@G 3;MAR;92 2H(@?
Bragment ;, :ocation6 #outh Carolina *ode6 CHA
;*<&*"% RE9;$*&C$7E C"#&*"% ;*<&7ATE ;*<&T$TA:
2HEE32 SC 23?A2 3?;NO6;93 H?(@?
2E33?A SC 3GCCA 3;OCT;93 H?(@?
e. What ty)e o distributed database o)erations must be su))orted at each remote siteA
To answer th0s "#est0on, we &#st %0rst !raw a &a. o% the ocat0ons, the %ra5&ents at each ocat0on, an! the t$.e o%
transact0on or re"#est s#..ort re"#0re! to access the !ata 0n the !0str0b#te! !atabase(
*ode
Bragment *A# AT: TA% CHA Headquarters
CUSTOMER C3 C2 CE CH
IN6OICE I3 I2 IE IH
+0str0b#te! O.erat0ons Re"#0re! none none none none !0str0b#te! re"#est
:0ven the .robe&Os s.ec0%0cat0ons, we conc#!e that no 0nterstate access o% CUSTOMER or IN6OICE !ata 0s re"#0re!(
There%ore, no !0str0b#te! !atabase access 0s re"#0re! 0n the %o#r no!es( For the hea!"#arters, the &ana5er wants to be abe to
access the !ata 0n a %o#r no!es thro#5h a s0n5e S/L re"#est( There%ore, the ++,MS &#st s#..ort !0str0b#te! re"#ests(
%( What t$.e o% !0str0b#te! !atabase o.erat0ons &#st be s#..orte! at the hea!"#arters s0teN
See the answer %or .art e(