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

CSE444MidtermExam

October29,2007

Name_______SampleSolution___________________



Question1 /40

Question2 /30

Question3 /30

Total /100

CSE444Midterm,Oct.29,2007 Page1of8

Question1.SQL(40points)Considerthefollowingschemaforadatabasethatcontainsinformation
aboutbooks,authors,andpublishers.

BOOK(isbn,title,publisher,year)
AUTHOR(ssn,name,city)
PUBLISHER(name,city)
WROTE(isbn,ssn)

Everybookhasauniqueisbn(standardizedbooknumber)andeveryauthorhasauniquessn.Tokeep
theproblemsimpler,wewillassumethatauthorshavesinglenames(likeCher,Bono,orKnuth),
andwewillalsoassumethateachpublisherhasauniquenameandislocatedinasinglecity.The
WROTErelationshipconnectsauthorssnswiththeisbnsofbooksthattheywrote,eitherasasingle
authororasoneofseveralcoauthors.

(a)WriteaSQLquerythatgivesthetitlesandyearsofallbookswrittenbytheauthornamedRowling.

SELECTb.title,b.year
FROMBookb,Authora,Wrotew
WHEREb.isbn=w.isbn
ANDw.ssn=a.ssn
ANDa.name='Rowling';

(continuednextpage)

CSE444Midterm,Oct.29,2007 Page2of8
(b)WriteaSQLquerythatgivestheuniquenamesofallauthorswhohavewrittenabookwhereoneof
thebooksauthorshasthenameUllman(i.e.,Ullmanandallofhiscoauthorsonallofhisbooks).

SELECTDISTINCTcoauthors.name
FROMAuthora,Wrotew,WrotecoauthorsWrote,Authorcoauthors
WHEREa.name='Ullman'
ANDa.ssn=w.ssn
ANDw.isbn=coauthorsWrote.isbn
ANDcoauthorsWrote.ssn=coauthors.ssn;

(c)WriteaSQLquerythatliststhenamesofallpublishersinthecityofBostonandthenumberofbooks
thattheypublishedinthedecadebeginningin1990andendingin1999.

SELECTp.name,COUNT(*)
FROMPublisherpLEFTOUTERJOINBookbONp.name=b.publisher
WHEREb.yearBETWEEN1990AND1999
ANDp.city='Boston'
GROUPBYp.name;

(continuednextpage)

CSE444Midterm,Oct.29,2007 Page3of8
(d)WriteoneormoreSQLstatementstoaddthefollowingbooktothisdatabase:

AlgorithmsforSmartiesbyKnuth,AddisonWesley,1968,ISBN=03211742X

YoumayassumethattherealreadyareentriesforKnuthintheAUTHORtableandforAddisonWesley
inthePUBLISHERtable,andyoumayassumethatthereisonlyoneauthornamedKnuthintheAUTHOR
table.

INSERTINTOBook(isbn,title,publisher,year)
VALUES('03211742X','AlgorithmsforSmarties','AddisonWesley',1968);

INSERTINTOWrote(isbn,ssn)
SELECT'03211742X',ssn
FROMAuthor
WHEREname='Knuth';

CSE444Midterm,Oct.29,2007 Page4of8
Question2.E/RDiagrams(30points)WandasWidgetWorks(akaWWW,Inc.)needsyourhelp
designingadatabasetokeeptrackoftheflowofpartsinthefactory.Everyparthasanitemnumber,a
suppliername,andabinnumberwhereitiskeptinthefactory.Widgetsarebuiltfrompartsandalso
fromotherwidgets,whichareusedassubassemblies.Eachwidgethasanitemnumberandabin
number,justlikeapart.Partsuppliershavenamesandaddressesand,tokeeptheproblemsimple,we
willassumethatsuppliernamesareuniqueandaddressesareasinglestring.

(a)GiveanE/Rdiagramforthisapplication.Stateanyassumptionsyouneedtomake.

(continuednextpage)

CSE444Midterm,Oct.29,2007 Page5of8
(b)WriteappropriateCREATETABLEstatementsforSQLtablestostoretheinformationinyourE/R
diagramfrompart(a).Youshouldchooseverysimpleatomicdatatypesfortheattributes.Indicateall
keys,andincludeanyforeignkeyconstraintsneededtopreservetheintegrityofthedata.

CREATETABLESubAssembly
(
item_noINTPRIMARYKEY
bin_noINTNOTNULL
);

CREATETABLESupplier
(
namevarchar(125)PRIMARYKEY,
addrvarchar(max)NOTNULL
);

CREATETABLEWidget
(
item_noINTPRIMARYKEYREFERENCESSubAssembly(item_no)
);

CREATETABLEPart
(
item_noINTPRIMARYKEYREFERENCESSubAssembly(item_no),
supplier_idINTREFERENCESSupplier(name)
);

CREATETABLEBuilt_From
(
widget_noINTREFERENCESWidget(item_no),
componentINTREFERENCESSubAssembly(item_no)
);

CSE444Midterm,Oct.29,2007 Page6of8
Question3.(30points)Someofyouroldhighschoolfriendshaveopenedanewrestaurant,andithas
becomesowildlysuccessfulthattheyneedacomputertokeeptrackofdinnerreservations.Not
knowingallthatmuchaboutdatabases,theyhavecreatedasingletabletoholdreservation
information:

RESERVATION(Date,Time,Name,Phone,VIP)

Somecustomershaveonlymadeasinglereservation,butmanyofthemhavemultiplereservationsin
thetable.Notwocustomershavethesamenameandphonenumber,butsomedifferentcustomers
haveeitherthesamenameorsamephonenumber(butnotboth).VIPisabooleanthatistrueto
indicatetheverybestcustomers,whoreceiveextraspecialservice.

(a)Whatarethepossiblekey(s)andsuperkeys(s)forthisrelation?Whichofthesepossibilitiesforma
minimalkeyforthisrelation?Justifyyouranswerintermsoffunctionaldependenciesandclosures.

TheonlynontrivialfunctionaldependencyinthetableisNamePhoneVIP.Bothofthesets
{Date,Time,Name,Phone,VIP}and{Date,Time,Name,Phone}aresuperkeys,becausetheirclosures
includealloftheattributes,butonly{Date,Time,Name,Phone}isaminimalkey.

(continuednextpage)

CSE444Midterm,Oct.29,2007 Page7of8
(b)IdentifyanybadfunctionaldependenciesintheRESERVATIONtableandusethemtodecomposeit
intorelationsthatareinBoyceCodeNormalForm(BCNF).Theresultingrelationsshouldretainallof
theinformationintheoriginaltable(i.e.,itshouldbealosslessdecomposition).

ThenontrivialF.D.NamePhoneVIPviolatesBCNFbecause{Name,Phone}isnotasuperkeyforthe
RESERVATIONrelation.SowedecomposetheoriginalRESERVATIONtableintothefollowingtwo
tables:

RESERVATION(Date,Time,Name,Phone)
CUSTOMER(Name,Phone,VIP)

BothoftheserelationsareinBCNFandtheycontainalloftheinformationintheoriginaltable.

CSE444Midterm,Oct.29,2007 Page8of8