版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
-----创建序列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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 土方机械维修工岗中模拟考核试卷含答案
- 浆纱机操作工岗位工作流程考核试卷含答案
- 数字化解决方案设计师岗前绩效目标考核试卷含答案
- 铁路机车制修工岗位核心考核试卷含答案
- 客服服务考试试题及答案
- 四年级上册第七单元单元总案
- 高比例清洁能源背景下灵活调节产品交易机制:理论、实践与创新
- 高校贫困生人格健康发展的多维透视与赋能路径
- 高校科技特派员激励机制探究:以天津工业大学为样本
- 高校教师职业压力与职业倦怠的关联性剖析与应对策略研究
- 2025-2026学年人教版七年级英语全册(上下册)单词背默单
- 货物项目分包协议书范本
- 人教版高中地理必修第二册期末复习重点知识背诵提纲
- 二升三数学综合练习 暑假每日一练60天
- 从知到行:英语教学知识实践转化的自我叙事探究
- 备用发电机维护管理制度
- 烧烤店店长管理制度
- 极地建筑保温技术-洞察及研究
- 2025届杭州市锦绣育才教育科技集团八下英语期末联考试题含答案
- 2025年四川内江市兴元实业有限责任公司招聘笔试参考题库附带答案详解
- 中国兽药典三部 2020年版
评论
0/150
提交评论