Oracle触发器5-Instead of触发器
Oracle触发器5-Instead of触发器Oracle触发器4-数据库事件触发器http://www.zzzyk.com/database/201304/201423.htmlInstead of 触发器可以在由一个以上的表组成的视图上进行insert,update,delete,merge.也可以使用instead of 来创建分区表等。创建instead of 触发器语法:1 CREATE [OR REPLACE] TRIGGER trigger_name2 INTEAD OF operation3 ON view_name4 FOR EACH ROW5 BEGIN6 ...code goes here...7 END;下面开始测试 Instead of insert,instead of update,instead of delete触发器以及嵌套表的instead of 触发器1.创建测试表employee,permission_code,user_roleCREATE TABLE employee (employee_no VARCHAR2(8),last_name VARCHAR2(25) NOT NULL,first_name VARCHAR2(10) NOT NULL,dept_code VARCHAR2(3) NOT NULL,active_flag VARCHAR2(1) DEFAULT 'Y',mod_user_id VARCHAR2(30) DEFAULT USER,mod_user_date DATE DEFAULT SYSDATE);CREATE TABLE permission_code (pcode VARCHAR2(2),pcode_description VARCHAR2(40) NOT NULL,mod_user_id VARCHAR2(30) DEFAULT USER,mod_user_date DATE DEFAULT SYSDATE);CREATE TABLE user_role (dept_code VARCHAR2(3),pcode VARCHAR2(2),access_level VARCHAR2(1) DEFAULT 'R',mod_user_id VARCHAR2(30) DEFAULT USER,mod_user_date DATE DEFAULT SYSDATE);CREATE TABLE user_permission (employee_no VARCHAR2(8),pcode VARCHAR2(2),access_level VARCHAR2(1) DEFAULT 'R',mod_user_id VARCHAR2(30) DEFAULT USER,mod_user_date DATE DEFAULT SYSDATE);CREATE TABLE dept_code (dept_code VARCHAR2(3),dept_name VARCHAR2(30));CREATE TABLE test (test VARCHAR2(20));2.向表中插入测试数据:-- employee tableINSERT INTO employee(employee_no, last_name, first_name, dept_code, active_flag)VALUES('5001', 'Mark', 'Townsend', 'LCR', 'Y');INSERT INTO employee(employee_no, last_name, first_name, dept_code, active_flag)VALUES('3996', 'Dacko', 'Carol', 'ESR', 'Y');INSERT INTO employee(employee_no, last_name, first_name, dept_code, active_flag)VALUES('6842', 'Morgan', 'Daniel', 'ADM', 'Y');-- permission_code table dataINSERT INTO permission_codeVALUES ('BO', 'BILLING OPTIONS', USER, SYSDATE);INSERT INTO permission_codeVALUES ('CL', 'CLASS CODES', USER, SYSDATE);INSERT INTO permission_codeVALUES ('CR', 'CREWS', USER, SYSDATE);INSERT INTO permission_codeVALUES ('CT', 'CREW TYPES', USER, SYSDATE);INSERT INTO permission_codeVALUES ('CU', 'CUSTOMER TYPES', USER, SYSDATE);INSERT INTO permission_codeVALUES ('DH', 'WORKORDER DASH NUMBERS', USER, SYSDATE);INSERT INTO dept_code (dept_code, dept_name)VALUES ('ADM', 'ADMINISTRATION');INSERT INTO dept_code (dept_code, dept_name)VALUES ('COO', 'COORDINATOR');INSERT INTO dept_code (dept_code, dept_name)VALUES ('ESE', 'ELECTRICAL SERVICE');INSERT INTO dept_code (dept_code, dept_name)VALUES ('ESR', 'ELECTRICAL SERVICE REP');INSERT INTO dept_code (dept_code, dept_name)VALUES ('ENG', 'ENGINEER');INSERT INTO dept_code (dept_code, dept_name)VALUES ('LCR', 'LINE CREW');INSERT INTO dept_code (dept_code, dept_name)VALUES ('MCR', 'METER CREW');INSERT INTO dept_code (dept_code, dept_name)VALUES ('NWE', 'NETWORK ENGINEER');INSERT INTO dept_code (dept_code, dept_name)VALUES ('SKA', 'SKETCH ARTIST');INSERT INTO user_role(dept_code, pcode, access_level)SELECT r.dept_code, p.pcode, 'R'FROM dept_code r, permission_code p;INSERT INTO user_permission(employee_no, pcode, access_level)SELECT e.employee_no, r.pcode, r.access_levelFROM employee e, user_role rWHERE e.dept_code = r.dept_code;COMMIT;3.创建视图role_permission_view和employee_permission_view并测试是否能更新、删除视图数据。CREATE OR REPLACE VIEW role_permission_view ASSELECT r.dept_code, r.pcode, p.pcode_description, r.access_levelFROM user_role r, permission_code pWHERE r.pcode = p.pcode;-- this will failINSERT INTO role_permission_view(dept_code, pcode, pcode_description, access_level)VALUES('DAN', 'DM', 'Morgan', 'W');-- this will fail tooUPDATE role_permission_viewSET access_level = 'W'WHERE dept_code = 'SKA';-- another relational viewCREATE OR REPLACE VIEW employee_permission_view ASSELECT e.employee_no,e.first_name || ' ' || e.last_name NAME, e.dept_code,r.pcode, r.access_level DEFACCLVL, u.access_level,p.pcode_descriptionFROM employee e, user_role r, user_permission u, permission_code pWHERE e.dept_code = r.dept_codeAND e.employee_no = u.employee_noAND r.pcode = u.pcodeAND r.pcode = p.pcodeORDER BY 1,3;-- this will fail tooDELETE FROM employee_permission_viewWHERE dept_code = 'LCR';4.Instead Of Insert Trigger 举例:CREATE OR REPLACE TRIGGER ioft_insert_role_permINSTEAD OF INSERTON role_permission_viewFOR EACH ROWDECLAREx INTEGER;BEGINSELECT COUNT(*)INTO xFROM permission_codeWHERE pcode = :NEW.pcode;IF x = 0 THENINSERT INTO permission_code(pcode, pcode_description, mod_user_id, mod_user_date)VALUES(:NEW.pcode, 'New Code', USER, SYSDATE);END IF;SELECT COUNT(*)INTO xFROM dept_codeWHERE dept_code = :NEW.dept_code;IF x = 0 THENINSERT INTO dept_code(dept_code, dept_name)VALUES(:NEW.dept_code, 'New Dept');END IF;INSERT INTO user_role(dept_code, pcode, mod_user_id)VALUES(:NEW.dept_code, :NEW.pcode, 'Morgan');INSERT INTO test(test)VALUES('Z');END ioft_insert_role_perm;/SELECT *FROM permission_codeWHERE pcode = 'DM';SELECT *FROM dept_codeWHERE dept_code = 'DAN';SELECT *FROM user_roleWHERE dept_code = 'DAN';SELECT * FROM test;-- insert worksINSERT INTO role_permission_view(dept_code, pcode, pcode_description, access_level)VALUES('DAN上一个:Oracle触发器6-管理触发器
下一个:Oracle SQL多表查询
- 更多Oracle疑问解答:
- 运行exp备份oracle数据库提示oracle-12154错误
- 有没有,生产Oracle Rman 备份脚本的工具啊!
- 初学orcle,希望有大大帮忙解说一下详细步骤,从登录oracle到创建表的过程
- oracle语句问题:一张user表,三个字段,id,name,time,插入记录比如:张三2007,李四2008,张三2011
- 如何写一个ORACLE触发器同步两个表中的数据?
- oracle 如何查看一个服务器上有多少个数据库.
- oracle 创建包的时候错误 求解
- oracle 重复列的问题
- oracle 中如何查处2星期前的数据
- 请教oracle数据库安装中的问题
- 请问谁能提供给我标准的oracle ERP的数据库表结构并详细说明各表主要的作用?
- 安装oracle遇到的问题 invalid entry CRC (expected 0x3e12e795 but got 0x9db0e9fd)
- 我的是ORACLE 10G,在RMAN中如何按指定的时间恢复数据文件啊?
- oracle为什么没有自动增长列
- oracle快捷键都有哪些啊?