• <sub id="h4knl"><ol id="h4knl"></ol></sub>
    <sup id="h4knl"></sup>
      <sub id="h4knl"></sub>

      <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
      1. <strong id="h4knl"></strong>

      2. Oracle數(shù)據(jù)庫語句

        時間:2024-10-08 20:48:18 Oracle認(rèn)證 我要投稿

        Oracle數(shù)據(jù)庫語句大全

          Oracle數(shù)據(jù)庫是甲骨文公司的一款關(guān)系數(shù)據(jù)庫管理系統(tǒng)。它是在數(shù)據(jù)庫領(lǐng)域一直處于領(lǐng)先地位的產(chǎn)品。下面yjbys小編為大家分享的是Oracle數(shù)據(jù)庫查詢語句,希望能幫助到大家!

          一.入門部分

          1. 創(chuàng)建表空間

          create tablespace schooltbs datafile ‘D:oracledatasourceschooltbs.dbf’ size 10M autoextend on;

          2. 刪除表空間

          space schooltbs[including contents and datafiles];

          3. 查詢表空間基本信息

          select *||tablespace_name from DBA_TABLESPACES;

          4. 創(chuàng)建用戶

          create user lihua

          identified by lihua

          default tablespace schooltbs

          temporary tablespace temp;

          5. 更改用戶

          alter user lihua

          identified by 123

          default tablespace users;

          6. 鎖定用戶

          alter user lihua account lock|unlock;

          7. 刪除用戶

          drop user lihua cascade;--刪除用戶模式

          8. oracle數(shù)據(jù)庫中的角色

          connect,dba,select_catalog_role,_catalog_role,execute_catalog_role,exp_full_database,imp_full_database,resource

          9. 授予連接服務(wù)器的角色

          grant connect to lihua;

          10.授予使用表空間的角色

          grant resource to lihua with grant option;--該用戶也有授權(quán)的權(quán)限

          11.授予操作表的權(quán)限

          grant select, on user_tbl to scott;--當(dāng)前用戶

          grant ,on lihua.user_tbl to scott;--系統(tǒng)管理員

          12.修改表的結(jié)構(gòu)(alter)

          Alter table 表名 add(列的名稱,列的類型);

          二.SQL查詢和SQL函數(shù)

          1.SQl支持的命令:

          數(shù)據(jù)定義語言(DDL):create,alter,drop

          數(shù)據(jù)操縱語言(DML):,,update,select

          數(shù)據(jù)控制語言(DCL):grant,revoke

          事務(wù)控制語言(TCL):commit,savepoint,rollback

          2.Oracle數(shù)據(jù)類型

          字符,數(shù)值,日期,RAW,LOB

          字符型

          char:1-2000字節(jié)的定長字符

          varchar2:1-4000字節(jié)的變長字符

          long:2GB的變長字符

          注意:一個表中最多可有一列為long型

          Long列不能定義唯一約束或主鍵約束

          long列上不能創(chuàng)建索引

          過程或存儲過程不能接受long類型的參數(shù)。

          數(shù)值型

          number:最高精度38位

          日期時間型

          date:精確到ss

          timestamp:秒值精確到小數(shù)點后6位

          函數(shù)

          sysdate,systimestamp返回系統(tǒng)當(dāng)前日期,時間和時區(qū)。

          更改時間的顯示

          alter session set nls_date_language=’american’;

          alter session set nls_date_format=’yyyy-mm-dd’;

          Oracle中的偽列

          像一個表列,但沒有存儲在表中

          偽列可以查詢,但不能插入、更新和修改它們的值

          常用的偽列:rowid和rownum

          rowid:表中行的存儲地址,可唯一標(biāo)示數(shù)據(jù)庫中的某一行,可以使用該列快速定位表中的行。

          rownum:查詢返回結(jié)果集中的行的序號,可以使用它來限制查詢返回的行數(shù)。

          3.數(shù)據(jù)定義語言

          用于操作表的命令

          create table

          alter table

          truncate table

          修改表的命令

          alter table stu_table rename to stu_tbl;--修改表名

          alter table stu_tbl rename column stu_sex to sex;--修改列名

          alter table stu_tbl add (stu_age number);--添加新列

          alter table stu_tbl drop(sex);--刪除列

          alter table stu_tbl modify(stu_sex varchar2(2));--更改列的數(shù)據(jù)類型

          alter table stu_tbl add constraint pk_stu_tbl primary key(id);--添加約束

          4.數(shù)據(jù)操縱語言

          select,update,,

          利用現(xiàn)有的表創(chuàng)建表

          create table stu_tbl_log as select id,stu_name,stu_age from stu_tbl;--

          選擇無重復(fù)的行

          select distinct stu_name from stu_tbl;--

          插入來自其他表中的記錄

          into stu_tbl_log select id,stu_name,stu_age from stu_tbl;

          5.數(shù)據(jù)控制語言

          grant,revoke

          6.事務(wù)控制語言

          commit,savepoint,rollback

          7.SQL操作符

          算術(shù)操作符:L+-*/

          比較操作符:L=,!=,<>,>,<,>=,<=,between-and,in,like,is null等

          邏輯操作符:Land,or,not

          集合操作符:Lunion,union all,intersect,minus

          連接操作符:L||

          示例中stu_tbl_log中的數(shù)據(jù)如下:

          ID STU_NAME STU_AGE

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

          1000 李華 20

          1001 accp 20

          1003 nimda 3

          stu_tbl中的數(shù)據(jù)如下:

          ID STU_NAME ST STU_AGE

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

          1000 李華 男 20

          1001 accp 男 20

          1002 admin 男 30

          示例:

          select (3+2)/2 from dual;--算術(shù)操作符,結(jié)果:2.5

          select * from stu_tbl where stu_age>=20;--比較操作符

          select * from stu_tbl where stu_name like '%a%';--比較操作符:like

          select * from stu_tbl where stu_name like 'a___';--比較操作符:like

          select * from stu_tbl where stu_age in(20,30);--比較操作符:in

          select * from stu_tbl where stu_age between 20 and 30;--比較操作符:between

          select stu_name from stu_tbl union all

          select stu_name from stu_tbl_log;--集合操作符:union all,測試結(jié)果具體如下:

          STU_NAME

          -----------

          李華

          accp

          admin

          李華

          accp

          nimda

          已選擇6行。

          select stu_name from stu_tbl union

          select stu_name from stu_tbl_log;--集合操作符:union,測試結(jié)果具體如下:

          STU_NAME

          ---------

          accp

          admin

          nimda

          李華

          select stu_name from stu_tbl intersect

          select stu_name from stu_tbl_log;--集合操作符:intersect,測試結(jié)具體如下:

          STU_NAME

          ----------

          accp

          李華

          select stu_name from stu_tbl minus

          select stu_name from stu_tbl_log;--集合操作符:minus,測試結(jié)果如下:

          STU_NAME

          ----------

          Admin

          從中可以看出:

          minus是獲取第一張表獨有的數(shù)據(jù)

          intersect是獲取兩張表中都有的數(shù)據(jù)

          union是整合兩張表的數(shù)據(jù),都有的只顯示一次

          union all是純粹的兩張表數(shù)據(jù)整合

          select id,stu_name||' '||stu_sex as name_sex,stu_age

          from stu_tbl;--連接操作符||,測試結(jié)果具體如下:

          ID NAME_SEX STU_AGE

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

          1000 李華 男 20

          1001 accp 男 20

          1002 admin 男 30

          8.SQL函數(shù)

          單行函數(shù):從表中查詢的每一行只返回一個值,可出現(xiàn)在select子句,where子句中

          日期函數(shù)

          數(shù)字函數(shù)

          字符函數(shù)

          轉(zhuǎn)換函數(shù):ToChar(),ToDate(),ToNumber()

          其他函數(shù):

          Nvl(exp1,exp2):表達(dá)式一為null時,返回表達(dá)式二

          Nvl2(exp1,exp2,exp3):表達(dá)式一為null時返回表達(dá)式三,否則返回表達(dá)式二

          Nullif(exp1,exp2):兩表達(dá)式相等時,返回null,否則返回表達(dá)式一

          分組函數(shù):基于一組行來返回

          Avg,Min,Max,Sum,Count

          Group by,having

          分析函數(shù)

          Row_number,rank,dense_rank

          示例:

          select u.user_name,sum(oi.order_num*oi.order_price) as total,row_number() over (order by sum(oi.order_num*oi.order_price) desc) as sort from order_item_tbl

          oi,user_tbl u,order_tbl o where oi.order_id = o.id and o.user_id = u.id group by u.user_name;

          三.鎖和數(shù)據(jù)庫對象

          1.鎖:數(shù)據(jù)庫用來控制共享資源并發(fā)訪問的機制。

          鎖的類型:行級鎖,表級鎖

          行級鎖:對正在被修改的行進(jìn)行鎖定。行級鎖也被稱之為排他鎖。

          在使用下列語句時,Oracle會自動應(yīng)用行級鎖:

          ,update,,select…… for update

          select……for update允許用戶一次鎖定多條記錄進(jìn)行更新。

          使用commit or rollback釋放鎖。

          表級鎖:

          lock table user_tbl in mode mode;

          表級鎖類型:

          行共享 row share

          行排他 row exclusive

          共享 share

          共享行排他 share row exclusive

          排他 exclusive

          死鎖:兩個或兩個以上的事務(wù)相互等待對方釋放資源,從而形成死鎖

          2.數(shù)據(jù)庫對象

          oracle數(shù)據(jù)庫對象又稱模式對象

          數(shù)據(jù)庫對象是邏輯結(jié)構(gòu)的集合,最基本的數(shù)據(jù)庫對象是表

          數(shù)據(jù)庫對象:

          表,序列,視圖,索引

          序列

          用于生成唯一,連續(xù)序號的對象。

          創(chuàng)建語法:

          create sequence user_id_seq

          start with 1000

          increment by 1

          maxvalue 2000

          minvalue 1000

          nocycle

          cache 1000;--指定內(nèi)存中預(yù)先分配的序號

          訪問序列:

          select user_id_seq.currval from dual;

          select user_id-seq.nextval from dual;

          更改刪除序列:

          alter sequence user_id_seq maxvalue 10000;--不能修改其start with 值

          drop sequence user_id_seq;

          在Hibernate中訪問序列:

          user_id_seq

          視圖

          以經(jīng)過定制的方式顯示來自一個或多個表的數(shù)據(jù)

          創(chuàng)建視圖:

          create or replace view

          user_tbl_view (vid,vname,vage)

          as select id,user_name,age from user_tbl

          [with check option]|[with read only];

          創(chuàng)建帶有錯誤的視圖:

          create force view user_tbl_force_view as

          select * from user_table;--此時user_table可以不存在

          創(chuàng)建外聯(lián)接視圖:

          create view user_stu_view as

          select u.id,u.user_name,u.password,s.ddress

          from user_tbl u,stu_tbl s

          where u.s_id(+)=s.id;--哪一方帶有(+),哪一方就是次要的

          刪除視圖:

          drop user_stu_view;

          索引

          用于提高SQL語句執(zhí)行的性能

          索引類型:

          唯一索引,位圖索引,組合索引,基于函數(shù)的索引,反向鍵索引

          創(chuàng)建標(biāo)準(zhǔn)索引:

          create index user_id_index on user_tbl(id) tablespace schooltbs;

          重建索引:

          alter index user_id_index rebuild;

          刪除索引:

          drop index user_id_index;

          創(chuàng)建唯一索引:

          create unique index user_id_index on user_tbl(id);

          創(chuàng)建組合索引:

          create index name_pass_index on user_tbl(user_name,password);

          創(chuàng)建反向鍵索引:

          create index user_id_index on user_tbl(id) reverse;

          四.使用PL/SQL

          可用于創(chuàng)建存儲過程,觸發(fā)器,程序包,給SQL語句的執(zhí)行添加程序邏輯。

          支持SQL,在PL/SQL中可以使用:

          數(shù)據(jù)操縱命令

          事務(wù)控制命令

          游標(biāo)控制

          SQL函數(shù)和SQL運算符

          支持面向?qū)ο缶幊?OOP)

          可移植性

          更佳的性能,PL/SQL經(jīng)過編譯執(zhí)行

          分為三個部分:聲明部分,可執(zhí)行部分和異常處理部分

          [declare

          declarations]

          begin

          executable statements

          [exception

          handlers]

          end;

          打開輸出

          set serverout on;

          --根據(jù)輸入編號獲取某學(xué)員的成績--if

          declare

          score user_tbl.score%type;

          begin

          select score into score from user_tbl where id='&id';

          if score>90 then

          dbms_output.put_line('優(yōu)秀');

          elsif score>80 then

          dbms_output.put_line('良好');

          elsif score>60 then

          dbms_output.put_line('及格');

          else

          dbms_output.put_line('差');

          end if;

          end;

          --根據(jù)學(xué)員姓名獲取某學(xué)員的成績--if

          declare

          score user_tbl.score%type;

          begin

          select score into score from user_tbl where user_name='&name';

          if score>90 then

          dbms_output.put_line('優(yōu)秀');

          elsif score>80 then

          dbms_output.put_line('良好');

          elsif score>60 then

          dbms_output.put_line('及格');

          else

          dbms_output.put_line('差');

          end if;

          end;

          --case的使用

          declare

          grade user_tbl.grade%type;

          begin

          select grade into grade from user_tbl where id='&id';

          case grade

          when 'A' then dbms_output.put_line('優(yōu)異');

          when 'B' then dbms_output.put_line('優(yōu)秀');

          when 'C' then dbms_output.put_line('良好');

          else dbms_output.put_line('一般');

          end case;

          end;

          --基本循環(huán)

          declare

          i number(4):=1;

          begin

          loop

          dbms_output.put_line('loop size:'||i);

          i:=i+1;

          exit when i>10;

          end loop;

          end;

          --while循環(huán)

          declare

          i number(4):=1;

          begin

          while i<=10 loop

          dbms_output.put_line('while loop size='||i);

          i:=i+1;

          end loop;

          end;

          --for循環(huán)

          declare

          i number(4):=1;

          begin

          for i in 1..10 loop

          dbms_output.put_line('for loop Size:'||i);

          end loop;

          end;

          declare

          i number(2):=1;

          j number(2):=1;

          begin

          for i in reverse 1..9 loop

          for j in 1..i loop

          dbms_output.put(j||'x'||i||'='||j*i||' ');

          end loop;

          dbms_output.put_line('');

          end loop;

          end;

          --動態(tài)SQL

          declare

          userId number(2);

          sql_str varchar2(100);

          userName user_tbl.user_name%type;

          begin

          execute immediate 'create table testExe(id number,test_name varchar2(20))';

          userId:='&userId';

          sql_str:='select user_name from user_tbl where id=:id';

          execute immediate sql_str into userName using userId;

          dbms_output.put_line(userName);

          end;

          (or

          declare

          id_param number:='&id_param';

          sql_str varchar2(100);

          name_param stu_tbl.stu_name%type;

          begin

          sql_str:='select stu_name from stu_tbl where id=:p';

          execute immediate sql_str into name_param using id_param;

          dbms_output.put_line(name_param);

          end;

          /

          )

          --異常處理

          declare

          grade number(4);

          begin

          grade:='&grade';

          case grade

          when 1 then dbms_output.put_line('好的');

          --else dbms_output.put_line('不好');

          end case;

          exception

          when case_not_found then

          dbms_output.put_line('輸入類型不匹配!');

          end;

          --系統(tǒng)異常

          declare

          rowD user_tbl%rowtype;

          begin

          select * into rowD from user_tbl;

          dbms_output.put_line(rowD.id||''||rowD.user_name||' '||rowD.password);

          exception

          when too_many_rows then

          dbms_output.put_line('不能將多行賦予一個屬性!');

          end;

          or

          declare

          rowD user_tbl%rowtype;

          begin

          select * into rowD from user_tbl where id=5;

          dbms_output.put_line(rowD.id||' '||rowD.user_name||' '||rowD.password);

          exception

          when too_many_rows then

          dbms_output.put_line('不能將多行賦予一個屬性!');

          when no_data_found then

          dbms_output.put_line('沒有您要查找的數(shù)據(jù)!');

          end;

          --自定義錯誤

          declare

          invalidError exception;

          category varchar2(20);

          begin

          category:='&category';

          if category not in('附件','頂盤','備件') then

          raise invalidError;

          else

          dbms_output.put_line('您輸入的類別是:'||category);

          end if;

          exception

          when invalidError then

          dbms_output.put_line('無法識別的類別!');

          end;

          --引發(fā)應(yīng)用程序異常

          declare

          app_exception exception;

          grade user_tbl.grade%type;

          begin

          select grade into grade from user_tbl where id=&id;

          if grade='A' then

          raise app_exception;

          else

          dbms_output.put_line('查詢的等級為:'||grade);

          end if;

          exception

          when app_exception then

          raise_application_error(-20001,'未知的等級!');

          end;

          五、游標(biāo)管理

          游標(biāo)類型:隱式游標(biāo),顯式游標(biāo),REF游標(biāo)

          REF游標(biāo)用于處理運行時才能確定的動態(tài)SQL查詢的結(jié)果

          ==========隱式游標(biāo)==========

          在PL/SQL中使用DML語句時自動創(chuàng)建隱式游標(biāo)

          隱式游標(biāo)自動聲明、打開和關(guān)閉,其名為SQL

          隱式游標(biāo)的屬性:

          %found SQL語句影響實質(zhì)后返回true

          %notfound SQL語句沒有影響實質(zhì)后返回true

          %rowcount SQL語句影響的行數(shù)

          %isopen 游標(biāo)是否打開,始終為false

          示例:

          begin

          user_tbl set score=score+5;

          if SQL%found then

          dbms_output.put_line('數(shù)據(jù)被更改: '||SQL%rowcount);

          elsif sql%notfound then

          dbms_output.put_line('沒有找到數(shù)據(jù)!');

          end if;

          if SQL%isopen then

          dbms_output.put_line('Open');

          else

          dbms_output.put_line('Close');

          end if;

          end;

          ==========顯式游標(biāo)==========

          在PL/SQL的聲明部分定義查詢,該查詢可以返回多行

          J 聲明游標(biāo)

          J 打開游標(biāo)

          J 從游標(biāo)中取回數(shù)據(jù)

          J 關(guān)閉游標(biāo)

          聲明游標(biāo)完成兩個任務(wù):

          給游標(biāo)命名

          將一個查詢與游標(biāo)關(guān)聯(lián)

          cursor cursor_name is select statement;

          打開游標(biāo):

          open cursor_name;

          取數(shù)據(jù):

          fetch cursor_name into record_list;

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

          close cursor_name;

          顯式游標(biāo)的屬性:

          %found 執(zhí)行最后一條fetch語句成功返回行時為true

          %notfound 執(zhí)行最后一條fetch語句未能返回行時為true

          %rowcount 返回到目前為止游標(biāo)提取的行數(shù)

          %isopen 游標(biāo)是否打開

          示例:

          declare

          users user_tbl%rowtype;

          cursor boys_cur is select * from user_tbl where sex='h';

          begin

          open boys_cur;

          loop

          fetch boys_cur into users;

          exit when boys_cur%notfound;

          dbms_output.put_line(users.user_name||' '||users.password);

          dbms_output.put_line(boys_cur%rowcount);

          end loop;

          close boys_cur;

          end;

          帶參的顯式游標(biāo)

          declare

          users user_tbl%rowtype;

          cursor boys_cur(sexParam varchar2)

          is select * from user_tbl where sex=sexParam;

          begin

          open boys_cur('&sex');

          loop

          fetch boys_cur into users;

          exit when boys_cur%notfound;

          dbms_output.put_line(users.user_name||' '||users.password);

          dbms_output.put_line(boys_cur%rowcount);

          end loop;

          close boys_cur;

          end;

          使用顯式游標(biāo)更新行

          declare

          cursor user_update_cur is select sex from user_tbl for update;

          usersex user_tbl.sex%type;

          begin

          open user_update_cur;

          loop

          fetch user_update_cur into usersex;

          exit when user_update_cur%notfound;

          dbms_output.put_line(usersex);

          if usersex = 'M' then

          user_tbl set score=score-5 where current of user_update_cur;

          else

          user_tbl set score=score+5 where current of user_update_cur;

          end if;

          end loop;

          close user_update_cur;

          commit;

          end;

          循環(huán)游標(biāo)

          declare

          cursor user_cur is select * from user_tbl;

          begin

          for username in user_cur loop

          dbms_output.put_line(username.user_name||' '||username.sex);

          end loop;

          end;

          ==========REF游標(biāo)==========

          REF游標(biāo)和游標(biāo)變量用于處理運行時動態(tài)執(zhí)行的SQL查詢

          創(chuàng)建游標(biāo)變量的步驟:

          J 聲明REF游標(biāo)類型

          J 聲明REF游標(biāo)類型的變量

          聲明類型的語法

          Type ref_cursor_name is ref cursor [return return_type];

          打開游標(biāo)變量的語法

          Open cursor_name for select_statement;

          ----聲明強類型的游標(biāo)

          declare

          type ref_cur is ref cursor return user_tbl%rowtype;

          users_cur ref_cur;

          ----聲明弱類型的游標(biāo)

          declare

          type ref_cur is ref cursor;

          users_cur ref_cur;

          示例

          ----強類型

          declare

          type ref_cur is ref cursor return user_tbl%rowtype;

          users_cur ref_cur;

          users user_tbl%rowtype;

          begin

          open users_cur for select * from user_tbl where user_name='ny2t92';

          loop

          fetch users_cur into users;

          exit when users_cur%notfound;

          dbms_output.put_line(users.user_Name);

          end loop;

          close users_cur;

          end;

          ----弱類型

          declare

          type ref_cur is ref cursor;

          my_cur ref_cur;

          users user_tbl%rowtype;

          stus stu_tbl%rowtype;

          begin

          open my_cur for select * from user_tbl;

          loop

          fetch my_cur into users;

          exit when my_cur%notfound;

          dbms_output.put_line(users.user_Name);

          end loop;

          close my_cur;

          open my_cur for select * from user_tbl where user_name='ny2t92';

          loop

          fetch my_cur into users;

          exit when my_cur%notfound;

          dbms_output.put_line(users.user_Name);

          end loop;

          close my_cur;

          open my_cur for select * from stu_tbl;

          loop

          fetch my_cur into stus;

          exit when my_cur%notfound;

          dbms_output.put_line(stus.stu_Name);

          end loop;

          close my_cur;

          end;

          ----動態(tài)SQL游標(biāo)

          declare

          type ref_cur is ref cursor;

          my_cur ref_cur;

          users user_tbl%rowtype;

          username varchar2(20);

          sqlstmt varchar2(200);

          begin

          username:='&username';

          sqlstmt := 'select * from user_tbl where user_name= :name';

          open my_cur for sqlstmt using username;

          loop

          fetch my_cur into users;

          exit when my_cur%notfound;

          dbms_output.put_line(users.user_Name);

          end loop;

          close my_cur;

          end;

          六.子程序

          子程序分為:存儲過程和函數(shù),它是命名的PL/SQL塊,編譯并存儲在數(shù)據(jù)庫中。

          子程序的各個部分:聲明部分,可執(zhí)行部分,異常處理部分。

          過程----執(zhí)行某些操作

          函數(shù)----執(zhí)行操作并返回值

          ==========存儲過程==========

          創(chuàng)建過程的語法:

          create or replace procedure

          proce_name (parameter_list)

          is|as

          local variable declaration

          begin

          executable statements

          exception

          exception_handlers

          end proce_name;

          過程參數(shù)的三種模式:

          In----用于接收調(diào)用的值,默認(rèn)的參數(shù)模式

          Out----用于向調(diào)用程序返回值

          In out----用于接收調(diào)用程序的值,并向調(diào)用程序返回更新的值

          執(zhí)行過程的語法:

          Execute proce_name(parameter_list);

          或

          Declare

          Variable var_list;

          Begin

          Proce_name(var_list);

          End;

          將過程執(zhí)行的權(quán)限授予其他用戶:

          Grant execute on proce_name to scott;

          Grant execute on proce_name to public;

          刪除存儲過程:

          Drop procedure proce_name;

          ==========函數(shù)==========

          創(chuàng)建函數(shù)的語法:

          Create or replace function

          Fun_name (parameter_list)

          Return datatype is|as

          Local declarations

          Begin

          Executable statements;

          Return result;

          Exception

          Exce_handlers;

          End;

          函數(shù)只能接收in參數(shù),不能接受out或in out參數(shù),形參不能是PL/SQL類型

          函數(shù)的返回類型也必須是數(shù)據(jù)庫類型

          訪問函數(shù)的方式:

          J 使用PL/SQL塊

          J 使用SQL語句

          Select fun_name(parameter_list) from dual;

          拓展閱讀

          如何啟動和關(guān)閉Oracle數(shù)據(jù)庫

          對于一個oracle數(shù)據(jù)庫新手來說,怎么樣進(jìn)行管理oracle數(shù)據(jù)庫呢? 首先從最簡單的啟動和關(guān)閉數(shù)據(jù)庫來說,下面簡單的介紹一下Oracle數(shù)據(jù)庫的啟動和關(guān)閉順序?qū)嵗v解。

          一、oralce的啟動順序:

          1、先啟動監(jiān)聽程序(對應(yīng)端口1521) :

          #lsnrctl start

          2、啟動oracle實例:

          #sqlplus / as sysdba(回車)

          SQL>startup

          --啟動的是環(huán)境變量中的默認(rèn)數(shù)據(jù)庫實例 $ORACLE_SID

          --如果啟動多個實例:export ORACLE_SID=ctaudit

          #sqlplus / as sysdba(回車)

          SQL>startup

          3、啟動em(對應(yīng)端口1158),isqlplus(對應(yīng)端口5560):

          #emctl start dbconsole

          #isqlplusctl start

          二、oracle的關(guān)閉順序

          1、先關(guān)閉em,isqlplus:

          #emctl stop dbconsole

          #isqlplusctl stop

          2、關(guān)閉監(jiān)聽:

          #lsnrctl stop

          3、關(guān)閉oracle實例:

          #sqlplus / as sysdba(回車)

          SQL>shutdown immediate

        《&.doc》
        将本文的Word文档下载到电脑,方便收藏和打印
        推荐度:
        点击下载文档

        【Oracle數(shù)據(jù)庫語句】相關(guān)文章:

        oracle數(shù)據(jù)庫基本語句02-08

        Oracle數(shù)據(jù)庫SELECT語句10-25

        ORACLE數(shù)據(jù)庫操作基本語句09-15

        Oracle數(shù)據(jù)庫基礎(chǔ)知識:SELECT語句01-23

        Oracle數(shù)據(jù)庫認(rèn)證層次08-29

        oracle數(shù)據(jù)庫基礎(chǔ)知識01-21

        Oracle 數(shù)據(jù)庫查詢小技巧10-17

        oracle數(shù)據(jù)庫培訓(xùn)課程大綱08-07

        Oracle數(shù)據(jù)庫基本知識09-13

        在线咨询
        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码
      3. <sub id="h4knl"><ol id="h4knl"></ol></sub>
        <sup id="h4knl"></sup>
          <sub id="h4knl"></sub>

          <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
          1. <strong id="h4knl"></strong>

          2. 色吊丝亚洲中文字幕 | 亚国产亚洲亚洲精品视频 | 亚洲国产精品欧美日韩一区二区 | 亚洲国产综合一区第一页 | 亚洲国产欧美丝袜精品一区 | 综合专区亚洲无 |

            Oracle數(shù)據(jù)庫語句大全

              Oracle數(shù)據(jù)庫是甲骨文公司的一款關(guān)系數(shù)據(jù)庫管理系統(tǒng)。它是在數(shù)據(jù)庫領(lǐng)域一直處于領(lǐng)先地位的產(chǎn)品。下面yjbys小編為大家分享的是Oracle數(shù)據(jù)庫查詢語句,希望能幫助到大家!

              一.入門部分

              1. 創(chuàng)建表空間

              create tablespace schooltbs datafile ‘D:oracledatasourceschooltbs.dbf’ size 10M autoextend on;

              2. 刪除表空間

              space schooltbs[including contents and datafiles];

              3. 查詢表空間基本信息

              select *||tablespace_name from DBA_TABLESPACES;

              4. 創(chuàng)建用戶

              create user lihua

              identified by lihua

              default tablespace schooltbs

              temporary tablespace temp;

              5. 更改用戶

              alter user lihua

              identified by 123

              default tablespace users;

              6. 鎖定用戶

              alter user lihua account lock|unlock;

              7. 刪除用戶

              drop user lihua cascade;--刪除用戶模式

              8. oracle數(shù)據(jù)庫中的角色

              connect,dba,select_catalog_role,_catalog_role,execute_catalog_role,exp_full_database,imp_full_database,resource

              9. 授予連接服務(wù)器的角色

              grant connect to lihua;

              10.授予使用表空間的角色

              grant resource to lihua with grant option;--該用戶也有授權(quán)的權(quán)限

              11.授予操作表的權(quán)限

              grant select, on user_tbl to scott;--當(dāng)前用戶

              grant ,on lihua.user_tbl to scott;--系統(tǒng)管理員

              12.修改表的結(jié)構(gòu)(alter)

              Alter table 表名 add(列的名稱,列的類型);

              二.SQL查詢和SQL函數(shù)

              1.SQl支持的命令:

              數(shù)據(jù)定義語言(DDL):create,alter,drop

              數(shù)據(jù)操縱語言(DML):,,update,select

              數(shù)據(jù)控制語言(DCL):grant,revoke

              事務(wù)控制語言(TCL):commit,savepoint,rollback

              2.Oracle數(shù)據(jù)類型

              字符,數(shù)值,日期,RAW,LOB

              字符型

              char:1-2000字節(jié)的定長字符

              varchar2:1-4000字節(jié)的變長字符

              long:2GB的變長字符

              注意:一個表中最多可有一列為long型

              Long列不能定義唯一約束或主鍵約束

              long列上不能創(chuàng)建索引

              過程或存儲過程不能接受long類型的參數(shù)。

              數(shù)值型

              number:最高精度38位

              日期時間型

              date:精確到ss

              timestamp:秒值精確到小數(shù)點后6位

              函數(shù)

              sysdate,systimestamp返回系統(tǒng)當(dāng)前日期,時間和時區(qū)。

              更改時間的顯示

              alter session set nls_date_language=’american’;

              alter session set nls_date_format=’yyyy-mm-dd’;

              Oracle中的偽列

              像一個表列,但沒有存儲在表中

              偽列可以查詢,但不能插入、更新和修改它們的值

              常用的偽列:rowid和rownum

              rowid:表中行的存儲地址,可唯一標(biāo)示數(shù)據(jù)庫中的某一行,可以使用該列快速定位表中的行。

              rownum:查詢返回結(jié)果集中的行的序號,可以使用它來限制查詢返回的行數(shù)。

              3.數(shù)據(jù)定義語言

              用于操作表的命令

              create table

              alter table

              truncate table

              修改表的命令

              alter table stu_table rename to stu_tbl;--修改表名

              alter table stu_tbl rename column stu_sex to sex;--修改列名

              alter table stu_tbl add (stu_age number);--添加新列

              alter table stu_tbl drop(sex);--刪除列

              alter table stu_tbl modify(stu_sex varchar2(2));--更改列的數(shù)據(jù)類型

              alter table stu_tbl add constraint pk_stu_tbl primary key(id);--添加約束

              4.數(shù)據(jù)操縱語言

              select,update,,

              利用現(xiàn)有的表創(chuàng)建表

              create table stu_tbl_log as select id,stu_name,stu_age from stu_tbl;--

              選擇無重復(fù)的行

              select distinct stu_name from stu_tbl;--

              插入來自其他表中的記錄

              into stu_tbl_log select id,stu_name,stu_age from stu_tbl;

              5.數(shù)據(jù)控制語言

              grant,revoke

              6.事務(wù)控制語言

              commit,savepoint,rollback

              7.SQL操作符

              算術(shù)操作符:L+-*/

              比較操作符:L=,!=,<>,>,<,>=,<=,between-and,in,like,is null等

              邏輯操作符:Land,or,not

              集合操作符:Lunion,union all,intersect,minus

              連接操作符:L||

              示例中stu_tbl_log中的數(shù)據(jù)如下:

              ID STU_NAME STU_AGE

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

              1000 李華 20

              1001 accp 20

              1003 nimda 3

              stu_tbl中的數(shù)據(jù)如下:

              ID STU_NAME ST STU_AGE

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

              1000 李華 男 20

              1001 accp 男 20

              1002 admin 男 30

              示例:

              select (3+2)/2 from dual;--算術(shù)操作符,結(jié)果:2.5

              select * from stu_tbl where stu_age>=20;--比較操作符

              select * from stu_tbl where stu_name like '%a%';--比較操作符:like

              select * from stu_tbl where stu_name like 'a___';--比較操作符:like

              select * from stu_tbl where stu_age in(20,30);--比較操作符:in

              select * from stu_tbl where stu_age between 20 and 30;--比較操作符:between

              select stu_name from stu_tbl union all

              select stu_name from stu_tbl_log;--集合操作符:union all,測試結(jié)果具體如下:

              STU_NAME

              -----------

              李華

              accp

              admin

              李華

              accp

              nimda

              已選擇6行。

              select stu_name from stu_tbl union

              select stu_name from stu_tbl_log;--集合操作符:union,測試結(jié)果具體如下:

              STU_NAME

              ---------

              accp

              admin

              nimda

              李華

              select stu_name from stu_tbl intersect

              select stu_name from stu_tbl_log;--集合操作符:intersect,測試結(jié)具體如下:

              STU_NAME

              ----------

              accp

              李華

              select stu_name from stu_tbl minus

              select stu_name from stu_tbl_log;--集合操作符:minus,測試結(jié)果如下:

              STU_NAME

              ----------

              Admin

              從中可以看出:

              minus是獲取第一張表獨有的數(shù)據(jù)

              intersect是獲取兩張表中都有的數(shù)據(jù)

              union是整合兩張表的數(shù)據(jù),都有的只顯示一次

              union all是純粹的兩張表數(shù)據(jù)整合

              select id,stu_name||' '||stu_sex as name_sex,stu_age

              from stu_tbl;--連接操作符||,測試結(jié)果具體如下:

              ID NAME_SEX STU_AGE

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

              1000 李華 男 20

              1001 accp 男 20

              1002 admin 男 30

              8.SQL函數(shù)

              單行函數(shù):從表中查詢的每一行只返回一個值,可出現(xiàn)在select子句,where子句中

              日期函數(shù)

              數(shù)字函數(shù)

              字符函數(shù)

              轉(zhuǎn)換函數(shù):ToChar(),ToDate(),ToNumber()

              其他函數(shù):

              Nvl(exp1,exp2):表達(dá)式一為null時,返回表達(dá)式二

              Nvl2(exp1,exp2,exp3):表達(dá)式一為null時返回表達(dá)式三,否則返回表達(dá)式二

              Nullif(exp1,exp2):兩表達(dá)式相等時,返回null,否則返回表達(dá)式一

              分組函數(shù):基于一組行來返回

              Avg,Min,Max,Sum,Count

              Group by,having

              分析函數(shù)

              Row_number,rank,dense_rank

              示例:

              select u.user_name,sum(oi.order_num*oi.order_price) as total,row_number() over (order by sum(oi.order_num*oi.order_price) desc) as sort from order_item_tbl

              oi,user_tbl u,order_tbl o where oi.order_id = o.id and o.user_id = u.id group by u.user_name;

              三.鎖和數(shù)據(jù)庫對象

              1.鎖:數(shù)據(jù)庫用來控制共享資源并發(fā)訪問的機制。

              鎖的類型:行級鎖,表級鎖

              行級鎖:對正在被修改的行進(jìn)行鎖定。行級鎖也被稱之為排他鎖。

              在使用下列語句時,Oracle會自動應(yīng)用行級鎖:

              ,update,,select…… for update

              select……for update允許用戶一次鎖定多條記錄進(jìn)行更新。

              使用commit or rollback釋放鎖。

              表級鎖:

              lock table user_tbl in mode mode;

              表級鎖類型:

              行共享 row share

              行排他 row exclusive

              共享 share

              共享行排他 share row exclusive

              排他 exclusive

              死鎖:兩個或兩個以上的事務(wù)相互等待對方釋放資源,從而形成死鎖

              2.數(shù)據(jù)庫對象

              oracle數(shù)據(jù)庫對象又稱模式對象

              數(shù)據(jù)庫對象是邏輯結(jié)構(gòu)的集合,最基本的數(shù)據(jù)庫對象是表

              數(shù)據(jù)庫對象:

              表,序列,視圖,索引

              序列

              用于生成唯一,連續(xù)序號的對象。

              創(chuàng)建語法:

              create sequence user_id_seq

              start with 1000

              increment by 1

              maxvalue 2000

              minvalue 1000

              nocycle

              cache 1000;--指定內(nèi)存中預(yù)先分配的序號

              訪問序列:

              select user_id_seq.currval from dual;

              select user_id-seq.nextval from dual;

              更改刪除序列:

              alter sequence user_id_seq maxvalue 10000;--不能修改其start with 值

              drop sequence user_id_seq;

              在Hibernate中訪問序列:

              user_id_seq

              視圖

              以經(jīng)過定制的方式顯示來自一個或多個表的數(shù)據(jù)

              創(chuàng)建視圖:

              create or replace view

              user_tbl_view (vid,vname,vage)

              as select id,user_name,age from user_tbl

              [with check option]|[with read only];

              創(chuàng)建帶有錯誤的視圖:

              create force view user_tbl_force_view as

              select * from user_table;--此時user_table可以不存在

              創(chuàng)建外聯(lián)接視圖:

              create view user_stu_view as

              select u.id,u.user_name,u.password,s.ddress

              from user_tbl u,stu_tbl s

              where u.s_id(+)=s.id;--哪一方帶有(+),哪一方就是次要的

              刪除視圖:

              drop user_stu_view;

              索引

              用于提高SQL語句執(zhí)行的性能

              索引類型:

              唯一索引,位圖索引,組合索引,基于函數(shù)的索引,反向鍵索引

              創(chuàng)建標(biāo)準(zhǔn)索引:

              create index user_id_index on user_tbl(id) tablespace schooltbs;

              重建索引:

              alter index user_id_index rebuild;

              刪除索引:

              drop index user_id_index;

              創(chuàng)建唯一索引:

              create unique index user_id_index on user_tbl(id);

              創(chuàng)建組合索引:

              create index name_pass_index on user_tbl(user_name,password);

              創(chuàng)建反向鍵索引:

              create index user_id_index on user_tbl(id) reverse;

              四.使用PL/SQL

              可用于創(chuàng)建存儲過程,觸發(fā)器,程序包,給SQL語句的執(zhí)行添加程序邏輯。

              支持SQL,在PL/SQL中可以使用:

              數(shù)據(jù)操縱命令

              事務(wù)控制命令

              游標(biāo)控制

              SQL函數(shù)和SQL運算符

              支持面向?qū)ο缶幊?OOP)

              可移植性

              更佳的性能,PL/SQL經(jīng)過編譯執(zhí)行

              分為三個部分:聲明部分,可執(zhí)行部分和異常處理部分

              [declare

              declarations]

              begin

              executable statements

              [exception

              handlers]

              end;

              打開輸出

              set serverout on;

              --根據(jù)輸入編號獲取某學(xué)員的成績--if

              declare

              score user_tbl.score%type;

              begin

              select score into score from user_tbl where id='&id';

              if score>90 then

              dbms_output.put_line('優(yōu)秀');

              elsif score>80 then

              dbms_output.put_line('良好');

              elsif score>60 then

              dbms_output.put_line('及格');

              else

              dbms_output.put_line('差');

              end if;

              end;

              --根據(jù)學(xué)員姓名獲取某學(xué)員的成績--if

              declare

              score user_tbl.score%type;

              begin

              select score into score from user_tbl where user_name='&name';

              if score>90 then

              dbms_output.put_line('優(yōu)秀');

              elsif score>80 then

              dbms_output.put_line('良好');

              elsif score>60 then

              dbms_output.put_line('及格');

              else

              dbms_output.put_line('差');

              end if;

              end;

              --case的使用

              declare

              grade user_tbl.grade%type;

              begin

              select grade into grade from user_tbl where id='&id';

              case grade

              when 'A' then dbms_output.put_line('優(yōu)異');

              when 'B' then dbms_output.put_line('優(yōu)秀');

              when 'C' then dbms_output.put_line('良好');

              else dbms_output.put_line('一般');

              end case;

              end;

              --基本循環(huán)

              declare

              i number(4):=1;

              begin

              loop

              dbms_output.put_line('loop size:'||i);

              i:=i+1;

              exit when i>10;

              end loop;

              end;

              --while循環(huán)

              declare

              i number(4):=1;

              begin

              while i<=10 loop

              dbms_output.put_line('while loop size='||i);

              i:=i+1;

              end loop;

              end;

              --for循環(huán)

              declare

              i number(4):=1;

              begin

              for i in 1..10 loop

              dbms_output.put_line('for loop Size:'||i);

              end loop;

              end;

              declare

              i number(2):=1;

              j number(2):=1;

              begin

              for i in reverse 1..9 loop

              for j in 1..i loop

              dbms_output.put(j||'x'||i||'='||j*i||' ');

              end loop;

              dbms_output.put_line('');

              end loop;

              end;

              --動態(tài)SQL

              declare

              userId number(2);

              sql_str varchar2(100);

              userName user_tbl.user_name%type;

              begin

              execute immediate 'create table testExe(id number,test_name varchar2(20))';

              userId:='&userId';

              sql_str:='select user_name from user_tbl where id=:id';

              execute immediate sql_str into userName using userId;

              dbms_output.put_line(userName);

              end;

              (or

              declare

              id_param number:='&id_param';

              sql_str varchar2(100);

              name_param stu_tbl.stu_name%type;

              begin

              sql_str:='select stu_name from stu_tbl where id=:p';

              execute immediate sql_str into name_param using id_param;

              dbms_output.put_line(name_param);

              end;

              /

              )

              --異常處理

              declare

              grade number(4);

              begin

              grade:='&grade';

              case grade

              when 1 then dbms_output.put_line('好的');

              --else dbms_output.put_line('不好');

              end case;

              exception

              when case_not_found then

              dbms_output.put_line('輸入類型不匹配!');

              end;

              --系統(tǒng)異常

              declare

              rowD user_tbl%rowtype;

              begin

              select * into rowD from user_tbl;

              dbms_output.put_line(rowD.id||''||rowD.user_name||' '||rowD.password);

              exception

              when too_many_rows then

              dbms_output.put_line('不能將多行賦予一個屬性!');

              end;

              or

              declare

              rowD user_tbl%rowtype;

              begin

              select * into rowD from user_tbl where id=5;

              dbms_output.put_line(rowD.id||' '||rowD.user_name||' '||rowD.password);

              exception

              when too_many_rows then

              dbms_output.put_line('不能將多行賦予一個屬性!');

              when no_data_found then

              dbms_output.put_line('沒有您要查找的數(shù)據(jù)!');

              end;

              --自定義錯誤

              declare

              invalidError exception;

              category varchar2(20);

              begin

              category:='&category';

              if category not in('附件','頂盤','備件') then

              raise invalidError;

              else

              dbms_output.put_line('您輸入的類別是:'||category);

              end if;

              exception

              when invalidError then

              dbms_output.put_line('無法識別的類別!');

              end;

              --引發(fā)應(yīng)用程序異常

              declare

              app_exception exception;

              grade user_tbl.grade%type;

              begin

              select grade into grade from user_tbl where id=&id;

              if grade='A' then

              raise app_exception;

              else

              dbms_output.put_line('查詢的等級為:'||grade);

              end if;

              exception

              when app_exception then

              raise_application_error(-20001,'未知的等級!');

              end;

              五、游標(biāo)管理

              游標(biāo)類型:隱式游標(biāo),顯式游標(biāo),REF游標(biāo)

              REF游標(biāo)用于處理運行時才能確定的動態(tài)SQL查詢的結(jié)果

              ==========隱式游標(biāo)==========

              在PL/SQL中使用DML語句時自動創(chuàng)建隱式游標(biāo)

              隱式游標(biāo)自動聲明、打開和關(guān)閉,其名為SQL

              隱式游標(biāo)的屬性:

              %found SQL語句影響實質(zhì)后返回true

              %notfound SQL語句沒有影響實質(zhì)后返回true

              %rowcount SQL語句影響的行數(shù)

              %isopen 游標(biāo)是否打開,始終為false

              示例:

              begin

              user_tbl set score=score+5;

              if SQL%found then

              dbms_output.put_line('數(shù)據(jù)被更改: '||SQL%rowcount);

              elsif sql%notfound then

              dbms_output.put_line('沒有找到數(shù)據(jù)!');

              end if;

              if SQL%isopen then

              dbms_output.put_line('Open');

              else

              dbms_output.put_line('Close');

              end if;

              end;

              ==========顯式游標(biāo)==========

              在PL/SQL的聲明部分定義查詢,該查詢可以返回多行

              J 聲明游標(biāo)

              J 打開游標(biāo)

              J 從游標(biāo)中取回數(shù)據(jù)

              J 關(guān)閉游標(biāo)

              聲明游標(biāo)完成兩個任務(wù):

              給游標(biāo)命名

              將一個查詢與游標(biāo)關(guān)聯(lián)

              cursor cursor_name is select statement;

              打開游標(biāo):

              open cursor_name;

              取數(shù)據(jù):

              fetch cursor_name into record_list;

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

              close cursor_name;

              顯式游標(biāo)的屬性:

              %found 執(zhí)行最后一條fetch語句成功返回行時為true

              %notfound 執(zhí)行最后一條fetch語句未能返回行時為true

              %rowcount 返回到目前為止游標(biāo)提取的行數(shù)

              %isopen 游標(biāo)是否打開

              示例:

              declare

              users user_tbl%rowtype;

              cursor boys_cur is select * from user_tbl where sex='h';

              begin

              open boys_cur;

              loop

              fetch boys_cur into users;

              exit when boys_cur%notfound;

              dbms_output.put_line(users.user_name||' '||users.password);

              dbms_output.put_line(boys_cur%rowcount);

              end loop;

              close boys_cur;

              end;

              帶參的顯式游標(biāo)

              declare

              users user_tbl%rowtype;

              cursor boys_cur(sexParam varchar2)

              is select * from user_tbl where sex=sexParam;

              begin

              open boys_cur('&sex');

              loop

              fetch boys_cur into users;

              exit when boys_cur%notfound;

              dbms_output.put_line(users.user_name||' '||users.password);

              dbms_output.put_line(boys_cur%rowcount);

              end loop;

              close boys_cur;

              end;

              使用顯式游標(biāo)更新行

              declare

              cursor user_update_cur is select sex from user_tbl for update;

              usersex user_tbl.sex%type;

              begin

              open user_update_cur;

              loop

              fetch user_update_cur into usersex;

              exit when user_update_cur%notfound;

              dbms_output.put_line(usersex);

              if usersex = 'M' then

              user_tbl set score=score-5 where current of user_update_cur;

              else

              user_tbl set score=score+5 where current of user_update_cur;

              end if;

              end loop;

              close user_update_cur;

              commit;

              end;

              循環(huán)游標(biāo)

              declare

              cursor user_cur is select * from user_tbl;

              begin

              for username in user_cur loop

              dbms_output.put_line(username.user_name||' '||username.sex);

              end loop;

              end;

              ==========REF游標(biāo)==========

              REF游標(biāo)和游標(biāo)變量用于處理運行時動態(tài)執(zhí)行的SQL查詢

              創(chuàng)建游標(biāo)變量的步驟:

              J 聲明REF游標(biāo)類型

              J 聲明REF游標(biāo)類型的變量

              聲明類型的語法

              Type ref_cursor_name is ref cursor [return return_type];

              打開游標(biāo)變量的語法

              Open cursor_name for select_statement;

              ----聲明強類型的游標(biāo)

              declare

              type ref_cur is ref cursor return user_tbl%rowtype;

              users_cur ref_cur;

              ----聲明弱類型的游標(biāo)

              declare

              type ref_cur is ref cursor;

              users_cur ref_cur;

              示例

              ----強類型

              declare

              type ref_cur is ref cursor return user_tbl%rowtype;

              users_cur ref_cur;

              users user_tbl%rowtype;

              begin

              open users_cur for select * from user_tbl where user_name='ny2t92';

              loop

              fetch users_cur into users;

              exit when users_cur%notfound;

              dbms_output.put_line(users.user_Name);

              end loop;

              close users_cur;

              end;

              ----弱類型

              declare

              type ref_cur is ref cursor;

              my_cur ref_cur;

              users user_tbl%rowtype;

              stus stu_tbl%rowtype;

              begin

              open my_cur for select * from user_tbl;

              loop

              fetch my_cur into users;

              exit when my_cur%notfound;

              dbms_output.put_line(users.user_Name);

              end loop;

              close my_cur;

              open my_cur for select * from user_tbl where user_name='ny2t92';

              loop

              fetch my_cur into users;

              exit when my_cur%notfound;

              dbms_output.put_line(users.user_Name);

              end loop;

              close my_cur;

              open my_cur for select * from stu_tbl;

              loop

              fetch my_cur into stus;

              exit when my_cur%notfound;

              dbms_output.put_line(stus.stu_Name);

              end loop;

              close my_cur;

              end;

              ----動態(tài)SQL游標(biāo)

              declare

              type ref_cur is ref cursor;

              my_cur ref_cur;

              users user_tbl%rowtype;

              username varchar2(20);

              sqlstmt varchar2(200);

              begin

              username:='&username';

              sqlstmt := 'select * from user_tbl where user_name= :name';

              open my_cur for sqlstmt using username;

              loop

              fetch my_cur into users;

              exit when my_cur%notfound;

              dbms_output.put_line(users.user_Name);

              end loop;

              close my_cur;

              end;

              六.子程序

              子程序分為:存儲過程和函數(shù),它是命名的PL/SQL塊,編譯并存儲在數(shù)據(jù)庫中。

              子程序的各個部分:聲明部分,可執(zhí)行部分,異常處理部分。

              過程----執(zhí)行某些操作

              函數(shù)----執(zhí)行操作并返回值

              ==========存儲過程==========

              創(chuàng)建過程的語法:

              create or replace procedure

              proce_name (parameter_list)

              is|as

              local variable declaration

              begin

              executable statements

              exception

              exception_handlers

              end proce_name;

              過程參數(shù)的三種模式:

              In----用于接收調(diào)用的值,默認(rèn)的參數(shù)模式

              Out----用于向調(diào)用程序返回值

              In out----用于接收調(diào)用程序的值,并向調(diào)用程序返回更新的值

              執(zhí)行過程的語法:

              Execute proce_name(parameter_list);

              或

              Declare

              Variable var_list;

              Begin

              Proce_name(var_list);

              End;

              將過程執(zhí)行的權(quán)限授予其他用戶:

              Grant execute on proce_name to scott;

              Grant execute on proce_name to public;

              刪除存儲過程:

              Drop procedure proce_name;

              ==========函數(shù)==========

              創(chuàng)建函數(shù)的語法:

              Create or replace function

              Fun_name (parameter_list)

              Return datatype is|as

              Local declarations

              Begin

              Executable statements;

              Return result;

              Exception

              Exce_handlers;

              End;

              函數(shù)只能接收in參數(shù),不能接受out或in out參數(shù),形參不能是PL/SQL類型

              函數(shù)的返回類型也必須是數(shù)據(jù)庫類型

              訪問函數(shù)的方式:

              J 使用PL/SQL塊

              J 使用SQL語句

              Select fun_name(parameter_list) from dual;

              拓展閱讀

              如何啟動和關(guān)閉Oracle數(shù)據(jù)庫

              對于一個oracle數(shù)據(jù)庫新手來說,怎么樣進(jìn)行管理oracle數(shù)據(jù)庫呢? 首先從最簡單的啟動和關(guān)閉數(shù)據(jù)庫來說,下面簡單的介紹一下Oracle數(shù)據(jù)庫的啟動和關(guān)閉順序?qū)嵗v解。

              一、oralce的啟動順序:

              1、先啟動監(jiān)聽程序(對應(yīng)端口1521) :

              #lsnrctl start

              2、啟動oracle實例:

              #sqlplus / as sysdba(回車)

              SQL>startup

              --啟動的是環(huán)境變量中的默認(rèn)數(shù)據(jù)庫實例 $ORACLE_SID

              --如果啟動多個實例:export ORACLE_SID=ctaudit

              #sqlplus / as sysdba(回車)

              SQL>startup

              3、啟動em(對應(yīng)端口1158),isqlplus(對應(yīng)端口5560):

              #emctl start dbconsole

              #isqlplusctl start

              二、oracle的關(guān)閉順序

              1、先關(guān)閉em,isqlplus:

              #emctl stop dbconsole

              #isqlplusctl stop

              2、關(guān)閉監(jiān)聽:

              #lsnrctl stop

              3、關(guān)閉oracle實例:

              #sqlplus / as sysdba(回車)

              SQL>shutdown immediate