5s

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

CHAPTER5AdvancedSQLPracticeExercises5.1Describethecircumstancesinwhichyouwouldchoosetouseembed-dedSQLratherthanSQLaloneoronlyageneral-purposeprogramminglanguage.Answer:WritingqueriesinSQListypicallymucheasierthancodingthesamequeriesinageneral-purposeprogramminglanguage.HowevernotallkindsofqueriescanbewritteninSQL.Alsonondeclarativeactionssuchasprintingareport,interactingwithauser,orsendingtheresultsofaquerytoagraphicaluserinterfacecannotbedonefromwithinSQL.Undercircumstancesinwhichwewantthebestofbothworlds,wecanchooseembeddedSQLordynamicSQL,ratherthanusingSQLaloneorusingonlyageneral-purposeprogramminglanguage.EmbeddedSQLhastheadvantageofprogramsbeinglesscomplicatedsinceitavoidstheclutteroftheODBCorJDBCfunctioncalls,butrequiresaspecializedpreprocessor.5.2WriteaJavafunctionusingJDBCmetadatafeaturesthattakesaResult-Setasaninputparameter,andprintsouttheresultintabularform,withappropriatenamesascolumnheadings.Answer:publicclassResultSetTableimplementsTabelModel{ResultSetresult;ResultSetMetaDatametadata;intnumcols;ResultSetTable(ResultSetresult)throwsSQLException{this.result=result;metadata=result.getMetaData();numcols=metadata.getColumnCount();for(inti=1;i=numcols;i++){System.out.print(metadata.getColumnName(i)+‘‘‘‘);12Chapter5AdvancedSQL}System.out.println();while(result.next()){for(inti=1;i=numcols;i++){System.out.print(result.getString(metadata.getColumnName(i)+‘‘‘‘));}System.out.println();}}}5.3WriteaJavafunctionusingJDBCmetadatafeaturesthatprintsalistofallrelationsinthedatabase,displayingforeachrelationthenamesandtypesofitsattributes.Answer:DatabaseMetaDatadbmd=conn.getMetaData();ResultSetrs=dbmd.getTables();while(rs.next()){System.out.println(rs.getString(‘‘TABLENAME’’);ResultSetrs1=dbmd.getColumns(null,‘‘schema-name’’,rs.getString(‘‘TABLENAME’’),‘‘%’’);while(rs1.next()){System.out.println(rs1.getString(‘‘COLUMNNAME’’),rs.getString(‘‘TYPENAME’’);}}5.4Showhowtoenforcetheconstraint“aninstructorcannotteachintwodifferentclassroomsinasemesterinthesametimeslot.”usingatrigger(rememberthattheconstraintcanbeviolatedbychangestotheteachesrelationaswellastothesectionrelation).Answer:FILL5.5Writetriggerstoenforcethereferentialintegrityconstraintfromsectiontotimeslot,onupdatestosection,andtimeslot.NotethattheoneswewroteinFigure5.8donotcovertheupdateoperation.Answer:FILL5.6Tomaintainthetotcredattributeofthestudentrelation,carryoutthefollowing:a.Modifythetriggeronupdatesoftakes,tohandleallupdatesthatcanaffectthevalueoftotcred.b.Writeatriggertohandleinsertstothetakesrelation.Exercises3c.Underwhatassumptionsisitreasonablenottocreatetriggersonthecourserelation?Answer:FILL5.7ConsiderthebankdatabaseofFigure5.25.Letusdefineaviewbranchcustasfollows:createviewbranchcustasselectbranchname,customernamefromdepositor,accountwheredepositor.accountnumber=account.accountnumberSupposethattheviewismaterialized;thatis,theviewiscomputedandstored.Writetriggerstomaintaintheview,thatis,tokeepitup-to-dateoninsertionstoanddeletionsfromdepositororaccount.Donotbotheraboutupdates.Answer:Forinsertingintothematerializedviewbranchcustwemustsetadatabasetriggeronaninsertintodepositorandaccount.Weassumethatthedatabasesystemusesimmediatebindingforruleexecution.Fur-ther,assumethatthecurrentversionofarelationisdenotedbytherelationnameitself,whilethesetofnewlyinsertedtuplesisdenotedbyqualifyingtherelationnamewiththeprefix–inserted.Theactiverulesforthisinsertionaregivenbelow–definetriggerinsertintobranchcustviadepositorafterinsertondepositorreferencingnewtableasinsertedforeachstatementinsertintobranchcustselectbranchname,customernamefrominserted,accountwhereinserted.accountnumber=account.accountnumberdefinetriggerinsertintobranchcustviaaccountafterinsertonaccountreferencingnewtableasinsertedforeachstatementinsertintobranchcustselectbranchname,customernamefromdepositor,insertedwheredepositor.accountnumber=inserted.accountnumberNotethatiftheexecutionbindingwasdeferred(insteadofimmediate),thentheresultofthejoinofthesetofnewtuplesofaccountwiththesetofnewtuplesofdepositorwouldhavebeeninsertedbybothactiverules,leadingtoduplicationofthecorrespondingtuplesinbranchcust.Thedeletionofatuplefrombranchcustissimilartoinsertion,exceptthatadeletionfromeitherdepositororaccountwillcausethenaturaljoinoftheserelationstohavealessernumberoftuples.Wedenotethenewly4Chapter5AdvancedSQLdeletedsetoftuplesbyqualifyingtherelationnamewiththekeyworddeleted.definetriggerdeletefrombranchcustviadepositorafterdeleteondepositorreferencingoldtableasdeletedforeachstatementdeletefrombranchcustselectbranchname,customernamefromdeleted,accountwheredeleted.accountnumber=account.accountnumberdefinetriggerdeletefrombranchcustviaaccountafterdeleteonaccountreferencingoldtableasdeletedforeachstatementdeletefrombranchcustselectbranchname,customernamefromdepositor,deletedwheredepositor.accountnumber=deleted.accountnumber5.8ConsiderthebankdatabaseofFigure5.25.WriteanSQLtriggertocarryoutthefollowingaction:Ondeleteofanaccount,foreachowneroftheaccount,checkiftheownerhasanyremainingaccounts,andifshedoesnot,deleteherf

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功