SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
写如下触发器,是为了使DEPT表的DEPTNO列发生变化的时候,EMP表的相关的行的对应的列也做相应修改,用这个来增强参照完整性的约束
SQL> create or replace trigger cascade_update
2 after update on delete on dept
3 for each row
4 begin
5 update emp
6 set emp.deptno= :new.deptno
7 where emp.deptno=
ld.deptno;
8 end;
9 /
after update on delete on dept
*
ERROR 位于第 2 行:
ORA-00903: 表名无效
SQL> l 2
2* after update on delete on dept
SQL> c /update on/update or
2* after update or delete on dept
SQL> /
触发器已创建
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
看看原先EMP表有DEPTNO=10的列
SQL> select * from emp where deptno=10;
EMPNO ENAME JOB MGR HIREDATE SAL CO
---------- ---------- --------- ---------- ---------- ---------- --------
DEPTNO
----------
7782 CLARK MANAGER 7839 09-6月 -81 2450
10
7839 KING PRESIDENT 17-11月-81 5000
10
7934 MILLER CLERK 7782 23-1月 -82 1300
10
SQL> update dept set deptno=1 where deptno=10;
已更新 1 行。
然后验证一下情况
发现原先有DEPTNO=10的列不见了
SQL> select * from emp where deptno=10;
未选定行
变成等于1的列了
SQL> select * from emp where deptno=1;
EMPNO ENAME JOB MGR HIREDATE SAL CO
---------- ---------- --------- ---------- ---------- ---------- --------
DEPTNO
----------
7782 CLARK MANAGER 7839 09-6月 -81 2450
1
7839 KING PRESIDENT 17-11月-81 5000
1
7934 MILLER CLERK 7782 23-1月 -82 1300
1
提交一下
SQL> commit;
提交完成。
SQL> select * from emp where deptno=1;
EMPNO ENAME JOB MGR HIREDATE SAL CO
---------- ---------- --------- ---------- ---------- ---------- --------
DEPTNO
----------
7782 CLARK MANAGER 7839 09-6月 -81 2450
1
7839 KING PRESIDENT 17-11月-81 5000
1
7934 MILLER CLERK 7782 23-1月 -82 1300
1
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
1 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
然后开另外一个回话窗口,按道理来说EMP表的改变是在触发器中实现的,触发器中又没有COMMIT语句,另一个窗口应该看不到EMP表的改变吧,但是事实是可以看到啊,说明被提交了啊。是否是DEPT提交后EMP也会被提交呢
连接到:
Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.1.0 - Production
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
1 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> select * from emp where deptno=10;
未选定行
SQL> select * from emp where deptno=1;
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- ---------- ---------- ----------
DEPTNO
----------
7782 CLARK MANAGER 7839 09-6月 -81 2450
1
7839 KING PRESIDENT 17-11月-81 5000
1
7934 MILLER CLERK 7782 23-1月 -82 1300
1
SQL>