Sie sind auf Seite 1von 11

NUNStAOnlineBookExchange StudentsattheNationalUniversityofNgendipura(NUN)buybooksfortheirstudies.Theyalsolendand borrowbooksfromotherstudents.Yourcompany,ApasajaPt.Ltd.,iscommissionedbyNUNStudents Association(NUNStA)todesignandimplementanonlinebookexchangesystemforitsstudents. ApasajaPt.Ltd.designsandimplementsadatabaseapplicationthatrecordsinformationabout students,bookstheyownandbookstheyborrowfromotherstudents. Thedatabaserecordsthename,faculty,departmentandstudentnumberofeachstudent.Eachstudent isidentifiedinthesystembyitsemail.Thedatabasealsorecordsthedateatwhichthestudentjoined theuniversity. Thedatabaserecordsthetitle,authors,publisher,yearandeditionandtheISBN10andISBN13for eachbook.TheInternationalStandardBookNumber,ISBN10or13,isanindustrystandardforthe uniqueidentificationofbooks.Itispossiblethatthedatabaserecordsbooksthatarenotownedbyany students(becausetheownersofacopygraduatedorbecausethebookwasadvisedbyalecturerfora coursebutnotyetpurchasedbyanystudent.) Thedatabaserecordsthedateatwhichabookcopyisborrowedandthedateatwhichitisreturned.

Wewillrefertothisinformationasaloanrecord. Forhistoricalpurposesthedatabaserecordsinformationaboutthecopiesandtheownersofcopiesas longastheownersarestudentsorthereareloanrecordsconcerningthecopies. Forhistoricalpurposesthedatabaserecordsinformationaboutgraduatedstudentsaslongasthereare loanrecordsconcerningbooksthattheyowned. Forhistoricalpurposes(inordertokeeptheloanrecordsforthisbook)thedatabaserecordsthecaseof abookthatwasownedandsubsequentlysoldunlessthecopywasneverborrowed.

CREATETABLEbook( titleVARCHAR(256)NOTNULL, formatCHAR(9)CHECK(format='paperback'ORformat='hardcover'), pagesINT, languageVARCHAR(32), authorsVARCHAR(256), publisherVARCHAR(64), yearDATE, ISBN10CHAR(10)NOTNULLUNIQUE, ISBN13CHAR(14)PRIMARYKEY ) CREATETABLEstudent( nameVARCHAR(32)NOTNULL, emailVARCHAR(256)PRIMARYKEY, yearDATENOTNULL, facultyVARCHAR(62)NOTNULL, departmentVARCHAR(32)NOTNULL, graduateDATE, CHECK(graduate>=year) ) CREATETABLEcopy( ownerVARCHAR(256)REFERENCESstudent(email)ONUPDATECASCADEONDELETECASCADE, bookCHAR(14)REFERENCESbook(ISBN13)ONUPDATECASCADE, copyINTCHECK(copy>0), availableBITNOTNULLDEFAULT'TRUE', PRIMARYKEY(owner,book,copy) ) CREATETABLEloan( borrowerVARCHAR(256)REFERENCESstudent(email), ownerVARCHAR(256) bookCHAR(14), copyINT, borrowedDATE, returnedDATE, FOREIGNKEY(owner,book,copy)REFERENCEScopy(owner,book,copy)ONUPDATECASCADEON DELETECASCADE, PRIMARYKEY(borrowed,borrower,owner,book,copy), CHECK(returned>=borrowed) )

Tutorial1 1) Createthetablethatcontainsthefollowinginformationaboutbooks:title,format(paperback orhardcover),numberofpages,authors,publisher,year,edition,ISBN10and13.Checkout onebookontheWeb,forinstanceonamazon.com,toseesomeexamplesofthevaluesofthese attributes.CheckouttheWebfortheavailableSQLdomains(types)andtrythemwithyour databasemanagementsystem.Chooseaprimarykey.ForbidNULLvalues. CREATETABLEbook( titleVARCHAR(128)NOTNULL, formatCHAR(9)CHECK(format=paperbackORformat=hardcoverORformatISNULL), pagesINT, authorsVARCHAR(128), publisherVARCHAR(32), yearDATE, ISBN10CHAR(10)NOTNULLUNIQUE, ISBN13CHAR(14)PRIMARYKEY, ) 2) Printalltheinformationaboutbooks. SELECT*FROMbook 3) Deletetherelationbook. DROPTABLEbook 4) Recreatethetablebooks. CREATETABLEbook( titleVARCHAR(128)NOTNULL, formatCHAR(9)CHECK(format=paperbackORformat=hardcoverORformatISNULL), pagesINT, authorsVARCHAR(128), publisherVARCHAR(32), yearDATE, ISBN10CHAR(10)NOTNULLUNIQUE, ISBN13CHAR(14)PRIMARYKEY, ) 5) InsertonebookcalledIntroductiontoDatabaseSystems.GototheWebtofindactualdetails. INSERTINTObookVALUES('IntroductiontoDatabaseSystems','Paperback',168,'Stephane BressanandBarbaraCatania','MacGrawHill','20050101','0071246509','9780071246507') 6) InserthalfadozenbooksyoucanfindwithtitlecontainingIntroductiontoDatabaseSystems orauthoredbyC.J.Date.GototheWeb,forinstanceamazon.com,tofindthedetails. INSERTINTObookVALUES('AnIntroductiontoDatabaseSystems','Paperback',1024,'C.J.Date', 'AddisonWesley','20030801','0321197844','9780321197849')

7)

8)

9)

10)

11)

12)

13)

INSERTINTObookVALUES('IntroductiontoDatabaseSystems(ManagementInformation Systems)','Paperback',29,'R.Dixon,G.Rawlings','CIMAPublishing','19981201', '0948036230','9780948036231') INSERTINTObookVALUES('IntroductiontoDatabaseSystems','Hardcover',650,'BipinC. Desai','WestGroup','19900801','0314667717','9780314667717') INSERTINTObookVALUES('SQLandRelationalTheory:HowtoWriteAccurateSQLCode', 'Paperback',432,'C.J.Date','OReillyMedia','20090123,'0596523068','9780596523060') ModifyalltheBooksauthoredbyC.J.Datetomentiontheauthorsfirstname(findtheauthors firstnamefromtheWeb.) UPDATEbookSETauthors=ChristopherJ.DateWHEREauthors=C.J.Date Printalltheinformationaboutbooks SELECT*FROMbook DeleteallthebooksauthoredbyC.J.Date DELETEFROMbook WHEREauthors=ChristopherJ.Date DELETEFROMbook WHEREauthorsLIKEC%Date Findthetitle,format,numberofpages,authors,publisher,year,edition,ISBN10and13ofthe books. SELECTtitle,format,pages,authors,publisher,year,edition,ISBN10,ISBN13FROMbook Findthetitlesofthebooks. SELECTtitleFROMbook SELECTDISTINCTtitleFROMbook FindtheauthorsofthebookscalledIntroductiontoDatabaseSystems. SELECTauthorsFROMbookWHEREtitle=IntroductiontoDatabaseSystems Addalanguageattributetoallbooks.SetthedefaultlanguagetoEnglish. ALTERTABLEbook ADDlanguageVARCHAR(32)DEFAULTEnglish

INSERTINTObook(title,format,pages,authors,publisher,year,ISBN10,ISBN13)VALUES('SQL andRelationalTheory:HowtoWriteAccurateSQLCode','Paperback',432,'C.J.Date','OReilly Media','20090123','0596523068','9780596523060') 14) CreatethetablesfortheremainderofthistutorialusingthecodeinNUSStASchema.sql. PopulatethetablesusingNUSStAData.sql.YoucanuseNUSStAClean.sqltoremovealldataand tables.ThefilesareavailableinIVLEworkbin.

Tutorial2queries,duplicates,referentialintegrity,multiplerelationsandconditionsandcomplex conditions,UNION. Alwaysaskyourselfthequestionaboutduplicatespotentiallycreatedbythequerywithorwithout DISTINCT.OnlyuseDISTINCTwhenstrictlynecessary. 1) CreatethetablesusingthefileNUNStASchema.sql. Rearrangethestatements 2) PopulatethedatabaseusingthefilesNUMNStABook.sql,NUMNStACopy.sql,NUMNStALoan.sql andNUMNStAStudent.sql. Checktheforeignkeyconstraintstodecideinwhichordertoinsertthedata. 3) YoucancleanupdataandtablesanytimeusingthefileNUNStAClean.sql. 4) Findthedifferentemailsofstudents SELECTs.email FROMstudents 5) Findthedifferentemailsofstudents SELECTs.email FROMstudents DISTINCTisnotneeded 6) Printthenamesofstudentsindescendingalphabeticalorder. SELECTs.name FROMstudents ORDERBYnameDESC 7) Aretherestudentswiththesamename? SELECT*FROMstudents1,students2WHEREs1.name=s2.nameANDs1.email<s2.email 8) Findthedifferentnamesofstudents.Istheresultsorted? SELECTDISTINCTs.name FROMstudents ThesortedresultisasideeffectoftheduplicateeliminationpossiblyspecifictotheDBMS.Itis notguaranteed. 9) Findthenamesofstudentswhoownedacopyofbook9780262033848.

10)

11)

12)

13)

SELECTs.name FROMstudents,copyc WHEREc.owner=s.emailANDc.book=9780262033848 Donotusetablebookasitisguaranteedbyreferentialintegritythatthereissuchabookifit appearsintablecopy. Findthenamesofstudentswhoownedacopyofbookwhosetitlecontainscomputerwith morethan100pages. SELECTs.name FROMstudents,copyc,bookb WHEREc.owner=s.emailANDc.book=b.ISBN13ANDb.titleLIKE%computer%ANDb.pages> 100 FindthenumberofA4pagesneededtophotocopythetwobookswithISBN13'978 0262033848'and'9780321295354'(ifyouownthebooksandforpersonaluseonly!) SELECT(b1.pages+b2.pages)/2 FROMbookb1,bookb2 WHEREb1.ISBN13='9780262033848'ANDb2.ISBN13='9780321295354' Findthedifferentnamesofstudentswhoownedacopyofbookotherthat9780262033848. SELECTDISTINCTs.name FROMstudents,copyc WHEREc.owner=s.emailANDc.book<>9780262033848 ThisisnotPrintthenamesofthedifferentstudentswhy? Whatisthemeaningofthefollowingquery?SELECTs.nameFROMstudents,copycWHERE NOT(c.owner=s.emailANDc.book<>9780262033848) SELECTDISTINCTs.name FROMstudents,copyc WHEREc.owner<>s.emailORc.book<>9780262033848 Thisprintsallthestudents!

14) Findthenamesofstudentswhoborrowedacopyofbook9780262033848. SELECTs.name FROMstudents,loanl WHEREl.borrower=s.emailANDl.book=9780262033848 15) Findthenamesofstudentswhoownedorborrowedacopyofbook9780262033848.Use UNION.

SELECTs.name FROMstudents,copyc WHEREc.owner=s.emailANDc.book=9780262033848 UNION SELECTs.name FROMstudents,loanl WHEREl.borrower=s.emailANDl.book=9780262033848 16) Findthenamesofstudentswhoownedorborrowedacopyofbook9780262033848.USEOR. SELECTs.name FROMstudents,copyc,loanl WHERE(c.owner=s.emailANDc.book=9780262033848) OR(l.borrower=s.emailANDl.book=9780262033848) 17) Deleteallthedataintablecopy DELETEFROMcopy 18) Tryagainqueries(12)and(13) WeseethatthequerywithORreturnsnoresults.Thisiswrong.Thatmeansthatthequerywas notcorrect.IthappensbecausetheCartesianproductintheFROMclauseisempty.Whenisit correcttouseOR?ORshouldbeusedamongconditionsontheexactlysametables.

Tutorial3Aggregatefunctions,nestedqueries,viewsandtriggers. 1) Findthenumberofcopiesinthesystem. SELECTCOUNT(*) FROMcopyc 2) Find,foreachbook,thecorrespondingnumberofcopies.[Printtheprimarykeyofthebookand thenumberofcopies.] SELECTc.book,COUNT(*) FROMcopyc GROUPBYc.book 3) Findtheavailablebookswiththelargestnumberofcopies. SELECTc.book FROMcopyc WHEREc.available='TRUE' GROUPBYc.book HAVINGCOUNT(*)>=ALL(SELECTCOUNT(*)FROMcopycWHEREc.available='TRUE'GROUPBY c.book) 4) FindthenamesofthestudentsthathaveborrowedsomebookbyCharlesDickens. SELECTs.name FROMstudents,loanl,bookb WHEREl.borrower=s.emailANDl.book=b.ISBN13ANDb.authors='CharlesDickens' 5) FindthenumberofdifferentbooksbyCharlesDickens. SELECTCOUNT(DISTINCTs.email) FROMstudents,loanl,bookb WHEREl.borrower=s.emailANDl.book=b.ISBN13ANDb.authors='CharlesDickens' Notethatwedonotneedthecopytablebytransitivityofthereferentialintegrity. 6) FindthenamesofthedifferentstudentsthathaveborrowedallthebooksbyCharlesDickens. Useaggregatefunctions. SELECTs.name FROMstudents,loanl,bookb WHEREl.borrower=s.emailANDl.book=b.ISBN13ANDb.authors='CharlesDickens' GROUPBYs.name,s.email HAVINGCOUNT(DISTINCTb.ISBN13)=(SELECTCOUNT(*) FROMbookb WHEREb.authors='CharlesDickens')

7) Findthenamesofstudentswhoownedacopyofbookwhosetitlecontainscomputerwith morethan100pages.Usenestedqueries.Thisisnotthepreferredanswer. SELECTs.name FROMstudents,copyc WHEREc.owner=s.emailANDc.bookIN( SELECTb.ISBN13FROMbookbWHEREb.titleLIKE'%computer%'ANDb.pages>100) SELECTs.name FROMstudents WHEREs.emailIN( SELECTc.ownerFROMcopycWHEREc.bookIN( SELECTb.ISBN13FROMbookbWHEREb.titleLIKE'%computer%'ANDb.pages>100)) SELECTs.name FROMstudents,copyc,bookb WHEREs.email=c.ownerANDc.book=b.ISBN13ANDb.titleLIKE'%computer%'ANDb.pages <100 8) Findthedifferentnamesofstudentswhoneverownedacopyofbookotherthat978 0262033848. SELECTDISTINCTs.name FROMstudents WHEREs.emailNOTIN(SELECTc.ownerFROMcopycWHEREc.book='9780470128725') 9) FindthenamesofthedifferentstudentsthathaveborrowedallthebooksbyCharlesDickens. UseNOTEXISTS.(YoumayalsotrywithNOTINandEXCEPT.) SELECTs.name FROMstudents WHERENOTEXISTS (SELECT* FROMbookb WHEREb.authors=CharlesDickensANDNOTEXISTS (SELECT* FROMloanl WHEREl.book=b.ISBN13ANDl.borrower=s.email)) 10) FindthenamesofthedifferentstudentsthathaveborrowedallthebooksbyAmelie Nothomb. Thereisnosuchbooksoeverystudenthasborrowedallherbooks!Ornone? SELECTs.name FROMstudents,loanl,bookb WHEREl.borrower=s.emailANDl.book=b.ISBN13ANDb.authors='AmelieNothomb'

GROUPBYs.name,s.email HAVINGCOUNT(DISTINCTb.ISBN13)=(SELECTCOUNT(*) FROMbookb WHEREb.authors='AmelieNothomb') SELECTs.name FROMstudents WHERENOTEXISTS (SELECT* FROMbookb WHEREb.authors='AmelieNothomb'ANDNOTEXISTS (SELECT* FROMloanl WHEREl.book=b.ISBN13ANDl.borrower=s.email))

Das könnte Ihnen auch gefallen