`

linux 下oracle 命令小结(3)

阅读更多
24、视图:
a)创建视图:
create or replace view 视图名称 as 
select 表A.字段1,表A.字段2,……,表B.字段1,表B.字段2……
from 表A,表B  on 表A. 字段=表B.字段;
b)删除视图:
drop view 视图名称;

25、索引:
a)创建索引:
create index 索引名称 on 表名称 (列名称) [ tablespace 表空间名称];
注:索引还有其他多种类型,请参看书102-103页。
26、游标的使用:
a)动态游标:示例如下:
declare
    存储游标的变量 test_tab%rowtype;
--定义游标
cursor 游标名称 is select * from test_tab where 条件; 
begin
        open 游标名称;--打开游标
           loop – 对游标进行循环操作
              fetch 存储游标的变量 into 游标名称; --将游标的值赋给变量
              exit when 游标名称%notfound; --结束循环的条件
              dbms_output.put_line (游标名称.字段名); --对赋值的变量操作
            end loop;
            close 游标名称;
    end;
b)循环游标:(特点:可以直接对有表进行操作,不需要打开、显示赋值、显示判断结束的条件、关闭游标等) 建议使用改类型游标。示例如下:
declare
      cursor 游标名称 is select * from cs_test where name='chenshan'; 
begin
           for 变量 in 游标名称
            loop
               dbms_output.put_line(变量.字段名称);
           end loop;
end;
c)ref 游标:(特点:可以在运行时才确定游标要执行的sql 语句,更加灵活)。其示例如下:
declare
      type temp is ref cursor;
       aa temp;
begin
     if 条件 then 
        open 游标名称 for select * from cs_test where 条件;
     elsif 条件 then
       open 游标名称for select * from cs_goods where 条件;
     else
         open 游标名称for select * from cs_test where 条件;
     end if;
     fetch 游标名称 into 存储游标的变量
     loop
         具体的操作;
     end loop;
     close 游标名称;
end;
27、过程:
a)创建过程:
create or replace procedure (参数列表)
as
 --本地变量申明
begin
     --过程的主体操作
exception 
        when 条件 then 
            --错误处理;
end;
b):执行过程:
execute 过程名称(参数列表);

注:过程
c):将执行过程的权限授予其他用户
grant execute on  过程名 to 用户名;
注:过程的调用不应该在sql 语句块中;
d):删除过程
drop procedure 过程名;
28、函数:
a):创建函数
create [ or replace ] function function_name(parameter1,parameter2,......)—参数列表
return 要返回的数据类型
is | as
[ 本地变量声明 ];
begin
            【函数主体】;
exception
             when tiaojian then 
                【错误处理主体】;
end;
b):调用函数
可以为查询语句:
select 函数名称 from dual;;
也可以为pl / sql 语句块调用
begin
       。。。。。。。。。。。。
       调用函数;
       。。。。。。。。。。。。
end;
c)将函数的使用权限授予其他用户:
grant execute on 函数名 to 用户名;
d)删除函数:
drop function 函数名称;
29、程序包:
a)程序包规范的创建:
create or replace package cs_manage_findindent as
       --声明函数
function func_addgoodds (goodstypeid  number , goodstypename varchar2) return  varchar2;
       --声明过程
       procedure 过程名称(参数);
       --声明游标
       cursor 游标名称(参数) return 返回的数据类型;
end;
b)程序包主体的创建:
create  [ or  replace ]  package  body  程序包名称  is | as
       --下面是函数主体
function  func_addgoodds ( goodstypeid  number , goodstypename varchar2 ) 
return  varchar2  as
      begin
insert  into  cs_ProduceType  values ( goodstypeid , goodstypename );
           return '操作成功' ;
       exception 
               when others then
                   return '操作失败' ; 
       end ;
      --下面是过程主体
      
procedure 过程名 (参数列表) as
       begin
         过程主体;--(pl/sql 语句块)
      exception
             when others then
                  错误主体;
      end;
       --下面是游标主体
cursor 游标名称 (参数列表) return 应该返回的数据类型(一般为表名称或者为 (表名称 % ROWTYPE) ) is  查询语句;
end;
注:程序包主要是用于包含过程,函数,游标等操作的集合,它的使用能够让pl/sql代码变得更加简洁、易懂,同时管理员管理起来也更加轻松。
程序包分为两个部分:一个为规范声明,另一个为程序包主体。顾名思义,规范声明是声明程序包中具体要实现的各种对象,而程序包主体则是具体实现规范声明中已经声明的各种对象。
程序保就好比是一本书,书的目录就相当于程序保的规范声明,而书中的章节内容就相当于是程序包的主体,它是对目录的一个实现。
30、触发器:
a)创建触发器:
create or replace trigger 触发器名称
before | after [ delete | update | insert ]
on 表名称
for each row 
begin
    --开始实现触发器的主体
insert into cs_test_temp values(:old.name,:old.age,:new.sex);
    dbms_output.put_line('已经启动了触发器!!!'); 
exception
        when others then 
            dbms_output.put_line('发现未知错误!!!'); 
end;
注:before | after 表示改触发器是在事件之前还是事件之后被触发。
[ delete | update | insert ]表示选择可以触发该触发器的事件。
for each row 表示对每一行的修改都会触发该触发器,即创建了一个行级触发器。
:old.name 表示事件之前name的值。
:new.sex   表示事件之后name的值。
31、产生随机数字的语法:
num := dbms_random . random ;
32、删除重复的记录:
delete from 表名称 where id in 
       ( select id from 表名称 group by id having count( * ) > 1) 
and 
          ( select min( rowid ) from 表名称 group by id having count( * ) > 1) ;
注:group by 关键字用于将不重复的字段全部列出,以及将重复的字段列出一个。而having 则表示在group by 语句后的条件关键字,因为在group by 后面不能使用where条件关键字来使用条件语句。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics