-----创建序列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
到此这篇数据表的增删改查,四种操作语句的语法格式(数据增删改查sql语句的编写)的文章就介绍到这了,更多相关内容请继续浏览下面的相关 推荐文章,希望大家都能在编程的领域有一番成就!版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/sjkxydsj/17168.html