Valid XHTML 1.0 TransitionalOracle PL/SQL Review (P5)

Page: [index.html] (v2017-06-12_23-00)

  1. PL/SQL 基本概念:……
  2. PL/SQL 编程:
    1. 基本赋值、输入、输出:……
    2. 关于“块”:
      ……

      分类:
      1. 匿名块:……
      2. 命名块:
        ……

        分类:
        1. 过程  :……
        2. 函数  :……
        3. 包    :……
        4. 触发器:
          “命名块”的一种,用于在特定条件成立时执行特定的操作;不需要、也无法手动调用。

          分类:
          1. 行级触发器:
            表中的数据行发生改变 (插入、更新、删除) 前后被触发;但“之后”触发多无意义 (不易找 / 构建到有意义的试验环境) 。

            样例:
            对于正规系统,数据的增删改查应当记录到日志表中;但本组样例对应的环境中没有日志表,所以仅仅对本表某字段的内容略做变更,添加标记,以示生效。
            1. 插入时触发:
              • 程序:
                -- Trigger block.
                create or replace trigger tfwTgrUserAdd
                	before insert    -- Before or after some table action.
                	on tfw_dash_user -- Target table of the action.
                	for each row     -- Row-level trigger.
                declare
                	-- Block local variable(s) declaring.
                	vch2UserRemark tfw_dash_user.remark%type;
                begin
                	dbms_output.put_line('Trigger - Modify remark of new user data before inserting:');
                	dbms_output.put_line('U s e r     I D    : ['||:new.id||']');
                	dbms_output.put_line('Before modification: ['||:new.remark||']');
                
                	vch2UserRemark := :new.remark;
                	vch2UserRemark := 'New inserted: '||vch2UserRemark;
                	:new.remark := vch2UserRemark;
                	dbms_output.put_line('After  modification: ['||:new.remark||']');
                end;
                / -- Block executing command?
                
                说明:
                • :new”:代表已经接受、但尚未实际写入的行数据。
              • 调用:
                [typhoon@TFW-CENT6-LT ~]$ NLS_LANG="SIMPLIFIED CHINESE_CHINA.AL32UTF8" ORACLE_SID=███ sqlplus ███/███;
                
                SQL*Plus: Release 11.2.0.1.0 Production on █████ █ ████ ██
                
                Copyright (c) 1982, 2009, Oracle.  All rights reserved.
                
                
                连接到:
                Oracle Database 11g Release 11.2.0.1.0 - 64bit Production
                
                SQL> variable vch2SysdateText varchar2(32);
                SQL> desc tfw_dash_user;
                 名称                                    是否为空? 类型
                 ----------------------------------------- -------- ----------------------------
                 ID                                        NOT NULL NUMBER(8)
                 NAME                                               VARCHAR2(48)
                 PASSWD                                             VARCHAR2(96)
                 STAT                                               NUMBER(1)
                 REMARK                                             VARCHAR2(768)
                 STATUS                                             NUMBER(3)
                
                SQL> set serveroutput on;
                SQL> insert into tfw_dash_user values(tfw_seq_dash_user_id.nextval, 'user_'||tfw_seq_dash_user_id.currval, 'passwd_'||tfw_seq_dash_user_id.currval, 0, 'remark_'||tfw_seq_dash_user_id.currval, null);
                Trigger - Modify remark of new user data before inserting:
                U s e r     I D    : [10148]
                Before modification: [remark_10148]
                After  modification: [New inserted: remark_10148]
                
                
                已创建 1 行。
                
                SQL> select remark from tfw_dash_user where id=10148;
                
                REMARK
                --------------------------------------------------------------------------------
                New inserted: remark_10148
                
                SQL>
                说明:
                • 仅在满足触发条件时自动调用,无需、也不能手工干预。
                • 仅在 SQL-Plus SQL Developer 等 PL/SQL 开发与调试环境中显示输出;“tfw-integration-demo”项目中的程序对表 tfw_dash_user 调用此触发器时无输出显示。
            2. 更新时触发:
              • 程序:
                -- Trigger block.
                create or replace trigger tfwTgrUserEdit
                	before update    -- Before or after some table action.
                	on tfw_dash_user -- Target table of the action.
                	for each row     -- Row-level trigger.
                begin
                	dbms_output.put_line('Trigger - Modify remark of existing user data before updating:');
                	dbms_output.put_line('U s e r     I D    : ['||:new.id||']');
                	dbms_output.put_line('Before modification: ['||:new.remark||']');
                	:new.remark := 'Updated		 : '||:new.remark;
                	dbms_output.put_line('After  modification: ['||:new.remark||']');
                end;
                / -- Block executing command?
                
              • 调用:
                SQL> update tfw_dash_user set name='用户_'||10148 where id=10148;
                Trigger - Modify remark of existing user data before updating:
                U s e r     I D    : [10148]
                Before modification: [New inserted: remark_10148]
                After  modification: [Updated            : New inserted: remark_10148]
                
                已更新 1 行。
                
                SQL> select remark from tfw_dash_user where id=10148;
                
                REMARK
                --------------------------------------------------------------------------------
                Updated          : New inserted: remark_10148
                
                SQL> update tfw_dash_user set remark='备注_'||10148 where id=10148;
                Trigger - Modify remark of existing user data before updating:
                U s e r     I D    : [10148]
                Before modification: [备注_10148]
                After  modification: [Updated            : 备注_10148]
                
                
                已更新 1 行。
                
                SQL> select remark from tfw_dash_user where id=10148;
                
                REMARK
                --------------------------------------------------------------------------------
                Updated            : 备注_10148
                
                SQL> exit;
                从 Oracle Database 11g Release 11.2.0.1.0 - 64bit Production 断开
                [typhoon@TFW-CENT6-LT typhoon]$
          2. 语句触发器:
            在某些语句执行前或执行后被触发。

            在已有的项目、表中未找到合适的逻辑,暂时搁置。