oracle學(xué)習(xí)知識點速查_第1頁
oracle學(xué)習(xí)知識點速查_第2頁
oracle學(xué)習(xí)知識點速查_第3頁
oracle學(xué)習(xí)知識點速查_第4頁
oracle學(xué)習(xí)知識點速查_第5頁
已閱讀5頁,還剩36頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、簡介:Oracle是一個對象關(guān)系數(shù)據(jù)庫管理系統(tǒng)(ORDBMS)。它既提供關(guān)系數(shù)據(jù)庫系統(tǒng)的功能,又提供面向?qū)ο髷?shù)據(jù)庫系統(tǒng)的功能。每個 Oracle數(shù)據(jù)庫對應(yīng)唯一的一個實例名SID,Oracle數(shù)據(jù)庫服務(wù)器啟動后,一般至少有以下幾個用戶:Internal,它不是一個真實的用戶名,而是具有SYSDBA優(yōu)先級的Sys用戶的別名,它由DBA用戶使用來完成數(shù)據(jù)庫的管理任務(wù),包括啟動和關(guān)閉數(shù)據(jù)庫;Sys,它是一個 DBA用戶名,具有最大的數(shù)據(jù)庫操作權(quán)限;System,它也是一個 DBA用戶名,權(quán)限僅次于 Sys用戶Oracle服務(wù)器是一個數(shù)據(jù)庫管理系統(tǒng),它提供開放、全面和綜合的信息管理。Oracle服務(wù)器由

2、Oracle數(shù)據(jù)庫和Oracle實例組成。后臺進程和內(nèi)存結(jié)構(gòu)的集合稱為Oracle實例。每一個運行的Oracle數(shù)據(jù)庫都與一個Oracle實例相聯(lián)系。一 安裝及體系結(jié)構(gòu)概述注意:環(huán)境變量的設(shè)置問題。接觸oracle幾個月中,經(jīng)常重復(fù)出現(xiàn)一些問題,其原因就是我沒有設(shè)置環(huán)境變量等相關(guān)的東西相關(guān)的設(shè)置:?安裝數(shù)據(jù)庫服務(wù)器時配置的名稱:全局數(shù)據(jù)庫名:SID:安裝客服端過程中,會出現(xiàn)Oracle Net Configuration Assistant(Oracle網(wǎng)絡(luò)配置助手)的【歡迎使用】界面:【監(jiān)聽程序配置】:【本地Net服務(wù)名配置】中:服務(wù)名:輸入全局數(shù)據(jù)庫名。網(wǎng)絡(luò)服務(wù)名(Net服務(wù)名):自己隨便

3、填!客戶機怎么連接數(shù)據(jù)庫?1.2 體系結(jié)構(gòu)數(shù)據(jù)庫的邏輯結(jié)構(gòu)Oracle9i數(shù)據(jù)庫的邏輯結(jié)構(gòu)主要指從數(shù)據(jù)庫使用者的角度來考察的數(shù)據(jù)庫的組成,如下圖。自上先下,數(shù)據(jù)庫的邏輯結(jié)構(gòu)共有6層。數(shù)據(jù)塊(Data Block)。數(shù)據(jù)區(qū)間(Data Extent)。數(shù)據(jù)段(Data Segment)。邏輯對象(Logic Object)。表空間(Tablespace)。注:Oracle 9i 安裝完畢后自動建立9個默認的表空間,如下表:名稱主要作用CWMLITE用于聯(lián)機分析處理(OLAP)DRSYS用于存放于工作空間設(shè)置有關(guān)的信息。EXAMPLE實例表空間,存放實例信息。INDEX索引表空間,存放數(shù)據(jù)庫索引信

4、息。SYSTEM系統(tǒng)表空間,存放表空間名稱、所含數(shù)據(jù)文件等管理信息。TOOLS工具表空間,存放數(shù)據(jù)庫工具軟件所需要的數(shù)據(jù)庫對象。UNDOTBS回滾表空間,存放數(shù)據(jù)庫恢復(fù)信息。USERS用戶表空間,存放用戶私有信息。數(shù)據(jù)庫(Database)1.3 登陸【管理服務(wù)器】從【Oracle企業(yè)管理控制臺】界面進入,如下所示:(只能是sysman賬號,其它的好像不行哦?)1.4 數(shù)據(jù)庫的啟動和關(guān)閉二 SQL*PLUS基礎(chǔ)、實例的啟動與關(guān)閉2.1 登陸問題環(huán)境:數(shù)據(jù)庫實例:testdb。密碼:testSql*plus連接在命令行下,sys用戶登陸:sqlplus "sys/test as sys

5、dba"實例沒有打開,使用【sqlplus "sys/test as sysdba"】執(zhí)行將錯誤ORA-12560:TNS:協(xié)議適配器錯誤!所以使用【set oracle_sid=testdb】先打開實例。如果目前實例沒有打開,我們可以使用nolog的形式登陸:sqlplus/nolog注意:無論如何都要先【set oracle_sid=testdb】,先選擇數(shù)據(jù)庫實例!操作如下:2.1.1 ORA-12560: TNS:protocol adapter error正確的連接方式更改登錄為sys:SQL> conn sys/sys服務(wù)名 as sysdba;

6、 演示:C:Documents and Settingsguocw>sqlplus /nologSQL*Plus: Release .0 - Production on 星期四 6月 18 09:12:17 2009Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.SQL> connect sys/testtestdb as sysdba;已連接。SQL> exit從Oracle9i Enterprise Edition Release .0 - Production

7、With the Partitioning, OLAP and Oracle Data Mining optionsJServer Release .0 - Production中斷開C:Documents and Settingsguocw>sqlplus/nologSQL*Plus: Release .0 - Production on 星期四 6月 18 09:17:42 2009Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.SQL> conn scott/tig

8、ertestdb已連接。SQL>啟動實例的命令:Startup:正常的命令,分為三個環(huán)節(jié),首先啟動實例;在打開控制文件,即database mount;最后打開數(shù)據(jù)文件,就是database openStartup mount:啟動數(shù)據(jù)庫實例的時候,同時啟動控制文件,但是不啟動數(shù)據(jù)文件!我們進行熱備份的時候,是不允許進行歸檔/非歸檔方式!。.?Startup nomount:啟動數(shù)據(jù)庫實例的時候,不啟動控制文件,不啟動數(shù)據(jù)文件!關(guān)閉實例:Shutdown:正常關(guān)閉,要等待所有客戶斷開連接后,才能真正關(guān)閉。Shutdown immeditale:迫使所有的用戶執(zhí)行完當(dāng)前的sql后。Shut

9、down mount:Shutdown about:強行關(guān)閉。(有丟失數(shù)據(jù)的危險,當(dāng)如火災(zāi)發(fā)生、地震的時候才使用)Sqlplus的所有的命令:SQL> help indexEnter Help topic for help. COPY PAUSE SHUTDOWN DEFINE PRINT SPOOL / (執(zhí)行緩沖區(qū)) DEL PROMPT SQLPLUS ACCEPT DESCRIBE(desc) QUIT START APPEND DISCONNECT RECOVER STARTUP ARCHIVE LOG EDIT REMARK STORE ATTRIBUTE EXECUTE R

10、EPFOOTER TIMING BREAK EXIT REPHEADER TTITLE BTITLE GET RESERVED WORDS (SQL) UNDEFINE CHANGE (c) HELP RESERVED WORDS (PL/SQL) VARIABLE CLEAR HOST RUN WHENEVER OSERROR COLUMN(col) INPUT SAVE WHENEVER SQLERROR COMPUTE LIST SET CONNECT PASSWORD SHOWEDIT(默認記事本打開緩沖區(qū),可以對緩沖區(qū)的sql編輯,然后保存關(guān)閉即可?。㏒QL> list 1*

11、select * from dept where deptno=20SQL> edit已寫入文件 afiedt.buf 1* select * from deptSQL> col deptno heading "編號"SQL> / 編號 DNAME LOC- - - 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON DESCRIBE(desc):對于表,我們想查看列對應(yīng)的字段我們用describe(desc縮寫)報表:?幾個相關(guān)命令Oracle數(shù)據(jù)

12、庫的幾種啟動和關(guān)閉方式有以下幾種啟動方式:1、startup nomount 非安裝啟動,這種方式啟動下可執(zhí)行:重建控制文件、重建數(shù)據(jù)庫 讀取init.ora文件,啟動instance,即啟動SGA和后臺進程,這種啟動只需要init.ora文件。 2、startup mount dbname 安裝啟動,這種方式啟動下可執(zhí)行: 數(shù)據(jù)庫日志歸檔、 數(shù)據(jù)庫介質(zhì)恢復(fù)、 使數(shù)據(jù)文件聯(lián)機或脫機, 重新定位數(shù)據(jù)文件、重做日志文件。 執(zhí)行“nomount”,然后打開控制文件,確認數(shù)據(jù)文件和聯(lián)機日志文件的位置, 但此時不對數(shù)據(jù)文件和日志文件進行校驗檢查。3、startup open dbname 先執(zhí)行“nom

13、ount”,然后執(zhí)行“mount”,再打開包括Redo log文件在內(nèi)的所有數(shù)據(jù)庫文件, 這種方式下可訪問數(shù)據(jù)庫中的數(shù)據(jù)。4、startup,等于以下三個命令 startup nomount alter database mount alter database open5、startup restrict 約束方式啟動 這種方式能夠啟動數(shù)據(jù)庫,但只允許具有一定特權(quán)的用戶訪問 非特權(quán)用戶訪問時,會出現(xiàn)以下提示: ERROR: ORA-01035: ORACLE 只允許具有 RESTRICTED SESSION 權(quán)限的用戶使用6、startup force 強制啟動方式 當(dāng)不能關(guān)閉數(shù)據(jù)庫時,可以

14、用startup force來完成數(shù)據(jù)庫的關(guān)閉 先關(guān)閉數(shù)據(jù)庫,再執(zhí)行正常啟動數(shù)據(jù)庫命令7、startup pfile=參數(shù)文件名 帶初始化參數(shù)文件的啟動方式 先讀取參數(shù)文件,再按參數(shù)文件中的設(shè)置啟動數(shù)據(jù)庫 例:startup pfile=E:Oracleadminoradbpfileinit.ora8、startup EXCLUSIVE=有三種啟動方式:1、shutdown normal 正常方式關(guān)閉數(shù)據(jù)庫。2、shutdown immediate 立即方式關(guān)閉數(shù)據(jù)庫。 在SVRMGRL中執(zhí)行shutdown immediate,數(shù)據(jù)庫并不立即關(guān)閉, 而是在Oracle執(zhí)行某些清除工作后才關(guān)閉

15、(終止會話、釋放會話資源), 當(dāng)使用shutdown不能關(guān)閉數(shù)據(jù)庫時,shutdown immediate可以完成數(shù)據(jù)庫關(guān)閉的操作。3、shutdown abort 直接關(guān)閉數(shù)據(jù)庫,正在訪問數(shù)據(jù)庫的會話會被突然終止, 如果數(shù)據(jù)庫中有大量操作正在執(zhí)行,這時執(zhí)行shutdown abort后,重新啟動數(shù)據(jù)庫需要很長時間。三 SQL3.1 三類語言:數(shù)據(jù)定義DDL:create、alter、drop。數(shù)據(jù)控制語言DCL:grant、revoke。數(shù)據(jù)操作語言DML:select、insert、delete、update。演示:-SQL> create table abc(a varchar2(

16、10),b char(10);表已創(chuàng)建。SQL> alter table abc add c number;表已更改。SQL> alter table abc drop c;alter table abc drop c *ERROR 位于第 1 行:ORA-00905: 缺少關(guān)鍵字SQL> alter table abc drop column c;表已更改。-3.2 oracle常用系統(tǒng)函數(shù)3.2.1 字符3.2常用系統(tǒng)函數(shù)3.2.1 字符length,ltrim,replace,rtrim,substr,trim演示:select length('abcdef&#

17、39;) from dual;查詢字符數(shù) 如本語句結(jié)果是6select length('abc好def') from dual;7select lengthb('abc好def') from dual;查詢字節(jié)數(shù),為8;select ltrim(' abc好def') from dual; 去除左邊空格select rtrim('abc好ef ')from dual; 去除左邊右邊空格select trim (' abc ')from dual; 去除兩邊的空格select length(trim (' a

18、bc ')from dual; 3select substr('abcdefg',2,3) from dual;從第二個開始取串取三個即得 bcdselect substr('abcdefg',1,3) from dual;取開始3個select substr('abcdefg',length('abcdefg')- 3 + 1,3) from dual;取末尾3個alter session set NLS_DATE_FORMAT='dd-mm-yyyy hh24:mi:ss' 設(shè)定時間格式select s

19、ysdate from dual; 結(jié)果:2009-6-17 11:32:313.2.2 日期sysdate,curtent_date,next_dayselect sysdate from dual; 2009-6-17 11:40:49select current_date from dual;2009-6-17 11:41:02 alter session set nls_date_format='dd-mon-yyyy hh:mi:ss'當(dāng)前時間2009-6-17 11:41:02 星期三 select next_day(sysdate,'星期四') f

20、rom dual; 下個星期四 2009-6-18 11:43:24select next_day(sysdate,'星期三') from dual; 下個星期三:2009-6-24 11:43:493.2.3 轉(zhuǎn)換to_char,to_date,to_number select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual; 轉(zhuǎn)換成字符2009-06-17 11:48:14select length(to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from

21、 dual; 19select to_date('2009-06-17 11:48:14','yyyy-mm-dd hh24:mi:ss') from dual;將字符串轉(zhuǎn)換成日期2009-6-17 11:48:14select to_number('123') from dual;將字符串轉(zhuǎn)換成數(shù)字(多大溢出?)3.2.4聚集函數(shù)sum,avg,max,min,count select max(price) from book; 求最大值select min(price) from book; 求最小值select avg(price) fro

22、m book; 求平均值select count(price) from book; select count(*) from book;查詢有幾條記錄 求數(shù)據(jù)總個數(shù)select sum(price) from book; 求總數(shù)select * from book where price > 20; 3.2.5其他user,decode,nvlselect user from abc; 查看用戶select sum(decode(sex,'男',1,0) 男人數(shù),sum(decode(sex,'女',1,0)女人數(shù) from e; sum(decode(

23、sex,'男',1,0)sex字段是男加1,否則加0。select a1,nvl(a2,'未輸入')a2 from aa; 在null的地方寫上'未輸入'select * from aa where a2 is null; 為空select * from aa where a2 is not null; 不為空select * from aa order by a1 asc; 升序select * from aa order by a1 desc; 降序select distinct a1 from aa;沒個a1 只顯示一個,不重復(fù)。selec

24、t all a1 frm aa;3.3 分組語句用戶匯總一些數(shù)據(jù)的時候:求合計select sum(price*qty) from books; qty數(shù)量列按出版社(pub)分組查詢的時候:select sum(price*qty) from books group by pub; 或select pub, sum(price*qty) from books group by pub; 注意:select顯示的列一定要在group by 中,否則錯誤!select pub,book_name, sum(price*qty) from books group by pub; 正確的語句應(yīng)該是:

25、select pub,book_name, sum(price*qty) from books group by pub,book_name; 語法上必須要求這樣做。group by可以多個,但是必須與前面的select中得到允許??梢栽黾觲here條件:select pub, sum(price*qty) from books where price>30 group by pub; 如果這樣寫:select pub, sum(price*qty) from books where sum(price*qty)>30 group by pub 就要報錯了!為什么呢?因為聚集函數(shù)不

26、能作為條件出現(xiàn)在where子句后面!要實現(xiàn)類似的功能,我們使用having子句去改造,group by要出現(xiàn)在having子句前面。select pub, sum(price*qty) from books group by pub having sum(price*qty)>30;除此以外,我們很少使用having子句。示例:select pub,count(pub) from book group by pub having count(pub)>1;select ftype from tcmo group by ftype having count(moid)>10;

27、3.4 表的連接內(nèi)連接:又叫等值奪標(biāo)查詢? 將按照等值的條件查詢多個數(shù)據(jù)表中關(guān)聯(lián)的數(shù)據(jù)。要求關(guān)聯(lián)的多個數(shù)據(jù)表的某些字段具有相同的屬性,即有相同的數(shù)據(jù)類型、寬度和取值范圍。正規(guī)的寫法select * from tcmo a join tcmotype b on a.ftype=b.fmotype;select * from tcmo a , tcmotype b where a.ftype=b.fmotype;外連接有兩種:左(外)連接和右(外)連接左連接:正規(guī)的寫法select * from tcmo a left join tcmotype b on a.ftype=b.fmotype;se

28、lect * from tcmo a , tcmotype b where a.ftype=b.fmotype(+);右連接:正規(guī)的寫法select * from tcmo a right join tcmotype b on a.ftype=b.fmotype;select * from tcmo a , tcmotype b where a.ftype(+)=b.fmotype;還可以在查詢中添加其他條件:兩種寫法有一些差別select * from tcmo a right join tcmotype b on a.ftype=b.fmotype where a.ftype='L

29、INUX'select * from tcmo a , tcmotype b where a.ftype(+)=b.fmotype and a.ftype='LINUX'3.5 子查詢IN、EXISTS相關(guān)子查詢無關(guān)子查詢有的時候,子查詢與連接查詢都能得到相同的結(jié)果:誰的效率更高呢?3.6 where與havingwhere檢查每條記錄是否符合條件,having是檢查分組后的各族是否滿足條件。Having語句只能配合group by語句使用,沒有g(shù)roup by時不能使用having,單可以使用where。3.7 嵌套更新語法:update 數(shù)據(jù)表set 字段名1=(s

30、elect 字段列 from 數(shù)據(jù)表 where 條件),字段名2=(select 字段列 from 數(shù)據(jù)表 where 條件),Where 更新條件;四 PL/SQLPL/SQL是Oracle對sql的一種擴展語言!命令行下要顯示輸出行:set serveroutput on size 10000 (最大可設(shè)置一萬)!4.1 第一個例子declare i number :=1;x varchar2(20):='初值'begin dbms_output.put_line('打印一行.初值i='|i); dbms_output.put_line(x); i:=10

31、; x:='this is .' -字符串連接|。行注釋- -快注釋/* 注釋內(nèi)容*/ dbms_output.put_line('打印一行.i='|i); dbms_output.put_line(x);end; 4.2 分支語句4.2.1 if分支declare i number ;str varchar2(20);begin i:=2; if i=1 then str:='sno001' elsif i=2 then str:='sno002' else str:='else' end if; dbms_ou

32、tput.put_line(str);end; 4.2.3 case分子declare i number ;str varchar2(20);begin i:=2; case when i=1 then str:='sno001' when i=2 then str:='sno002' else str:='else' end case; dbms_output.put_line(str);end; 4.3 循環(huán)語句4.3.1 基本循環(huán)(loop)示例 1:declare i number:=0;begin loop i:=i+1; if(i&g

33、t;=100)then exit; end if; dbms_output.put_line('loop內(nèi)i='|i); end loop; dbms_output.put_line('loop外i='|i);end;示例 2declare i number:=0;begin loop i:=i+1; exit when i>=3 ; dbms_output.put_line('loop內(nèi)i='|i); end loop; dbms_output.put_line('loop外i='|i);end;4.3.2 while循環(huán)

34、declare i number:=0;begin while i<=3 loop i:=i+1; dbms_output.put_line('loop內(nèi)i='|i); end loop; dbms_output.put_line('loop外i='|i);end;4.3.3 for循環(huán)在oracle的for循環(huán)中無論“從小到大循環(huán)”還是“從大到小的循環(huán)”,我們對應(yīng)的地增量不由我們控制,oracle的for循環(huán)的遞增量只能是1。但是我們可以使用goto進行跳轉(zhuǎn),需要一個標(biāo)記。Oracle支持goto。實例3將給我們演示。示例 1 :從小到大begin fo

35、r i in 1.5 loop dbms_output.put_line('i='|i); end loop; dbms_output.put_line('end of for loop.');end;實例 2 :從大到小,我們在in后面加上一個reverse。begin for i in reverse 1.5 loop dbms_output.put_line('i='|i); end loop; dbms_output.put_line('end of for loop.');end;實例 3 :用goto語句實現(xiàn)循環(huán)dec

36、lare i number:=0;begin -<<標(biāo)記>> <<repeat_loop>> i:=i+1; dbms_output.put_line('i='|i); if(i<=3)then goto repeat_loop; end if;end;4.3 異常處理在PL/SQL中的一個告警或錯誤的情形都被稱為異常。包括:編譯時刻錯誤或運行時刻錯誤。所以異常分類為:系統(tǒng)異常;自定義異常。常見系統(tǒng)異常:預(yù)定義異常描述DUP_VAL_ON_INDEX向有唯一約束的表中插入重復(fù)行NO_DATA_FOUND在一個select i

37、nto語句中無返回值TOO_MANY_ROWSSelect into 語句返回了多行VALUE_ERROR一個算法、轉(zhuǎn)換、截斷或大小約束發(fā)生錯誤ZERO_DIVIDE發(fā)生被零除4.3.1 一個系統(tǒng)異常的寫法:declare test varchar2(20);begin select dname into test from dept where deptno=90; dbms_output.put_line(test); exception when no_data_found then dbms_output.put_line('沒有找到數(shù)據(jù)');end;4.3.2 自定義

38、異常4.4 復(fù)合變量:記錄復(fù)合變量(記錄)是由幾個相關(guān)值構(gòu)成的復(fù)合變量,常用語支持select語句的返回值。使用記錄可以將一行數(shù)據(jù)看成一個單元進行處理,而不必將每一列單獨處理。記錄的聲明格式:TYPE type_name IS RECORD( Variable_name datatype, Variable_name datatype, .);示例 1:declare-定義復(fù)合變量(記錄) TYPE myrecord IS RECORD( id number, -最后一個變量沒有逗號 name varchar2(20) ); -聲明一個新類型的變量o o myrecord;begin-注意se

39、lect into : select deptno,dname into o from dept where deptno=10; dbms_output.put_line('部門號:'|o.id|',部門名:'|); exception when no_data_found then dbms_output.put_line('沒有找到數(shù)據(jù)');end;實例 2 : 記錄中的簡單變量與表的字段的定義的類型和長度一致declare-定義復(fù)合變量(記錄) TYPE myrecord IS RECORD( id number, -最后一個

40、變量沒有逗號 -指定name是和dept.dname一樣長度的聲明的變量 name dept.dname%type ); -聲明一個新類型的變量o o myrecord;begin-注意select into : select deptno,dname into o from dept where deptno=10; dbms_output.put_line('部門號:'|o.id|',部門名:'|); exception when no_data_found then dbms_output.put_line('沒有找到數(shù)據(jù)');

41、end;實例 3 :記錄與表的所有字段對應(yīng)declare myrec dept%ROWTYPE;begin-注意select into : select * into myrec from dept where deptno=10; dbms_output.put_line('部門號:'|myrec.deptno|',部門名:'|myrec.dname); exception when no_data_found then dbms_output.put_line('沒有找到數(shù)據(jù)');end;五 PL/SQL高級應(yīng)用5.1 游標(biāo)游標(biāo)是oracle

42、中一種PL/SQL控制結(jié)構(gòu);可以對SQL語句的處理進行顯式控制,便于對表的行數(shù)據(jù)逐條進行處理。游標(biāo)不是一個數(shù)據(jù)庫對象,而是以聲明的方式在內(nèi)存中開辟的一個區(qū)域。游標(biāo)可被看著兩個環(huán)節(jié)的內(nèi)容,由兩個方面組成的:一個是游標(biāo)的結(jié)果集(如sql的select語句查詢得到的表的結(jié)果集)。第二個就是這個結(jié)果中對應(yīng)的數(shù)據(jù)的位置。游標(biāo)分類:顯式游標(biāo);隱式游標(biāo)。游標(biāo)的屬性:6MI%FOUND%ISOPEN%NOTFOUND%ROWCOUNT重點經(jīng)驗:當(dāng)表中的數(shù)據(jù)量很大的時候,我們不建議使用游標(biāo)。為什么呢?因為它的執(zhí)行效率不是很高,很耗資源。但是在有的情況下,比如逐行取數(shù)據(jù)又是很靈活的。5.1.1 顯式游標(biāo)示例 1:

43、- 顯示游標(biāo)declare - cursor mycursor is select * from dept; -游標(biāo)的數(shù)據(jù)取來放入一個記錄的變量中。 myrecord dept%rowtype;begin - 將顯示游標(biāo) 結(jié)果集打開 open mycursor; -然后,把打開的游標(biāo)的結(jié)果集的一條(這里是第一條)放入我們申明的變量myrecord。 fetch mycursor into myrecord; while mycursor%found loop dbms_output.put_line(myrecord.dname|','|myrecord.deptno); -繼

44、續(xù)取,指導(dǎo)取完 fetch mycursor into myrecord; end loop; -取到最近偶的時間我們要把游標(biāo)關(guān)閉。 close mycursor; end;實例 2 :游標(biāo)參數(shù)實現(xiàn)對數(shù)據(jù)的控制。給游標(biāo)定義一個參數(shù)(在plsql中作為參數(shù)是不需要長度的,只給出類型就可以)declare cursor cur_param(id number) is select dname from dept d where d.deptno=id; t_name dept.dname%type; begin -打開游標(biāo)并賦一個值 open cur_param(1); loop fetch cu

45、r_param into t_name; exit when cur_param%notfound ; dbms_output.put_line(t_name); end loop; close cur_param;end;For循環(huán)的另外一種格式:不需要打開和關(guān)閉游標(biāo)的句子。declare cursor cur_param(id number) is select dname from dept d where d.deptno=id; begin for cur in cur_param(1) loop dbms_output.put_line(cur.dname|'.名稱'

46、;); end loop;end;5.1.2 %isopen屬性declared_name dept.dname%type;cursor cur(id number) isselect dname from dept where dept.deptno=id;begin if cur%isopen then dbms_output.put_line('游標(biāo)已經(jīng)打開'); else open cur(1); end if; fetch cur into d_name; close cur; dbms_output.put_line(d_name);end;5.1.3 %rowco

47、unt屬性返回至今為止,已經(jīng)從游標(biāo)中取回的數(shù)目。declaret_name varchar2(10);cursor mycur isselect dname from dept;beginopen mycur;loop-下面的fecth mycur into t_name語句被注釋掉,將是什么樣的結(jié)果?沒有做fetch的時候 mycur%notfound is null將執(zhí)行fetch mycur into t_name;exit when mycur%notfound or mycur%notfound is null;dbms_output.put_line('游標(biāo)mycur的ro

48、wcount是:'|mycur%rowcount);end loop;close mycur;end;5.1.4 使用游標(biāo)修改數(shù)據(jù)declaret_name varchar2(10);cursor mycur isselect dname from dept for update;beginopen mycur;loopfetch mycur into t_name;exit when mycur%notfound or mycur%notfound is null;-游標(biāo)的當(dāng)前行:current of mycurupdate dept set dname=dname|'_up

49、date' where current of mycur;end loop;close mycur;end;注意:update dept set dname=dname|'_update',loc=loc|'_u' where current of mycur;放在上面的pl/sql中,將報錯“字符串緩沖區(qū)太小”!游標(biāo)只指向的是列dname。5.1.5 隱式游標(biāo)PL/SQL為所有SQL數(shù)據(jù)操作語句(包括返回一行的SELECT)隱式聲明游標(biāo),稱為隱式聲明游標(biāo)的原因是用戶不能直接命名和控制此類游標(biāo)。當(dāng)用戶在PL/SQL中使用數(shù)據(jù)庫操作語言(DML)時,Ora

50、cle預(yù)定義一個名為SQL的隱式游標(biāo),通過檢查隱式游標(biāo)的屬性可以獲取與最近執(zhí)行的SQL語句相關(guān)的信息。在執(zhí)行DML語句后,隱式游標(biāo)屬性返回信息。對于隱式游標(biāo)而言,屬性%ISOPEN的值總是false,這是因為隱式游標(biāo)在DML語句執(zhí)行時打開,結(jié)束時就立即關(guān)閉。隱式游標(biāo):不需要我們declare、open、使用完了不需要我們close。begin for cur in(select dname from dept) loop dbms_output.put_line(cur.dname);end loop;end;當(dāng)表中的數(shù)據(jù)量很大的時候,我們不建議使用游標(biāo)。為什么呢?因為它的執(zhí)行效率不是很高,很

51、耗資源。但是在有的情況下,比如逐行取數(shù)據(jù)又是很靈活的。5.2 存儲過程創(chuàng)建語法 5.2.1 一個簡單的存儲過程IN:輸入?yún)?shù)create or replace procedure pro1(id in number)-is或as都可以is-參數(shù)name varchar2(20);beginselect dname into name from dept where deptno=id;dbms_output.put_line(name);exception when TOO_MANY_ROWS then dbms_output.put_line('Select into 語句返回了多行

52、');when no_data_found then dbms_output.put_line('沒有找到數(shù)據(jù)');-結(jié)束使用:end pro1也可以end;上面sql塊執(zhí)行成功就會創(chuàng)建存儲過程。如何調(diào)用這個存儲過程呢?如下所示declare var number;beginvar:=2;pro1(var);end;beginfor var in 1.10looppro1(var);end loop;end;beginpro1(40);end;Sql plus下還可以:execute pro1(40);5.2.2 輸出參數(shù)如何使用Oracle定時作業(yè)隊列當(dāng)數(shù)據(jù)表中數(shù)據(jù)量很大的時候,我們進行查詢速度會很慢,特別是上百萬數(shù)據(jù)做報表的時候體會就更深了,為此,我們采用定時執(zhí)行存儲過程,提取我們需要的數(shù)據(jù)。寫到這里的時候大家可能會有些疑問,這樣我們的數(shù)據(jù)不就不能實時最新了嗎?你的疑問是正確的,我們?yōu)槭裁刺岢鲞@種解決方案呢,原因在于我們的數(shù)據(jù)是生產(chǎn)數(shù)據(jù)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論