oracle 11g 學(xué)習(xí)筆記

1、Check規(guī)則

Check (Agebetween15and30 )把年齡限制在15~30歲之間

2、新SQL語(yǔ)法

在調(diào)用某一函數(shù)時(shí)摆屯,可以通過“=>符號(hào)來(lái)為特定的函數(shù)參數(shù)指定數(shù)據(jù)。而在Oracle11g中,在SQL語(yǔ)句中也可以這樣的語(yǔ)法栏笆,例如:

Select f( x => 6)from dual ;

3、將sequence的值賦給變量

11g之前的賦值方式Select seq_x.next_val intov_x from dual ;

11g賦值方式v_x :=seq_x.next_val ;

4臊泰、SQL的功能

?4.1數(shù)據(jù)定義

Create ,Drop ,Alter

?4.2數(shù)據(jù)操縱

Select , insert , update , delete

?4.3數(shù)據(jù)控制

Grant, Revoke

5蛉加、Oracle創(chuàng)建語(yǔ)法

?1、表空間

createtablespacelq_oradatadatafile'd:\oracle_test\user_data01.dbf'

size32mautoextendonnext32mmaxsize2048mextentmanagementlocaluniformsize16m ;

?2、臨時(shí)表空間

createtablespacelq_oradata_temptempfile

'd:\oracle_test\temp_data01.dbf'

size32mautoextendonnext32mmaxsize2048mextentmanagementlocaluniformsize16m ;

?3针饥、創(chuàng)建用戶

create user lq identified by lq

default tablespacelq_oradata

temporary tablespace temp;

注:此處的temp為系統(tǒng)臨時(shí)表空間祟偷,也可以自定義臨時(shí)表空間;

?4打厘、給用戶授權(quán)

grant connect,resource to lq;

?5、創(chuàng)建表

createtableit_employees (

employee_idnumber(6)notnullunique,

first_namevarchar2(30) ,

last_namevarchar2(30)notnull,

emailvarchar2(30),

phone_numbervarchar2(15),

job_idvarchar2(10),

salary_numbernumber(8,2),

manager_idnumber(6)

)

添加數(shù)據(jù)

insertintoit_employees(employee_id,first_name,last_name,email,phone_number,job_id,salary_number,manager_id,birth_date)

values(seq_employee.nextval,'Micheal','Joe','mj_aui01@188.com','13149258943','it_prog',5000.67,seq_employee.nextval,to_date('2011-11-01','yyyy-mm-dd'));

?6贺辰、創(chuàng)建視圖

給用戶分配創(chuàng)建視圖的權(quán)限

A:首先授予查詢所有表的權(quán)限

grant select any

table tolq;

B:再次授予查詢所有字典表的權(quán)限

grant select any

dictionary tolq;

C:如果已經(jīng)分配以上權(quán)限户盯,還是權(quán)限不足,則可分配管理員權(quán)限

grant dba tolq;

createviewprog_employees

as

selectemployee_id,first_name,last_name,email,phone_number,salary_number,manager_id

fromit_employees

wherejob_id='it_prog'

with check option;

注:可加上with check option;

?7、創(chuàng)建索引

聚簇索引:指索引向的順序與表中記錄的物理順序相一致的索引組織饲化。

用戶可以在查詢頻率最高的列上建立聚簇索引莽鸭,從而提高查詢效率。由于吃靠,聚簇索引是將索引和表記錄放在一起存儲(chǔ)硫眨,所以在一個(gè)基表上只能創(chuàng)建一個(gè)聚簇索引;在建立聚簇索引后巢块,由于更新索引列數(shù)據(jù)時(shí)會(huì)導(dǎo)致表中物理順序的變更礁阁,系統(tǒng)代價(jià)較高,因此對(duì)于經(jīng)常更新的列不宜建立聚簇索引族奢。

Create [unique] [cluster] index [索引名]

On <表名> (<列名>[<次序>]姥闭,<列名>[<次序>],<列名>[<次序>]……)

其中越走,unique表示此索引的每一個(gè)索引值不能重復(fù)棚品,對(duì)應(yīng)唯一的數(shù)據(jù)記錄。Cluster表示要?jiǎng)?chuàng)建的索引為聚簇索引廊敌。索引可以建立在對(duì)應(yīng)表的一列或者多列上铜跑,如果是多個(gè)列,各個(gè)列之間需要用逗號(hào)分隔骡澈。<次序>用于指定索引值的排列次序锅纺,ASC表示升序,DESC表示降序秧廉,默認(rèn)為ASC.

Create indexit_lastnameonit_employees(last_name)

描述:執(zhí)行后會(huì)在表it_employees的last_name列上建立一個(gè)索引伞广。

而it_employees表中的數(shù)據(jù)將按照l(shuí)ast_name值升序存放。

6疼电、Oracle刪除語(yǔ)法Drop

?6.1:刪除表

Droptable<表名>

刪除表時(shí)嚼锄,表中的數(shù)據(jù)、在該表上建立的索引一并都會(huì)被刪除蔽豺。

?6.2:刪除視圖

Drop view <視圖名>

?6.3:刪除索引

Drop index <索引名>

7区丑、Oracle修改語(yǔ)法Alter

Alter table <表名>

[add <新列><數(shù)據(jù)類型>[完整性約束]]

新增birth_date列

Altertableit_employeesaddbirth_date date;

[Drop<完整性約束>]

刪除employee_id字段的unique約束

Altertableit_employeesDrop unique(employee_id);

[Modify <列名> [數(shù)據(jù)類型]]

將manager_id字段改為8位

Altertableit_employeesmodifymanager_id number(8)

8、Oracle查詢select

?8.1:簡(jiǎn)單查詢

8.1.1使用from子句指定表

select * from hr.countries ;

select * from hr.countries,hr.departments;

8.1.2使用select指定列

Selectcolumn name_1, ……

column name_n

Fromtable_name_1 , table_name_2 .

8.1.3算術(shù)表達(dá)式

Selectem.first_name,em.last_name,

em.salary_number*(1+0.2) "new_salar y"

fromit_employees em;

查詢出員工薪資上調(diào)10%之后的結(jié)果,為了提高刻度性沧侥,可以為列重新制定顯示標(biāo)題"new_salar y"可霎;

注:如何列標(biāo)題中包含一些特殊字符,例如空格等宴杀,則必須使用雙引號(hào)將列標(biāo)題擴(kuò)起來(lái)癣朗。

8.1.3distinct關(guān)鍵字

Select distinct job_id from it_employees;

?8.2:where子句

8.2.1條件表達(dá)式

selectem.employee_id, em.first_name,em.last_namefromit_employees emwhereem.first_namelike'B%';

判斷first_name以“B”開頭的雇員;

8.2.2連接運(yùn)算and / or

selectem.employee_id, em.first_name,em.last_namefromit_employees em

whereem.phone_number='15321981677'

andem.salary_number >6000;

用and做連接符旺罢,電話號(hào)碼和薪酬為判斷條件旷余;

-------------------------------------------------

selectem.employee_id, em.first_name,em.last_namefromit_employees em

whereem.first_name='Britney'

orem.salary_number <6000;

8.2.3 NULL值

首先插入一條記錄,此處Email只為NULL

insertintoit_employees(employee_id,first_name,last_name,email,phone_number,job_id,salary_number,manager_id,birth_date)

values(seq_employee.nextval,'Britney','Joe',NULL,'15321981677','it_prog',10000.67,seq_employee.nextval,to_date('2011-11-01','yyyy-mm-dd'));

查詢值為null的語(yǔ)句:

select*fromit_employees emwhereem.emailisnull;

select*fromit_employees emwhereem.emailisnotnull;

?8.3:order by子句

selectem.last_name, em.job_id , em.salary_number

fromit_employees em

whereem.salary_number >5000

orderbyem.job_id,em.salary_numberdesc;

order by子句后邊可指定多個(gè)列名扁达,首先根據(jù)第一列排序正卧,當(dāng)?shù)谝涣兄迪嗤瑫r(shí),再對(duì)第二列進(jìn)行比較排序跪解,以此類推炉旷;

?8.4:groupby子句

通過Group By進(jìn)行的查詢:

selectem.job_id,avg(em.salary_number),sum(em.salary_number),max(em.salary_number),count(em.job_id)

fromit_employees emgroupbyem.job_id ;

可以在group by后使用rollup或者cube進(jìn)行匯總,在查詢結(jié)果中都會(huì)附加一條匯總信息叉讥,sql如下:

selectem.job_id,avg(em.salary_number),sum(em.salary_number),max(em.salary_number),count(em.job_id)

fromit_employees emgroupbyrollup(em.job_id) ;

?8.5:Having子句

selectjob_id ,avg(SALARY_NUMBER) ,sum(SALARY_NUMBER),max(SALARY_NUMBER),count(*)

fromIT_EMPLOYEESgroupbyJOB_IDhavingavg(SALARY_NUMBER) >5500

通常與Group by子句一起使用窘行,在完成對(duì)分組統(tǒng)計(jì)后,可以使用Having子句對(duì)分組的結(jié)果做進(jìn)一步篩選节吮;

?8.6:多表連接查詢

8.6.1基本形式

將表employees和表departments相連接抽高,從而生成一個(gè)笛卡積:

Selectemployee_id, last_name , department_name fromemployees , departments ;

8.6.2條件限定

selectit.employee_id,it.last_name, dm.department_name

fromit_employees it , departments dm

whereit.department_id = dm.department_id;

selectit.employee_id,it.last_name, dm.department_name

fromit_employees it , departments dm

whereit.department_id = dm.department_id

anddm.department_name ='Shipping';

8.6.3表別名

8.6.4 Join連接

Fromjoin_table1join_typejoin_table2

[ON(join_Condition)]

8.6.4.1內(nèi)連接

select em.employee_id, em.last_name,dep.department_name

from it_employees eminnerjoindepartments dep

on em.department_id = dep.department_id

where em.job_id='it_prog';

8.6.4.2自然連接

Selectem.employee_id, em.first_name, em.last_name, dep.department_name fromit_employees emnatural

joindepartmentsdepwhere dep.department_name=’Sales’

8.6.4.3外連接

左外連接:

selectem.employee_id, em.last_name, dep.department_name

fromit_employees emleftouterjoindepartments dep

onem.department_id = dep.department_id

whereem.job_id='it_prog';

右外連接:

selectem.employee_id, em.last_name, dep.department_name

fromit_employees emrightouterjoindepartments dep

onem.department_id = dep.department_id

wheredep.location_id =1700;

完全外連接:

selectem.employee_id, em.last_name, dep.department_name

fromit_employees emfullouterjoindepartments dep

onem.department_id = dep.department_id

wheredep.location_id =1700or em.job_id=’it_prog’;

8.6.4.4自連接

selectem1.last_name "manager" , em2.last_name "employee"

fromit_employees em1leftjoinit_employees em2

onem1.employee_id = em2.manager_id

orderbyem1.employee_id;

?8.7:集合操作

Union (并運(yùn)算)UNION ALL透绩、InTerSect(交運(yùn)算)翘骂、Minus (差運(yùn)算)

8.7.1Union

/*** < unionall>***/形成的結(jié)果集中包含有兩個(gè)結(jié)果集中重復(fù)的行。

selectit.employee_id, it.last_namefromit_employeesit

whereit.last_namelike'%e'orit.last_namelike'j%'

union

selectem.employee_id , em.last_namefromit_employees em

whereem.last_namelike'j%'orem.last_namelike'%o%';

8.7.2Intersect

Intersect查詢結(jié)果保留Last_name以j開頭的雇員

selectit.employee_id, it.last_namefromit_employeesit

whereit.last_namelike'%e'orit.last_namelike'j%'

intersect

selectem.employee_id , em.last_namefromit_employees em

whereem.last_namelike'j%'orem.last_namelike'%o%';

8.7.3Minus

例:返回Last_name以e結(jié)尾的集合

selectit.employee_id, it.last_namefromit_employeesit

whereit.last_namelike'%e'orit.last_namelike'j%'

minus

selectem.employee_id , em.last_namefromit_employees em

whereem.last_namelike'j%'orem.last_namelike'%o%';

?8.8:子查詢

8.7.1in關(guān)鍵字

selectemployee_id, last_name , department_id

from it_employees

where department_idIn

(

selectdepartment_id fromdepartments

wherelocation_id=1700

)

8.7.2Exists關(guān)鍵字

selectemployee_id, last_name , department_id

from it_employeesem

whereexists

(

select * fromdepartmentsdep

where em.department_id= dep.department_id

andlocation_id=1700

)

8.7.3比較運(yùn)算符

selectemployee_id , last_name , job_id , SALARY_NUMBER

fromit_employees

whereJOB_ID ='it_prog'

andSALARY_NUMBER >= (selectavg(SALARY_NUMBER)fromit_employeeswherejob_id='it_prog')

9帚豪、數(shù)據(jù)操縱

?9.1:Insert

9.1.1一般Insert

Insertinto[user.]table[@db_link] [([column1, column2… culumnx])]

Values([express1],[ express2]……)

insertintojobs (job_id,job_title,min_salary,max_salary)

values('it_test','測(cè)試員',3500.00,8000.00);

/***注:以下Desc… insert未測(cè)試成功***/

descjobs

insertintojobsvalues('it_dba','管理員',5000.00,15000.00)

9.1.2批量Insert

insertintoemployees(employee_id,first_name,last_name,email,phone_number,job_id,salary_number,manager_id)

selectem.employee_id,

em.first_name,

em.last_name,

em.email,

em.phone_number,

em.job_id,

em.salary_number,

em.manager_id

fromit_employees em , departments dep

whereem.department_id = dep.department_id

anddep.department_name='Shipping';

?9.2:Update

updateemployeessetsalary_number = salary_number*1.15

wherejob_id ='it_prog'

updateit_employeessetsalary_number=

(

select avg(salary_number) fromit_employees

where job_id='it_prog'

)

whereemployee_id=11;

?9.3:Delete

deletefromit_employeeswhereemployee_id=31;

?9.4: Truncate

truncatetableemployees;

10碳竟、數(shù)據(jù)控制

?10.1 Grant語(yǔ)句

Grantselect on table it_employees to User1 ;

Grantallprivileges on table it_employees , jobs toUser2 ;

Grant selecton table department to public ;

Grant update(employee_id), select on tableit_employees to User4;

Grant insert on table departmentto user5 with grant option ;

Grant createtab on database db_employees toUser8 ;

?10.2Revoke語(yǔ)句

Revokeupdate(employee_id)on tableit_employees from user4 ;

Revokeselect on table departmentfrompublic;

Revokeinsert on table department from User5 ;

11、Oracle常用函數(shù)

?11.1字符類函數(shù)

11.1.1 ASCII()

Selectascii('A')big_a,ascii('a') small_afromdual ;

11.1.2CHR()

Selectchr(65) ,chr(97),chr(100)fromdual ;

11.1.3Concat( c1,c2 )

selectconcat('oracle','11g')fromdual ;

返回的是

11.1.4initcap(C1)

selectinitcap('oracleuniversal installer')fromdual ;

返回的結(jié)果:

11.1.5 instr(c1,[c2,,[j]])

select instr('Moisossoppo','o',3,3) from dual;

返回的結(jié)果:

select instr('Moisossoppo','o',-2,3) fromdual ;

返回的結(jié)果:

11.1.6length(C1)

selectlength('oracle

11g') lgfromdual ;

結(jié)果:

11.1.7 lower(C1)

select*fromjobs;

結(jié)果:

selectlower(job_id)fromjobswherelower(job_id)like'it%'

結(jié)果:

11.1.8Ltrim(c1,c2)

selectltrim('Moisossoppo','Mois')fromdual ;

結(jié)果:

11.1.9replace(c1,c2,[c3])

selectreplace('feelblue','blue','yellow')fromdual ;

結(jié)果:

11.1.10substr(c1,,[j])

selectsubstr('Message',1,4)fromdual ;

Result :

?11.2數(shù)字類函數(shù)

?11.3日期類函數(shù)

?11.4轉(zhuǎn)換類函數(shù)

?11.5聚集類函數(shù)

第四章:Oracle PL/SQL語(yǔ)言及編程

?4.1 PL/SQL簡(jiǎn)介

4.1.1pl/sql的基本結(jié)構(gòu)

4.1.2pl/sql注釋

A:?jiǎn)涡凶⑨?/p>

createtableclasses (

class_idnumber(6)primarykey,

departmentchar(3)

)

altertableclassesaddcoursenumber ;

createsequenceseq_class_id

incrementby1--每次增加1

startwith100--100開始

nomaxvalue--不設(shè)置最大值

nocycle--一直累加不循環(huán)

cache10;

declare--單行注釋聲明

v_departmentchar(3);--保存3個(gè)字符的變量

--系統(tǒng)代碼

v_Coursenumber;--保存課程號(hào)的變量

begin

v_department:='tes';

v_Course :=19;

insertintoclasses(class_id,department , course)

values(seq_class_id.nextval,v_department,v_Course);

end;

commit;

B:多行注釋

/**

多行注釋

**/

declare

v_departmentchar(3);

v_Coursenumber;

begin

v_department :='duo';

v_Course :=99;

insertintoclasses(class_id,department ,course)

values(seq_class_id.nextval,v_department,v_Course);

end

;

commit;

4.1.3pl/sql字符集

A:合法字符集

B:分界符(delimiter)

4.1.4pl/sql數(shù)據(jù)類型

A:數(shù)字類型

B:字符類型

C:日期類型

D:布爾類型

E:type定義的數(shù)據(jù)類型(rowtype)

例:

/**

pl/sql數(shù)據(jù)類型使用type定義teacher_record記錄變量

**/

typeteacher_recordisRecord--record定義之后狸臣,在以后的使用中就可以定義基于teacher_record的記錄變量

(

tidnumber(5)notnull:=0,

namevarchar2(50),

titlevarchar2(50),

sexchar(1)

)

--定義一個(gè)teacher_record類型的記錄變量

ateacher

teacher_record ;

4.1.5pl/sql變量和常量

A:定義常量

Pass_Scoreconstantinteger:=60;

B:定義變量

C:變量初始化

4.1.6pl/sql語(yǔ)句控制結(jié)構(gòu)

A:選擇結(jié)構(gòu)

(1)If語(yǔ)句

(2)Case語(yǔ)句

declare

v_gradevarchar2(20) :='及格';

v_scorevarchar2(50);

begin

v_score:=Casev_grade

when'不及格'then'成績(jī)< 60'

when'及格'then'60<=成績(jī)< 70 '

when'中等'then'70<=成績(jī)< 80 '

when'良好'then'80<=成績(jī)< 90 '

when'優(yōu)秀'then'9 0<=成績(jī)< 100 '

else'輸入有誤'

end;

dbms_output.put_line(v_score);

end;

運(yùn)行結(jié)果如下:

B:NULL結(jié)構(gòu)

/**

null結(jié)構(gòu)(添加變量是否為null的判斷)

**/

declare

v_number1number;

v_number2number;

v_resultvarchar2(7);

begin

ifv_number1isnullorv_number2isnullthen

v_result ='Unknown';

elsifv_number1

v_result ='yes'

elsev_result ='no'

end;

end;

C:循環(huán)結(jié)構(gòu)

(1)Loop…exit … end語(yǔ)句

declare

control_valnumber:=0;--初始化control_val值為0

begin

loop--開始循環(huán)

dbms_output.put_line(control_val);--打印測(cè)試

ifcontrol_val >5then--如果control_val的值大于5則退出循環(huán)

exit;

endif;

control_val :=control_val+1;--否則改變control_val的值

endloop;--結(jié)束循環(huán)

end;

(2)Loop… exitwhen …end語(yǔ)句

declare

control_valnumber:=0;--初始化control_val值為0

begin

loop--開始循環(huán)

dbms_output.put_line(control_val);--打印測(cè)試

exitwhencontrol_val >6;--如果control_val的值大于6則退出循環(huán)

control_val:=control_val+1;

endloop;

end;

(3)While…loop…end;

(4)declare

(5)control_valnumber:=0;

(6)begin

(7)whilecontrol_val <=5--如果變量小于或等于5就循環(huán)

(8)loopcontrol_val:=control_val+1;

(9)dbms_output.put_line(control_val);--打印測(cè)試

(10)endloop;

(11)end;

(4)for… in … loop …end

這是個(gè)預(yù)知循環(huán)次數(shù)的循環(huán)控制語(yǔ)句:

declare

control_valnumber:=0;

begin

forcontrol_valin0.. .9loop

dbms_output.put_line(control_val);

null;

endloop;

end;

null為空操作語(yǔ)句莹桅,它表示什么也不做,在程序中用來(lái)標(biāo)識(shí)此處可以加執(zhí)行語(yǔ)句烛亦,起到一種記號(hào)的作用诈泼;

(5)GOTO語(yǔ)句示例

4.1.7pl/sql表達(dá)式

A:字符表達(dá)式

declare

hnvarchar2(20) :='hello';

wnvarchar2(20) :='world!';

rtnvarchar2(20) ;

begin

ifhisnotnullandwisnotnull

thenrt:=h||w;

dbms_output.put_line(rt);

endif;

end;

運(yùn)行結(jié)果:

B:布爾表達(dá)式

begin

if 'Scott' not in ('Mike','John','Mary')

then dbms_output.put_line('false');

end if ;

end;

運(yùn)行結(jié)果:

?4.2 PL/SQL的游標(biāo)

4.2.1基本原理

4.2.2顯示游標(biāo)

顯示游標(biāo)的處理包括:聲明游標(biāo)、打開游標(biāo)煤禽、提取游標(biāo)铐达、關(guān)閉游標(biāo)4個(gè)步驟;其操作過程如下圖:

注:聲明游標(biāo)需要在塊的聲明部分進(jìn)行檬果,其他3步驟都在執(zhí)行部分或異常處理中進(jìn)行瓮孙。

A:聲明游標(biāo)

B:打開游標(biāo)

Open <游標(biāo)名>唐断;

1)檢查聯(lián)偏變量的取值;

2)根據(jù)聯(lián)偏變量的取值杭抠,確定活動(dòng)集脸甘;

3)活動(dòng)集指針指向第一行;

C:提取游標(biāo)

D:關(guān)閉游標(biāo)

Close <游標(biāo)名>偏灿;

查出employees表中的所有數(shù)據(jù):

執(zhí)行下邊定義的sql:

--1,游標(biāo)的聲明

declare

FIRST_NAMEVARCHAR2(30);--定義四個(gè)變量來(lái)存放employees表中的內(nèi)容

LAST_NAMEVARCHAR2(30);

EMAILVARCHAR2(30);

phone_numvarchar2(30);

cursoremployee_curis

selecte.first_name,e.last_name,e.email,e.phone_number

fromemployeese

wheree.employee_id<10;--選出編號(hào)大于5的所有雇員

--2,游標(biāo)的開啟

begin

openemployee_cur;

--3,提取游標(biāo)(fetch語(yǔ)句每執(zhí)行一次丹诀,游標(biāo)向后移動(dòng)一行,直到結(jié)束翁垂;游標(biāo)只能逐個(gè)向后移動(dòng)忿墅,而不能跳躍移動(dòng)或者向前移動(dòng))

fetchemployee_curintoFIRST_NAME,LAST_NAME,EMAIL,phone_num;

--4,關(guān)閉游標(biāo)

loop

exitwhennotemployee_cur%found;--如果游標(biāo)到尾則結(jié)束

ifFIRST_NAME='lq'then

dbms_output.put_line('名稱為:lq');

else

dbms_output.put_line('沒有合法用戶');

endif;

fetchemployee_curintoFIRST_NAME,LAST_NAME,EMAIL,phone_num;

endloop;

closeemployee_cur;

end;

得出的結(jié)果為:

注:使用顯式游標(biāo)時(shí),需要注意以下事項(xiàng):

4.2.3隱式游標(biāo)

4.2.4游標(biāo)屬性

A:是否找到游標(biāo)(%found)

B:是否沒找到游標(biāo)(%found)

C:游標(biāo)行數(shù)(%RowCount)

--1,游標(biāo)的聲明

declare

FIRST_NAMEVARCHAR2(30);--定義四個(gè)變量來(lái)存放employees表中的內(nèi)容

LAST_NAMEVARCHAR2(30);

EMAILVARCHAR2(30);

phone_numvarchar2(30);

cursoremployee_curis

selecte.first_name,e.last_name,e.email,e.phone_number

fromemployeese

wheree.employee_id<10;--選出編號(hào)大于5的所有雇員

--2,游標(biāo)的開啟

begin

openemployee_cur;

--3,提取游標(biāo)(fetch語(yǔ)句每執(zhí)行一次沮峡,游標(biāo)向后移動(dòng)一行,直到結(jié)束亿柑;游標(biāo)只能逐個(gè)向后移動(dòng)邢疙,而不能跳躍移動(dòng)或者向前移動(dòng))

fetchemployee_curintoFIRST_NAME,LAST_NAME,EMAIL,phone_num;

--4,關(guān)閉游標(biāo)

loop--如果游標(biāo)到尾則結(jié)束(判斷游標(biāo)當(dāng)前行數(shù),即當(dāng)前只抽取3條記錄)

exitwhennotemployee_cur%foundoremployee_cur%rowcount=3;

ifFIRST_NAME='lq'then

dbms_output.put_line('名稱為:lq');

else

dbms_output.put_line('沒有合法用戶');

endif;

fetchemployee_curintoFIRST_NAME,LAST_NAME,EMAIL,phone_num;

endloop;

closeemployee_cur;

end;

D:游標(biāo)是否打開(%IsOpen)

E:參數(shù)化游標(biāo)

--敲回車后會(huì)彈出輸入對(duì)話框望薄,輸入即可疟游。

--ACCEPT my_tid prompt 'please input the tid';

ACCEPTmy_tidprompt'please input the tid';

declare

my_tid

number:=10;--定義的參數(shù)或者直接用以上prompt輸入?yún)?shù)

FIRST_NAMEVARCHAR2(30);--定義四個(gè)變量來(lái)存放employees表中的內(nèi)容

LAST_NAMEVARCHAR2(30);

EMAILVARCHAR2(30);

phone_numvarchar2(30);

cursoremployee_cur(cursor_idnumber)is--定義游標(biāo)時(shí)帶上參數(shù)

selecte.first_name,e.last_name,e.email,e.phone_number

fromemployeese

wheree.employee_id

begin

ifemployee_cur%isopenthen

loop

fetchemployee_cur

intoFIRST_NAME,LAST_NAME,EMAIL,phone_num;

exitwhenemployee_cur%notfound;

endloop;

else

openemployee_cur(my_tid);--帶上實(shí)參數(shù)

loop

fetchemployee_cur

intoFIRST_NAME,LAST_NAME,EMAIL,phone_num;

exitwhenemployee_cur%notfound;

endloop;

endif;

closeemployee_cur;

end;

4.2.5游標(biāo)變量

Oracle 11 G從入門到精通閱讀至126頁(yè),文檔未完待續(xù)痕支。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末颁虐,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子卧须,更是在濱河造成了極大的恐慌另绩,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件花嘶,死亡現(xiàn)場(chǎng)離奇詭異笋籽,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)椭员,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門车海,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人隘击,你說我怎么就攤上這事侍芝。” “怎么了埋同?”我有些...
    開封第一講書人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵州叠,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我莺禁,道長(zhǎng)留量,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮楼熄,結(jié)果婚禮上忆绰,老公的妹妹穿的比我還像新娘。我一直安慰自己可岂,他們只是感情好错敢,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著缕粹,像睡著了一般稚茅。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上平斩,一...
    開封第一講書人閱讀 52,246評(píng)論 1 308
  • 那天亚享,我揣著相機(jī)與錄音,去河邊找鬼绘面。 笑死欺税,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的揭璃。 我是一名探鬼主播晚凿,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼瘦馍!你這毒婦竟也來(lái)了歼秽?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤情组,失蹤者是張志新(化名)和其女友劉穎燥筷,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體院崇,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡荆责,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了亚脆。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片做院。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖濒持,靈堂內(nèi)的尸體忽然破棺而出键耕,到底是詐尸還是另有隱情,我是刑警寧澤柑营,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布屈雄,位于F島的核電站,受9級(jí)特大地震影響官套,放射性物質(zhì)發(fā)生泄漏酒奶。R本人自食惡果不足惜蚁孔,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望惋嚎。 院中可真熱鬧杠氢,春花似錦、人聲如沸另伍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)摆尝。三九已至温艇,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間堕汞,已是汗流浹背勺爱。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留讯检,地道東北人邻寿。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像视哑,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子誊涯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • 一挡毅、select語(yǔ)句 日期和數(shù)值型字段可以進(jìn)行加減乘除 關(guān)于NULLNULL表示不可用,未賦值暴构,不知道跪呈,不適用,所...
    比軒閱讀 3,101評(píng)論 2 17
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法取逾,類相關(guān)的語(yǔ)法耗绿,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法砾隅,異常的語(yǔ)法误阻,線程的語(yǔ)...
    子非魚_t_閱讀 31,661評(píng)論 18 399
  • 數(shù)據(jù)操縱語(yǔ)言 DML(Data Manipulation Language – 數(shù)據(jù)操縱語(yǔ)言) 可以在下列條件下執(zhí)...
    MPPC閱讀 450評(píng)論 0 3
  • SQL語(yǔ)言基礎(chǔ) 本章,我們將會(huì)重點(diǎn)探討SQL語(yǔ)言基礎(chǔ)晴埂,學(xué)習(xí)用SQL進(jìn)行數(shù)據(jù)庫(kù)的基本數(shù)據(jù)查詢操作究反。另外請(qǐng)注意本章的S...
    厲鉚兄閱讀 5,332評(píng)論 2 46
  • 1、組函數(shù):組函數(shù)作用于一組數(shù)據(jù)儒洛,并對(duì)一組數(shù)據(jù)返回一個(gè)值精耐。 AVG 平均值COUNT計(jì)數(shù)MAX最大值MIN最小值S...
    Shaw_Chen閱讀 504評(píng)論 0 2