zoukankan      html  css  js  c++  java
  • Oracle数据库零散知识03

    21,存储过程,简化复杂操作,增加数据独立性,提高安全性,提高性能

    与函数创建对比:

    create or replace function fun_01(v_01 in number)
        return number--必须要求有返回值
        as
        result number;
        begin
        result := power(v_01,2);
        return result;
        end;
        /
    
    Function created.
    
    select fun_01(9) from dual;
    
     FUN_01(9)
    ----------
            81
    

      

    存储过程创建示例,不要求有返回值:

    create or replace procedure pro_01(v_01 in number)
        as
        result number;
        begin
        result := power(v_01,2);
        dbms_output.put_line(result);
        end;
        /
    
    Procedure created.
    
    execute pro_01(9);
    81
    
    PL/SQL procedure successfully completed.
    

      

    22,触发器,一种特殊的存储过程

    create table log_tab--创建一个日志表
        (
        l_id number(3),
        l_old varchar2(20),--删除和已更新的主键
        l_type varchar2(10),--执行的操作类型
        l_new varchar2(20),--添加和已更新的主键
        l_date date,--更改的日期
        constraint pk_log_tab primary key(l_id)
        );
    
    Table created.
    

      

    create sequence log_tab_id--创建一个序列
        minvalue 100
        maxvalue 300
        start with 100
        increment by 1;
    
    Sequence created.
    

      

    create or replace trigger tr_student02--创建一个触发器
        after insert or update or delete –后插入更新删除
        on student02--针对student02表
        for each row—针对行触发
        begin
        case
        when inserting then—插入触发
        insert into log_tab values
        (
       log_tab_id.nextval,
       null,
       'insert',
       :new.sno,--导入inserted表中数据
       sysdate);
       when deleting then—删除触发
       insert into log_tab values
       (
       log_tab_id.nextval,
       :old.sno,--导入deleted表中数据
       'delete',
       null,
       sysdate);
       when updating then—更新触发
       insert into log_tab values
       (
       log_tab_id.nextval,
       :old.sno,
       'update',
       :new.sno,
       sysdate);
       end case;
      end;
    

      

    delete from student02 where rownum = 1;
    insert into student02(sno,sname,ssex) values(123,'hook','m');
     update student02 set sname = 'tokl' where sno = 123;
    
    
    
     select * from log_tab;
    
          L_ID L_OLD                L_TYPE     L_NEW                L_DATE
    ---------- -------------------- ---------- -------------------- ---------
           101 109                  delete                          28-OCT-15
           102                      insert     123                  28-OCT-15
           103 123                  update     123                  28-OCT-15
    

    23,Oracle别名as使用的时候,不能使用在表上,只能使用在列上,表使用空格别名。。

    24,通配符'_'使用的时候,如果字段使用的char型,固定长度,需要用通配符补全

    25,排序 空值在前在后  nulls first ..nulls last

  • 相关阅读:
    PHP双向队列
    [转]数据库查询的3个优化方法
    MySQL性能测试工具 mysqlslap
    PHP各种魔术方法测试
    VBA中级班课时3小结
    VBA中级班课时1小结
    执行cmd并返回程序结果
    Update Dataset data back to Database
    终于会用c#中的delegate(委托)和event(事件)了
    c#: Enqueued event for Queue<T>
  • 原文地址:https://www.cnblogs.com/whytohow/p/4916826.html
Copyright © 2011-2022 走看看