《大型數(shù)據(jù)庫Oracle》實(shí)驗(yàn)指導(dǎo)書(0710)_第1頁
《大型數(shù)據(jù)庫Oracle》實(shí)驗(yàn)指導(dǎo)書(0710)_第2頁
《大型數(shù)據(jù)庫Oracle》實(shí)驗(yàn)指導(dǎo)書(0710)_第3頁
《大型數(shù)據(jù)庫Oracle》實(shí)驗(yàn)指導(dǎo)書(0710)_第4頁
《大型數(shù)據(jù)庫Oracle》實(shí)驗(yàn)指導(dǎo)書(0710)_第5頁
已閱讀5頁,還剩47頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、大型數(shù)據(jù)庫oracle編撰閩江學(xué)院計算機(jī)實(shí)驗(yàn)教學(xué)中心印制51目 錄實(shí)驗(yàn)一 oracle數(shù)據(jù)庫管理與工具使用2實(shí)驗(yàn)二 表連接、高級子查詢6實(shí)驗(yàn)三 管理存儲結(jié)構(gòu)、用戶、權(quán)限安全、模式和表12實(shí)驗(yàn)四 約束、索引、視圖、序列和同義詞16實(shí)驗(yàn)五 pl/sql編程基礎(chǔ)20實(shí)驗(yàn)六 觸發(fā)器、過程、函數(shù)和程序包27實(shí)驗(yàn)七 管理存儲結(jié)構(gòu)、備份與恢復(fù)33實(shí)驗(yàn)八 sql*plus和sql基本查詢39實(shí)驗(yàn)九 管理存儲結(jié)構(gòu)、用戶、權(quán)限安全、模式和表42實(shí)驗(yàn)十 創(chuàng)建和管理表,視圖,約束46實(shí)驗(yàn)十一 序列、索引、同義詞48 實(shí)驗(yàn)一 oracle數(shù)據(jù)庫管理與工具使用一、 實(shí)驗(yàn)?zāi)康?. 掌握oracle的啟動、關(guān)閉、連接和管理初

2、始化參數(shù)2. 熟悉em和sql*plus的使用3. 了解用戶和模式的概念,并查詢hr模式表的數(shù)據(jù)二、實(shí)驗(yàn)環(huán)境一臺pc機(jī),安裝widows xp操作系統(tǒng),oracle 10g或11g數(shù)據(jù)庫軟件。三、實(shí)驗(yàn)內(nèi)容 1. 啟動和關(guān)閉window操作系統(tǒng)的oracle服務(wù)2. 使用sql*plus與oracle數(shù)據(jù)庫建立連接3. 使用sql*plus啟動和關(guān)閉oracle數(shù)據(jù)庫4. 使用em和sql*plus顯示和設(shè)置初始化參數(shù)文件5. 使用em和sql*plus解鎖hr模式,并使用sql*plus編寫查詢該模式中表的sql四、實(shí)驗(yàn)步驟1)進(jìn)入windows xp操作系統(tǒng) 控制面板-管理工具-服務(wù)啟動以下

3、幾個服務(wù):oracleservice(數(shù)據(jù)庫名)oracleoradb10g_home1tnslisteneroracleoradb10g_home1isql*plusoracledbconsoleorcl(啟動em需要的服務(wù))2)使用sql*plus連接oracle數(shù)據(jù)庫進(jìn)入windows xp操作系統(tǒng) oracle - oradb10g_home1-應(yīng)用程序開發(fā)-sql plus輸入:用戶名:sys/orcl as sysdba 按確定。 輸入select * from hr.employees; 按回車可以查詢該表數(shù)據(jù)3)使用sql*plus用sys用戶關(guān)閉 oracle數(shù)據(jù)庫輸入并執(zhí)行

4、如下sql語句:shutdown immediate輸入select * from hr.employees; 按回車無法查詢該表數(shù)據(jù)4)創(chuàng)建并啟動與數(shù)據(jù)庫對應(yīng)的實(shí)例 startup nomount 按回車5)為未加載數(shù)據(jù)庫的實(shí)例加載數(shù)據(jù)庫alter database mount; 按回車6)將處于未打開狀態(tài)的數(shù)據(jù)庫設(shè)置為打開狀態(tài)alter database open; 按回車7) 關(guān)閉數(shù)據(jù)庫然后直接啟動實(shí)例,加載數(shù)據(jù)庫,打開數(shù)據(jù)庫shutdown immediate;startup;8) 使用sql*plus顯示所有初始化參數(shù) show parameter9) 使用em查詢和設(shè)置動態(tài)初始化參

5、數(shù)進(jìn)入ie,http:/服務(wù)器:1158/em數(shù)據(jù)庫管理-初始化參數(shù)10) 解鎖hr用戶使用em用system登錄,通過方案-用戶-編輯hr用戶為解鎖,同時修改密碼。使用sqlplus 用system登錄alter user hr account unlock;alter user hr identified by hr;11) 使用hr用戶連接sqlplus,connect hr/rh;select table_name from user_tables;select * from employeesselect * from 客戶信息;查詢結(jié)果應(yīng)該包括客戶信息的所有列,并且包括該表的所有記

6、錄行。2)下面的sql語句將只返回“客戶姓名”、“客戶地址”和“聯(lián)系電話”列。select 客戶姓名,客戶地址,聯(lián)系電話from 客戶信息;這個查詢結(jié)果將包含指定的列,并且包含該表所有的記錄。3)在下面的select語句中,將使用where子句以限定檢索的客戶信息。select 客戶信息,客戶地址,聯(lián)系電話from 客戶信息where 所屬業(yè)務(wù)員編號1001由于添加了where子句,所以查詢結(jié)果現(xiàn)在只包含業(yè)務(wù)員編號為“1001”的客戶信息。4)下面創(chuàng)建的select語句用于對“客戶信息”表中的信息分組,輸入并執(zhí)行如下語句: select 所屬業(yè)務(wù)員編號 as 業(yè)務(wù)員編號,count(客戶編號)

7、 as 客戶數(shù)from 客戶信息group by 所屬業(yè)務(wù)員編號對應(yīng)于每一個業(yè)務(wù)員編號都返回一行,并且每行中都包含所屬的客戶數(shù)。5)將having子句添加到剛才執(zhí)行的select語句中,輸入并執(zhí)行如下sql語句。select 所屬業(yè)務(wù)員編號 as 業(yè)務(wù)員編號,count(客戶編號) as 客戶數(shù)from 客戶信息group by 所屬業(yè)務(wù)員編號having count(客戶編號)=3;使用sql*plus函數(shù):6)下面的sql語句使用一組數(shù)據(jù)演示trunc函數(shù)與round函數(shù)之間的區(qū)別和相同點(diǎn)。select trunc(46.326,2),round(46.326,2)from dual;運(yùn)行

8、該語句中,trunc函數(shù)的返回值為46.32,round函數(shù)的返回值為46.33。select trunc(46.326),round(46.326)from dual;運(yùn)行該語句中,trunc函數(shù)和round函數(shù)將返回相同的值46。五、實(shí)驗(yàn)小結(jié)通過本次實(shí)驗(yàn)學(xué)會了使用sql*plus與數(shù)據(jù)庫建立連接,使用select語句檢索數(shù)據(jù)。實(shí)驗(yàn)二 表連接、高級子查詢一、 實(shí)驗(yàn)?zāi)康?.理解什么是連接操作2.如何進(jìn)行簡單的連接操作3.掌握如何進(jìn)行內(nèi)連接、外連接、交叉連接4.創(chuàng)建返回多行的子查詢5.創(chuàng)建返回單值的子查詢6.掌握嵌套子查詢的使用二、實(shí)驗(yàn)設(shè)備一臺pc機(jī),安裝widows xp操作系統(tǒng),oracle

9、 10g或11g數(shù)據(jù)庫軟件。三、實(shí)驗(yàn)內(nèi)容 1. 通過查詢school模式中的表,以練習(xí)各種連接操作的使用,特別是具有外鍵關(guān)聯(lián)的表。在sql*plus環(huán)境下,打開sql*plus,連接到school模式。1)第一個操作是用逗號分隔連接,將表student和class連接起來,查詢學(xué)生及所在班級信息。2) 在上面語句的基礎(chǔ)上連接department表,以顯示所在院系名稱.3) 修改上一個select語句,將其改為內(nèi)連接中的等值連接.4) 修改上一個select語句,將其改為連接中的自然連接,并且限制只顯示”法律系”的學(xué)生信息.5) 修改上一個select語句,將其改為右連接條件.本練習(xí)使用逗號分隔

10、連接,交叉連接,內(nèi)連接和外連接查詢多個表,可以看連接查詢?yōu)椴僮鲾?shù)據(jù)庫提供了很大的靈活性.但是連接并不是唯一的訪問多個表的方法.子查詢的功能與連接一樣,也可以實(shí)現(xiàn)多表查詢.2.本練習(xí)將使用子查詢數(shù)據(jù).子查詢很大程度上是通過關(guān)鍵字連接在一起的,這里將使用多個關(guān)鍵字連接子查詢,以便查詢多個表. sql*plus , 連接到school模式.1) 創(chuàng)建的第一個select 語句將要返回”06法2班”所有的學(xué)生信息. 2) 修改上一個查詢語句,使用關(guān)鍵字exists連接子查詢,并且要求顯示的數(shù)據(jù)相同.3) 下面的語句將使用嵌套查詢顯示屬于”法律系”的所有學(xué)生信息.4) 現(xiàn)在再創(chuàng)建一個select 語,該

11、語句查詢”刑法通則”課程的學(xué)習(xí)成績最高的學(xué)生信息.5) 上面的語句通過使用大于等于(=)比較運(yùn)算符和all關(guān)鍵字實(shí)現(xiàn),另外一種簡單的方法是使用統(tǒng)計函數(shù)max實(shí)現(xiàn).四、實(shí)驗(yàn)步驟1.1)輸入并執(zhí)行如下sql語句:select s.sid , s.sname , s.birth , s.sclass ,s.saddress , c.department , c.cnamefrom student s , class cwhere s.sclass=c.cid;2)輸入并執(zhí)行如下sql語句:select s.sid , s.sname , s.sclass,s.saddress , c.departm

12、ent , c.cname, d.dnamefrome student s,class c , department dwhere s.sclass=c.cidand c.department=d.did;3)輸入并執(zhí)行如下sql語句:select s.sid , s.sname , s.sbirth, s.sclass,s.saddress , c.deoartment , c.cname , d.dnamefrom student s join class con s.sclass=c.cidjoin department don c.depatment=d.did;4)輸入并執(zhí)行如下sq

13、l語句:select s.sid , s.sname , s.sbirth , s.sclass,s.saddress , c.department, c.cname,d.dnamefrom student s natural join class c natural join department dwhere d.dname=法律系 ;5) 輸入并執(zhí)行如下sql語句:select s.sid , s.sname , s.sbirth , s.sclass, s.saddress , c.department , c.cname , d.dnamefrom student s right j

14、oin class con s.sclass=c.cidright join department don c.department=d.did;2. 1)輸入并執(zhí)行如下語句;select * from studentwhere sclass in(select cid from classwhere cname = 06法2班此語句使用子查詢查詢class表,以獲取名為” 06法2班”的cid列表.in關(guān)鍵字再把該返回值與student表中的cid列表進(jìn)行比較.2)輸入并執(zhí)行如下sql語句:select * from student swhere exists(select * from c

15、lass cwhere c.cname= 06法2班and s.sclass=c.cid);執(zhí)行該語句獲取的學(xué)生信息與上一個語句獲取的學(xué)生信息相同,均為屬于” 06法2班”的學(xué)生信息.3)輸入并執(zhí)行如下查詢語句:select * from studentwhere sclass in (select cid from classwhere department in(select did from departmentwhere dname=法律系);4)輸入并執(zhí)行如下sql語句:select * from studentwhere sid in(select sid from gradewh

16、ere score=all(select score from gradewhere coid in(select coid from courseswhere coname=刑法通則);在上面的語句中,最內(nèi)層的select語句用于查詢課程名為”刑法通則”的課程代號,然后上層select語句根據(jù)該課程代號查詢該課程的所有成績;次外層select 語句則使用大于等于(=)比較算符和all關(guān)鍵字,求出大于等于全部成績的所有學(xué)生編號;最后由select 語句列出這些學(xué)生信息.5)輸入并執(zhí)行如下sql語句,比較兩者的運(yùn)算結(jié)果.select * from studentwhere sid in(sele

17、ct sid from gradewhere score=(select max(score) from gradewhere coid=(select coid from courses where coname=刑法通則); 由于使用統(tǒng)計函數(shù)max,可以確定返回的最高成績只有一個,所以可以使用單值子查詢;另外由于每門課程只對一個代號,雖然這里沒有使用統(tǒng)計函數(shù),但是也可以使用單值子查詢.由此可以看出,sql語句是非常靈活的,可以由多種方法實(shí)現(xiàn)相同的功能.五、實(shí)驗(yàn)小結(jié)通過本次實(shí)驗(yàn)理解了什么是連接操作及如何進(jìn)行簡單的連接操作。掌握了如何進(jìn)行內(nèi)連接、外連接、交叉連接。實(shí)驗(yàn)三 管理存儲結(jié)構(gòu)、用戶、權(quán)

18、限安全、模式和表一、 實(shí)驗(yàn)?zāi)康?. 創(chuàng)建表空間2. 創(chuàng)建用戶3. 創(chuàng)建角色使得安全模式的設(shè)置和管理容易4. 使用grant和revoke語句授予和撤銷對象權(quán)限5. 創(chuàng)建表和管理表二、實(shí)驗(yàn)設(shè)備一臺pc機(jī),widows操作系統(tǒng),oracle 10g三、實(shí)驗(yàn)內(nèi)容 1創(chuàng)建一個表空間exer_tabspace,采用本地化管理,分配初始空間為100mb,使用空間配額不受限制,文件名:exer_tabspace.dbf,放在數(shù)據(jù)文件相應(yīng)目錄。2創(chuàng)建一個用戶tempuser,其口令為oracle,默認(rèn)表空間為exer_tabspace,臨時表空間為temp,對表空間沒有配額限制。3. 創(chuàng)建一個用戶配置文件te

19、mpprofile,包含的資源及口令限制如下:該用戶最多可以建立3個并發(fā)的會話連接用戶執(zhí)行語句使用的cpu最長時間為20分鐘空閑時間超過15分鐘后,斷開與用戶的連接限制用戶每次調(diào)用sql語句時,能夠讀取的數(shù)據(jù)庫塊數(shù)200限制用戶在登錄到oracle數(shù)據(jù)庫時允許失敗的次數(shù)24. 為用戶tempuser指定配置文件5. 向用戶授予連接數(shù)據(jù)庫系統(tǒng)權(quán)限。6向用戶授予對對象“vendition.商量信息”的select權(quán)限,并以用戶tempuser連接到數(shù)據(jù)庫,以查詢“商品信心”表。7撤銷向用戶tempuser授予的系統(tǒng)權(quán)限,向用戶授予connect,resource角色。四、實(shí)驗(yàn)步驟1. 創(chuàng)建一個表空

20、間exer_tabspace1)connect system/orcl;2) create smallfile tablespace exer_tabspace datafile f:oracleproduct10.2.0oradataorclexer_tabspace.dbf size 100m logging extent management local segment space management auto2. 創(chuàng)建一個用戶tempusercreate user tempuseridentified by oracle default tablespace userstempora

21、ry tablespace tempquota unlimited on users;3創(chuàng)建一個用戶配置文件tempprofilecreate profile tempprfile limitsessions_per_user 3cpu_per_call 2000idle_time 15logical_reads_per_call 200failed_login_attempts 24為用戶tempuser指定配置文件alter user tempuser profile tempprofile;5向用戶授予連接數(shù)據(jù)庫系統(tǒng)權(quán)限grant create session to tempuser;6

22、向用戶授予對對象“vendition.商量信息”的select權(quán)限,并以用戶tempuser連接到數(shù)據(jù)庫,以查詢“商品信心”表。grant select on vendition.商品信息 to tempuser;connect tempuser/oracle;select * from vendition.商品信息;7撤銷向用戶tempuser授予的系統(tǒng)權(quán)限,向用戶授予connect,resource角色。connect system/orcl;revoke select on vendition.商品信息 from tempuser;revoke create session from t

23、empuser;grant connect to tempuser;grant resource to exercise;8創(chuàng)建基本表(1)登錄到tempuser模式,在其中創(chuàng)建一個學(xué)生信息表student和一個班級信息表class。connect tempuser/oracle;create table student(id varchar2(20),name varchar2(8),sex number(1),classid varchar2(20)tablespace users;create table class(id varchar2(20),principal varchar2(

24、20),tel varchar2(20)tablespace users;(2)修改新創(chuàng)建表student的存儲參數(shù)。alter table student movestorage(next 32kmaxextents unlimitedpctincrease 0)(3).修改表class的數(shù)據(jù)塊參數(shù)。alter table classpctfree 40pctused 40;五、實(shí)驗(yàn)小結(jié)通過本次實(shí)驗(yàn)學(xué)會了創(chuàng)建表空間,用戶,創(chuàng)建角色使得安全模式的設(shè)置和管理容易,使用grant和revoke語句授予和撤銷對象權(quán)限。創(chuàng)建表和修改表的存儲參數(shù)。實(shí)驗(yàn)四 約束、索引、視圖、序列和同義詞一、 實(shí)驗(yàn)?zāi)康?.

25、理解如何在表中定義各種約束,以保證數(shù)據(jù)的完整性2. 理解索引的作用,了解oracle提供的主要索引類型,以及各自的特點(diǎn),掌握如何創(chuàng)建各種索引3.在用戶模式中創(chuàng)建視圖、可更新視圖,理解視圖的相關(guān)性4. 對序列對象進(jìn)行管理5管理同義詞二、 實(shí)驗(yàn)設(shè)備一臺pc機(jī),widows操作系統(tǒng),oracle 10g三、 實(shí)驗(yàn)內(nèi)容1.創(chuàng)建表student的主鍵,外鍵,唯一和其他約束。2.為school模式中的student表創(chuàng)建索引,并查看索引的使用情況。1) 在student表的sname列上創(chuàng)建b樹非唯一索引:2) 在student的班級信息列上創(chuàng)建位圖索引:3) 查看oracle的執(zhí)行路徑。4. 在scho

26、ol模式中創(chuàng)建視圖,查詢視圖的定義,并對視圖進(jìn)行更新。1)創(chuàng)建一個視圖student_math。這個視圖基于school模式中的student表,并且該視圖只包括那些系別為“數(shù)學(xué)系”的學(xué)生信息。在創(chuàng)建視圖時使用with check option,防止更新視圖時,輸入非數(shù)學(xué)系的學(xué)生信息。2)創(chuàng)建一個連接視圖department_class,它包含class表中的classname列和department表的depname列,在wherer子句中將兩個表連接在一起。3)查看視圖各個列是否允許更新。5. 創(chuàng)建序列,序列的開始數(shù)字為100,每次遞增10,無限增長。6. 創(chuàng)建公有同義詞emp 為hr.e

27、mployees 四、實(shí)驗(yàn)步驟1.創(chuàng)建表student的主鍵,外鍵,唯一和其他約束。1)在tempuser模式中創(chuàng)建表student,classconnect tempuser/oracle;create table student(id varchar2(20),name varchar2(8),sex number(1),classid varchar2(20)tablespace users;create table class(id varchar2(20),principal varchar2(20),tel varchar2(20)2)使用sql或可視化工具創(chuàng)建表student的主

28、鍵,外鍵,唯一和其他約束。2.為school模式中的student表創(chuàng)建索引,并查看索引的使用情況。1) 連接到school模式,使用如下語句在student表的sname列上創(chuàng)建b樹非唯一索引:connect school/school;create index sname_index on student(sname) tablespace users;2) 在student的班級信息列上創(chuàng)建位圖索引:create bitmap index sclass_index on student(sclass) tablespace users;3)輸入并執(zhí)行如下語句,查看該語句是否會使用索引。

29、set autotrace traceonly select * from student where sname=劉麗;可以看出使用該select語句使用了索引sname_index4. 在school模式中創(chuàng)建視圖,查詢視圖的定義,并對視圖進(jìn)行更新。1)創(chuàng)建一個視圖student_math。這個視圖基于school模式中的student表,并且該視圖只包括那些系別為“數(shù)學(xué)系”的學(xué)生信息。在創(chuàng)建視圖時使用with check option,防止更新視圖時,輸入非數(shù)學(xué)系的學(xué)生信息。create or replace view student_math as select * from scho

30、ol.student where sclass in ( select ame from class c,department d where c.department=d.didand d.dname=數(shù)學(xué)系) with check option;2)創(chuàng)建一個連接視圖department_class,它包含class表中的classname列和department表的depname列,在wherer子句中將兩個表連接在一起。輸入并執(zhí)行下列sql語句:cteate or replace view department_class asselect d.dname,amefrom class c

31、,department dwhere c.department=d.did;3)查看視圖各個列是否允許更新。輸入并執(zhí)行語句:col owner format a20col table_name format a20col column_name format a20select *from user_updatable_columnswhere table_name=upper(department_class);5. 創(chuàng)建序列,序列的開始數(shù)字為100,每次遞增1,無限增長。輸入并執(zhí)行如下語句:create sequence incr_idstart with 100increment by

32、1nomaxvaluenocycle order;6. 創(chuàng)建公有同義詞hr_emp 為hr.employees輸入并執(zhí)行如下語句:create public synonym hr_emp for hr.employees 五、實(shí)驗(yàn)小結(jié)通過本次實(shí)驗(yàn)初步掌握如何創(chuàng)建表各種約束,以保證數(shù)據(jù)的完整性理解索引的作用,掌握創(chuàng)建各種索引。掌握了創(chuàng)建視圖,創(chuàng)建序列和同義詞。實(shí)驗(yàn)五 pl/sql編程基礎(chǔ)一、 實(shí)驗(yàn)?zāi)康?.學(xué)習(xí)pl/sql程序塊的結(jié)構(gòu)2.pl/sql程序中使用的數(shù)據(jù)類型3.如何在pl/sql中使用各種變量和常量4.pl/sql程序的條件判斷語句5.pl/sql程序的循環(huán)控制語句6.在pl/sql程

33、序中使用游標(biāo)處理表中的信息二、實(shí)驗(yàn)設(shè)備一臺pc機(jī),widows操作系統(tǒng),oracle 10g三、實(shí)驗(yàn)內(nèi)容 1.在pl/sql程序中,為了處理數(shù)據(jù)庫中存儲的數(shù)據(jù),用戶可以使用系統(tǒng)預(yù)定義的標(biāo)量變量,也可以使用自定義的復(fù)合變量。本練習(xí)將演示如何使用各種類型的變量:1)使用標(biāo)量變量。標(biāo)量變量是最簡單的變量,它只能存儲單個值,它的數(shù)據(jù)類型也是系統(tǒng)預(yù)定義的。2)使用%type類型的變量。使用%type類型的變量后,如果用戶隨后修改數(shù)據(jù)庫中該列的結(jié)構(gòu),則該類型變量的值也隨之改變。3)使用記錄變量。使用記錄變量首先需要用戶定義記錄變量的類型,然后才可以聲明記錄類型的變量。4) 使用%rowtype變量。%ro

34、wtype變量的結(jié)構(gòu)與數(shù)據(jù)庫中行的結(jié)構(gòu)完全相同。它可以存儲數(shù)據(jù)表中一個完整的行。2.使用游標(biāo) 在程序中訪問數(shù)據(jù)庫表最通用的方法是嵌入sql語句,由于sql語句一般以集合的形式返回結(jié)果,而程序設(shè)計語言并不能處理集合形式的數(shù)據(jù)。因此,需要使用游標(biāo)架起這兩者之間連接的“橋梁”。本練習(xí)訪問數(shù)據(jù)庫最簡單的方法是使用for cursor。該方法作為一種1 ) 隱式游標(biāo),它不需要用戶顯示定義、打開等操作,就可以瀏覽數(shù)據(jù)庫中的表?,F(xiàn)在使用該語句瀏覽school 模式中的student 表。2 ) 使用顯示游標(biāo)。這次使用不帶參數(shù)的顯示游標(biāo),瀏覽school模式中的student表。3) 使用游標(biāo)變量。游標(biāo)變量是

35、動態(tài)的,可以在打開游標(biāo)時指定游標(biāo)所使用的select語句。4) 下一個要使用的游標(biāo)是更新游標(biāo)。更新游標(biāo)需要使用 for update 子句。四、實(shí)驗(yàn)步驟1.在pl/sql程序中,為了處理數(shù)據(jù)庫中存儲的數(shù)據(jù),用戶可以使用系統(tǒng)預(yù)定義的標(biāo)量變量,也可以使用自定義的復(fù)合變量。1)在sql*plus中連接到school模式: connect school/school;輸入如下語句:set serveroutput on; declarevar_score number;beginvar_score:=89;update grade set score=var_scorewhere sid=s06037

36、201 and coid=c0154;end;在該程序中定義一個number類型的變量,為其賦值89。當(dāng)運(yùn)行時,程序塊中的update 語句將使用此值修改grade中的分?jǐn)?shù)score 字段。2) sql*plus中輸入如下語句: declarevar_name student.sname%type;beginselect snameinto var_namefrom studentwhere sid=s06037201;dbms_output.put_line(var_name);end;運(yùn)行該程序?qū)@示編號為s06037201的學(xué)生姓名。3)在sql*plus中輸入如下程序,以顯示某學(xué)生信息

37、:declaretype student_info is record(stu_id student.sid%type,stu_name student.sname%type);rec_student student_info;beginselect sid,snameinto rec_studentfrom studentwhere sid=s06037201;dbms_output.put_line(rec_student.stu_id);dbms_output.put_line(rec_student.stu_name);end;4) sql*plus中輸入如下程序,以顯示student

38、表中一個完整的行。declarevar_student student%rowtype;beginselect*into var_studentfrom studentwhere sid=s06037242;dbms_output.put_line(var_student.sid)dbms_output.put_line(var_student.sname);dbms_output.put_line(var_student.sclass);end;2.使用游標(biāo) 1 ) 隱式游標(biāo),它不需要用戶顯示定義、打開等操作,就可以瀏覽數(shù)據(jù)庫中的表。現(xiàn)在使用該語句瀏覽school 模式中的student 表

39、。set serveroutput onbeginfor studenttable in(select sid ,sname,sbirthfrom student)loopdbms_output.put(studenttable.sid);dbms_output.put(studenttable.sname);dbms_output.put_line(studenttable.sbirth);end loop;end;2 ) 使用顯示游標(biāo)。這次使用不帶參數(shù)的顯示游標(biāo),瀏覽school模式中的student表。在sql*plus中輸入并執(zhí)行如下語句:declarecursor student_c

40、ur isselect * from student;student_row student%rowtype;beginopen student_cur;loopfetch student_cur into student_row;exit when student_cur%notfound;dbms_output.put_line(student_row.sid);dbms_output.put_line(student_row.sname);end loop;close student_cur;end;3) 使用游標(biāo)變量。游標(biāo)變量是動態(tài)的,可以在打開游標(biāo)時指定游標(biāo)所使用的select語句。

41、 在sql*plus中輸入并執(zhí)行如下sql 語句,該語句通過游標(biāo)變量瀏覽student 表:declaretype student_rc is ref cursor;v_rc student_rc;student_row student%rowtype;beginopen v_rc for select* from student;loopfetch v_rc into student_row;exit when v_rc%notfound;dbms_output.put_line(student_row.sid);dbms_output.put_line(student_row.sname)

42、;end loop;close v_rc;end;4) 下一個要使用的游標(biāo)是更新游標(biāo)。更新游標(biāo)需要使用 for update 子句。在sql*plus中輸入并執(zhí)行如下語句:declarecursor grade_cur is select * from gradefor update of score nowait;var_score number;beginfor r in grade_cur loopif r.score studentid); end; 執(zhí)行結(jié)果將顯示指定編號的學(xué)生姓名、所在班級和系的名稱。5) 輸入并執(zhí)行如下語句:create or replace package us

43、er_pkg is tupe student_info is record( id student.sid%type, name student.sname%type, birth student.sbirth%type); procedure printstudentinfo; function getstudentinfo(id in varchar2) return student_info;end user_pkg;執(zhí)行這段代碼將返回“程序包已經(jīng)創(chuàng)建”的信息。6)輸入并執(zhí)行如下語句: create or replace package body user_pkg is cursor s

44、tudent_cur is select sid,sname,sbirth,saddresss from student; studentinfo student_info; begin open student_cur; fetch student_cur into studentinfo; while student_cur%found loop dbms_output.put(v_sthdentinfo.id | ); dbms_output.put(v_ | ); dbms_output.put(v_sthdentinfo.birth| ); dbms_output.put_line(v_sthdentinfo.addr | ); end loop; close student_cur; exception when invalid_cursor then dbms_output.pu

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論