版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
-----创建序列createsequencebook_idINCREMENTBY1--每次加几个STARTWITH001--从1开始计数NOMAXVALUE--不设置最大值NOCYCLE--一直累加,不循环CACHE10;------创建books表createtablebooks(books_idvarchar2(1000),books_namevarchar2(100),pricenumber,qtynumber,pubvarchar2(200));------修改books表的字段altertablebooksmodify(books_idnumber)-------------往books表中插入数据insertintobooksvalues(book_id.nextval,'中国文学1',39,12,'人民文学');insertintobooksvalues(book_id.nextval,'中国文学2',30,32,'人民文学');insertintobooksvalues(book_id.nextval,'中国文学3',59,22,'清华大学');insertintobooksvalues(book_id.nextval,'中国文学4',33,52,'清华大学');insertintobooksvalues(book_id.nextval,'中国文学5',99,62,'电子工业');-----------跟新books中的信息updatebookssetprice=100wherebooks_id=1----------按出版社分组查询每个出版社金额的情况selectpub,sum(price*qty)frombooksgroupbypub----------按出版社、书籍名称分组查询每个出版社金额的情况selectpub,books_name,sum(price*qty)frombooksgroupbypub,books_name----------按出版社、书籍名称分组查询每个出版社金额的情况>50selectpub,books_name,sum(price*qty)frombooksgroupbypub,books_namehavingsum(price)>50----------查询相同出版社的记录数selectpub,count(pub)frombooksgroupbypubhavingcount(pub)>1-----标的内链接selecteid,ename,six,namefrome,dwherea.id=d.idselecteid,ename,six,namefromejoindona.id=d.id-----做外连接selecteid,ename,six,namefromejoindona.id=d.id(+)----右外连接selecteid,ename,six,namefromejoindona.id(+)=d.id----无关子查询select*fromewhereidin(selecteidfromd)----相关子查询select*fromewhereidin(selecteidfromdwhereid=d.idandid='003')select*fromewhereidnotin(selecteidfromdwhereid=d.idandid='003')-----存在则显示select*fromewhereexists(selectidfromdwhereid=d.id)-----不存在则显示select*fromewherenotexists(selectidfromdwhereid=d.id)-----------------------PLSQL基本语法----------------------------------------------------------------------------------------------------------setserveroutputonsize10000declarexvarchar2(100);beginx:='Thisis....';DBMS_OUTPUT.PUT_LINE('xvalueis'||x);end;-----ifelsifelsedeclareanumber;bvarchar2(10);begina:=2;ifa=1thenb:='A';elsifa=2thenb:='B';elseb:='C';endif;DBMS_OUTPUT.put_line(b);end;----------------casedeclareanumber;bvarchar2(10);begina:=2;casewhena=1thenb:='A';whena=2thenb:='B';endcase;DBMS_OUTPUT.put_line(b);end;-------------------------PLSQL循环--------------------------------------------------loopdeclarexnumber;beginx:=1;loopx:=x+1;ifx>3thenexit;endif;DBMS_OUTPUT.put_line(x);endloop;DBMS_OUTPUT.put_line(x);end;--------------whiledeclarexnumber;beginx:=1;whilex>3loopx:=+1;DBMS_OUTPUT.put_line(x);endloop;DBMS_OUTPUT.put_line(x);end;-------forbeginforxin1..10loop------从小到大DBMS_OUTPUT.put_line(x);endloop;DBMS_OUTPUT.put_line('endofforloop');end;beginforxinreverse1..10loop------从大到小DBMS_OUTPUT.put_line(x);endloop;DBMS_OUTPUT.put_line('endofforloop');end;----------------做标签declarexnumber;beginx:=0;<<repeat_loop>>x:=x+1;DBMS_OUTPUT.put_line(x);ifx<3thengotorepeat_loop;endif;end;----------------exception处理-------------------------------------declaretestvarchar2(100);beginselectbooks_nameintotestfrombookswherebooks_id=1;DBMS_OUTPUT.put_line(test);exceptionwhenno_data_foundthenDBMS_OUTPUT.put_line('没有找到数据');end;-----------自定义异常declaretestvarchar2(100);eexception;beginselectbooks_nameintotestfrombookswherebooks_id=1;iftest<>'中国文学1'thenraisee;endif;DBMS_OUTPUT.put_line(test);exceptionwhenethenDBMS_OUTPUT.put_line('不是需要的书籍名称');end;-----------------------记录的声明-------------------------------declaretypemyrecordisrecord(bnamevarchar2(100),bpubvarchar2(100));real_recordmyrecord;beginselectbooks_name,pubintoreal_recordfrombookswherebooks_id=1;DBMS_OUTPUT.put_line(real_record.bname||real_record.bpub);end;declaretypemyrecordisrecord(bnamebooks.books_id%type,---------------声明的字段和表中的字段类型一样bpubvarchar2(100));real_recordmyrecord;beginselectbooks_name,pubintoreal_recordfrombookswherebooks_id=1;DBMS_OUTPUT.put_line(real_record.bname||real_record.bpub);end;declaremyrecordbooks%rowtype;beginselect*intomyrecordfrombookswherebooks_id=1;DBMS_OUTPUT.put_line(myrecord.books_name||myrecord.pub);end;-----------------------游标------------------------显示游标的使用方法declarecursormycursorisselect*frombooks;myrecordbooks%rowtype;beginopenmycursor;fetchmycursorintomyrecord;whilemycursor%foundloopDBMS_OUTPUT.put_line(myrecord.books_name||myrecord.pub);fetchmycursorintomyrecord;endloop;closemycursor;end;------带参数的游标declarecursormycursor(bookidnumber)isselect*frombookswherebooks.books_id=bookid;myrecordbooks%rowtype;beginopenmycursor(1);fetchmycursorintomyrecord;whilemycursor%foundloopDBMS_OUTPUT.put_line(myrecord.books_name||myrecord.pub);fetchmycursorintomyrecord;endloop;closemycursor;end;------使用for做游标的循环declarecursormycursor(bookidnumber)isselectbooks_namefrombookswherebooks.books_id=bookid;beginforcurinmycursor(1)loopDBMS_OUTPUT.put_line(cur.books_name);endloop;end;----isopendeclarebooknamebooks.books_name%type;cursormycursor(booksidnumber)isselectbooks_namefrombookswherebooks_id=booksid;beginifmycursor%isopenthenDBMS_OUTPUT.put_line('cursorisopened');elseopenmycursor(1);endif;fetchmycursorintobookname;closemycursor;dbms_output.put_line(bookname);end;-------rowcountdeclarebooknamebooks.books_name%type;cursormycursorisselectbooks_namefrombooks;beginopenmycursor;loopfetchmycursorintobookname;exitwhenmycursor%notfoundormycursor%notfoundisnull;DBMS_OUTPUT.put_line(mycursor%rowcount);endloop;closemycursor;end;-----游标跟新数据declarecursormycursorisselectbooks_namefrombooksforupdate;textvarchar2(100);beginopenmycursor;fetchmycursorintotext;whilemycursor%foundloopupdatebookssetbooks_name=books_name||'_t'wherecurrentofmycursor;fetchmycursorintotext;endloop;closemycursor;end;----------------隐式游标不需要声明beginforcurin(selectbooks_namefrombooks
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 初中七年级地理“东南亚”区域地理教学设计:基于区域认知与人地协调的深度学习实践
- 锯齿剥绒工操作安全模拟考核试卷含答案
- 小学五年级信息科技《猜球结果逻辑推》教学设计
- 工艺染织品制作工测试验证竞赛考核试卷含答案
- 行李值班员安全文化知识考核试卷含答案
- 锅炉设备装配工操作规程知识考核试卷含答案
- 高中政治必修四《哲学与文化》9.2《文化发展的基本路径》教学设计
- 小学一年级道德与法治“精神有模样”教学设计
- 高中物理高三一轮复习圆周运动教学设计
- 小学四年级劳动教育《制作小灯笼》项目式教学设计
- 2026年非融资担保服务行业市场深度调查及投资规划报告及未来五至十年区域市场差异与机会
- 2026-2027学年四年级上册数学第一次月考分层训练测试完整版试卷
- 高中主题班会 珍爱生命远离毒品课件-高中主题班会
- 比亚迪造车工作制度
- 河北省石家庄市等2地2025-2026学年高二上学期10月月考物理试题
- 2024年秋人教版三年级英语上册电子课本
- 猪场非瘟质保协议书
- 安全员岗位考试及答案题库
- CJ/T 316-2009城镇供水服务
- T-CNAS 17-2020 成年女性压力性尿失禁护理干预
- 安全防护与急救处理智慧树知到课后章节答案2023年下临沂大学
评论
0/150
提交评论