oracle学习小结

来源:互联网 发布:工业造型设计软件 编辑:程序博客网 时间:2024/06/06 11:01

1.循环(3种):

  (1)Loop

      Exit when 条件;

End loop;

  (2)while 条件 loop

    End loop;

  (3)for 变量名 in (游标,常量,游标.first..last) loop

End loop;

2.游标

  (1)游标的定义

Cursor cur_name is

  Select ... From ...where ...;

  (2)open cur_named;

  (3)fetch cur_name into 变量;(加循环注意)

  (4)close cur_name;

  (5)for 变量 in cur_name loop

End loop;

  (6)游标的属性:first,last(返回值为number类型),exists(索引)

     SQL%rowcount,SQl%found,SQLT%notfound在这前三种update,insert

Delete都会引起变化,只是select在没有查到数据时,会抛出异常no_data_found,看不到结果,但属性还是变化的。SQL%isopen只有显示的游标方可;

  (7)游标的嵌套,思路清晰,但耗时间。

  (8)对于更新的在游标for update nowait

    在updatewhere current of 游标

3.异常(SQLCODESQLERRM

  (1)预定义异常:

No_data_found

Too_many_rows

  (2)未定义异常(仅有错误号,没有常量定义)

比如:-2292 数据库一致性

这时要绑定错误号:
    exception_name exception;

Pragma exception_init(exception_name,错误号)

  (3)用户自定义的异常

Exception_name exception;

Raise exception_name;

Exception:

  When exception_name then

     Statement;

4.自定义数据类型

  (1)type type_record_name is record(

       可以是表,列等

);

  (2)type type_table_name is table of 表,列 index by binary_integer

   例子:

   TYPE STUDENT_TYPEIS RECORD(

    STUDENT_NAME HAND_STUDENT.STUDENT_NAME%TYPE,

    STUDENT_NO   HAND_STUDENT.STUDENT_NO%TYPE,

    COURSE_NAME  HAND_COURSE.COURSE_NAME%TYPE,

    CORE         HAND_STUDENT_CORE.CORE%TYPE);

  TYPE STUDENT_INFORMATIONIS TABLE OF STUDENT_TYPEINDEX BY HAND_STUDENT.STUDENT_NO%TYPE;

5.存储过程和函数

  在存储过程和函数中参数声明不用说明长度,而在is中一定说明长度。

 (1)函数

Create or replace function function_name [参数]

Return 类型;

Is

  声明;

Begin

  Statement;

  Return ..;

End;

在包里面:

Function function_name[参数]

Return 类型;

Is

  声明;

Begin

  Statement;

  Return ...;

End;

  (2)存储过程

     Create or replace procedure procedurn_name[参数]

     Is

       声明;

     Begin

       Statement;

     End;

     在包里面:

     Procedure procedure_name[参数]

     Is

       声明;

     Begin

       Statement;

 

     End;

     存储过程和函数小结:

        在sql语句中使用函数,函数体内不能含有dml,updatedelete中,函数体不能针对同一张表进行select,再函数体内不能含有事务结束语句commitrollback

6.触发器

  (1)触发器的定义:

Create or replace trigge triggre_name

Before update or delete on table_name/after update or insert on table_name

(for each row)

Begin

  Statement;

End;

  (2)oldnew

     在update存在:old和:new。而delete只存在oldinsert只存在new
     同时在new赋值,只能在before,不能再after

    

0 0
原创粉丝点击