MySQL数据库技术与项目应用教程(微课版)(AI助学)(第3版)-习题答案 项目6_第1页
MySQL数据库技术与项目应用教程(微课版)(AI助学)(第3版)-习题答案 项目6_第2页
MySQL数据库技术与项目应用教程(微课版)(AI助学)(第3版)-习题答案 项目6_第3页
MySQL数据库技术与项目应用教程(微课版)(AI助学)(第3版)-习题答案 项目6_第4页
MySQL数据库技术与项目应用教程(微课版)(AI助学)(第3版)-习题答案 项目6_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

MySQL数据库技术与项目应用教程(微课版)(AI助学)(第3版)习题答案项目六编程操作网上商城系统数据单选题:BCAADAABDA思考题(参考):使用存储过程有诸多优点,在书中已有描述,那使用存储过程是否存在缺点呢?答:缺点可从以下几方面阐述(1)可移植性差(2)开发调试复杂,由于IDE的问题,存储过程的开发调试比一般程序要更困难(3)SQL本身是结构化查询语言,不是面向对象语言,在复杂业务的处理上会比较吃力。SQL擅长的是数据查询而不是业务逻辑处理,如果把业务逻辑全部放在数据库,违背了这一原则2.触发器在数据库中有很多合适的用途,它在插入、删除或者修改特定表中数据时,会触发一些数据操作,用以维护数据的参照完整性和维护数据安全等。但是我们说使用触发器时,需要特别小心,如果可以使用其他技术手段处理尽量别用触发器,这是为什么?答:MySQL触发器能基于行触发,MySQL触发器始终时基于表中的一条记录触发,而不是一组SQL语句。因此,如果需要变动整个数据集而数据集数据量又较大时,触发器效果会非常低。触发器是由于表中的数据变化,引起另外的数据表中发生相应的数据变化,过多的触发器会导致数据的链式反应,效率很低,甚至造成死循环,因此,使用触发器一定要特别小心。项目实践:#存储函数创建并调用存储函数func_users_count,查询2021年1月1日以后注册的用户总数。delimiter//createfunctionfunc_users_count()returnsintbeginreturn(selectcount(uid)fromusers whereuregtime>='2021-01-01');end//使用SQL语句查看用户自定义函数func_users_count。showfunctionstatuslike'func_users_count';(3)创建并调用存储过程proc_get_integer,输出100以内能够同时被3和5整除的整数。delimiter//createprocedureproc_get_integer()begindeclarecounterintdefault0;whilecounter<=100do ifcounter%3=0andcounter%5=0then selectcounter; endif; setcounter=counter+1; endwhile;end//(4)创建并调用存储过程proc_rand_record,为users表添加10000条测试记录。delimiter//createprocedureproc_rand_record()begindeclareiintdefault0; whilei<10000do insertintousers(ulogin,uName,uPwd) values(fnReturnStr(10),fnReturnStr(6),fnReturnStr(8)); seti=i+1; endwhile;end//(5)创建并调用存储过程proc_user_order,根据指定的uid查询该用户的订单总数。delimiter//createprocedureproc_user_order(userIDint)beginselectcount(oid) fromorders whereuid=userID;end//#(6)删除存储过程proc_user_order。dropprocedureproc_user_order(7)创建存储过程proc_orders_count,统计查询每个用户的订单数。delimiter//createprocedureproc_orders_count()beginselectuid,count(oid) fromorders groupbyuid;end//delimiter//createtriggertrig_order_numafterinsertonordersItemforeachrowbeginupdategoods setgdQuantity=gdQuantity-new.inum,gdSaleQty=gdSaleQty+new.inum wheregid=new.gid;end//(9)创建触发器trig_goods_type,当更改category表中某个类别id时,同时将goods表对应的类别id全部更新。delimiter//createtriggertrig_goods_typeafterupdateoncategoryforeachrowbeginupdategoods setcid=new.cid wherecid=old.cid;end//#(10)在网上商城数据库中,完成销售月报表及日报表功能,统计每月及每日的销售总金额,以及商品#销售总数量。#月报表功能#创建月报表createtablemonthlyReport(mrIDintprimarykeyauto_increment,mrTimevarchar(15)notnullunique,mrAmontfloatnotnullcheck(mrAmont>=0),mrNumintnotnullcheck(mrNum>=0));dropeventevent_monthlyReport;delimiter//createeventevent_monthlyReportonscheduleevery1monthstarts'2021-12-0103:00:10'docallproc_monthlyReport();//delimiter//createprocedureproc_monthlyReport()begindeclareamountfloat;--用于存放上月销售总金额 declarenumint;--用于存放上月销售总数量 --统计上月销售总金额 selectifnull(sum(ototal),0)intoamount fromorders whereyear(otime)=year(DATE_SUB(CURDATE(),INTERVAL1month)) andmonth(otime)=month(DATE_SUB(CURDATE(),INTERVAL1month)); --统计上月销售总数量 selectifnull(sum(odnum),0)intonum fromorderitemodjoinordersoonod.oid=o.oid whereyear(otime)=year(DATE_SUB(CURDATE(),INTERVAL1month)) andmonth(otime)=month(DATE_SUB(CURDATE(),INTERVAL1month));insertintomonthlyReport(mrTime,mrAmont,mrNum) values(DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL1month),'%Y-%m'), amount,num);end//#日报表功能#创建日报表createtabledailyReport(drIDintprimarykeyauto_increment,drDatedatenotnullunique,drAmontfloatnotnullcheck(drAmont>=0),drNumintnotnullcheck(drNum>=0));delimiter//createeventevent_dailyReportonscheduleevery1daystarts'2021-12-0103:00:10'docallproc_dailyReport();//delimiter//createprocedureproc_dailyReport()begindeclareamountfloat;--用于存放昨日销售总金额 declarenumint;--用于存放昨日销售总数量 --统计昨日销售总金额 selectifnull(sum(ototal),0)intoamount fromorders wheredate(otime)=(DATE_SUB(CURDATE(),INTERVAL1day)); --统计昨日销售总数量 selectifnull(sum(odnum),0)intonum fromorderitemodjoinordersoonod.oid=o.oid wheredate(otime)=(DATE_SUB(CURDATE(),INTERVAL1day));insertintodailyReport(drTime,drAmont,drNum) values(DATE_SUB(CURDATE(),INTERVAL1day),amount,num);end//拓展实训:(1)创建并调用存储函数func_poets_count,查询生于公元1000年之后的诗人总数。delimiter//createfunctionfunc_poets_count()returnsintbeginreturn(selectcount(pid) frompoet wherepbirthyear>=1000);end//selectfunc_poets_count();(2)使用SQL语句查看用户自定义函数func_poets_count。showfunctionstatuslike'func_poets_count'(3)创建并调用存储过程proc_poem_dynasty,统计各个朝代所创作的诗歌数量。delimiter//createprocedureproc_poem_dynasty()beginselectpdynasty,count(pmid) frompoemjoinpoetonpoem.pid=poet.pid groupbypdynasty;end//callproc_poem_dynasty();(4)创建并调用存储过程proc_feihualing,查询指定诗歌所包含的所有诗令名称。delimiter//createprocedureproc_feihualing(titlevarchar(50))beginselectfname fromfeihualingjoinpoemlingjoinpoem onfeihualing.fid=poemling.fidandpoemling.pmid=poem.pmid wherepmTitle=title;end//callproc_feihualing('永遇乐·京口北固亭怀古');(5)创建并调用存储过程proc_poem_type,查询指定诗词分类下所有的诗歌的诗歌标题。delimiter//createprocedureproc_poem_type(typevarchar(10))beginselectpmtitle frompoemjoinpoemIndexjoinpoemType onpoem.pmid=poemIndex.pmidandpoemIndex.ptid=poemType.ptid whereptname=type;end//callproc_poem_type('爱国');#(6)创建并调用存储过程proc_poem_count,统计指定诗人所创作的所有诗歌的诗歌数量。delimiter//createprocedureproc_poem_count(poetNamevarchar(30))beginselectcount(pmid) frompoemjoinpoet

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论