我正在执行类似
的声明DELETE FROM USER WHERE USER_ID=1;
在SQLDeveloper中。
由于在许多表中引用了用户(例如用户有订单,设置......),我们激活了ON DELETE CASCADE,这样我们就不必手动删除每一行。然而,在开发过程中,我们有兴趣知道有多少行以及哪些表通过级联删除“自动”删除。
有没有办法找到这个。是通过SQL-Statement,直接在sqldeveloper中从日志文件还是其他任何想法?
答案 0 :(得分:2)
虽然使用sql%rowcount是不可能的,但是如果你编写触发器代码是可能的,但这意味着你需要在要监视的所有表上触发。触发器也会使操作变慢。
e.g:
SQL> select * from one;
ID
----------
1
2
SQL> select * from child_of_one;
ID O_ID
---------- ----------
1 1
2 1
3 1
4 2
5 2
6 2
7 2
8 2
我们希望包规范包含一个表+计数数组:
SQL> create or replace package foo
2 as
3 type rowcount_tab is table of pls_integer index by varchar2(30);
4 t_rowcount rowcount_tab;
5 end foo;
6 /
Package created.
我们希望顶级表上的触发器将这些计数重置为零:
SQL> create or replace trigger one_biud
2 before insert or update or delete
3 on one
4 declare
5 begin
6 foo.t_rowcount.delete;
7 end;
8 /
Trigger created.
这假设您只对从顶级表中删除的数组感兴趣。如果没有,您需要在foo.t_rowcount.delete('TABLE_NAME')
的每张桌子上设置一个触发器。
现在为每个感兴趣的表上的每个行触发设置数组:
SQL> create or replace trigger one_aiudfer
2 after insert or update or delete
3 on one
4 for each row
5 declare
6 begin
7 if (foo.t_rowcount.exists('ONE'))
8 then
9 foo.t_rowcount('ONE') := nvl(foo.t_rowcount('ONE'), 0)+1;
10 else
11 foo.t_rowcount('ONE') := 1;
12 end if;
13 end;
14 /
Trigger created.
SQL> create or replace trigger child_of_one_aiudfer
2 after insert or update or delete
3 on child_of_one
4 for each row
5 declare
6 begin
7 if (foo.t_rowcount.exists('CHILD_OF_ONE'))
8 then
9 foo.t_rowcount('CHILD_OF_ONE') := nvl(foo.t_rowcount('CHILD_OF_ONE'), 0)+1;
10 else
11 foo.t_rowcount('CHILD_OF_ONE') := 1;
12 end if;
13 end;
14 /
Trigger created.
现在当我们删除或其他什么时候:
SQL> delete from one where id = 1;
1 row deleted.
SQL> declare
2 v_table varchar2(30);
3 begin
4 v_table := foo.t_rowcount.first;
5 loop
6 exit when v_table is null;
7 dbms_output.put_line(v_table || ' ' || foo.t_rowcount(v_table) || ' rows');
8 v_table := foo.t_rowcount.next(v_table);
9 end loop;
10 end;
11 /
CHILD_OF_ONE 3 rows
ONE 1 rows
PL/SQL procedure successfully completed.
SQL> delete from one where id = 2;
1 row deleted.
SQL> declare
2 v_table varchar2(30);
3 begin
4 v_table := foo.t_rowcount.first;
5 loop
6 exit when v_table is null;
7 dbms_output.put_line(v_table || ' ' || foo.t_rowcount(v_table) || ' rows');
8 v_table := foo.t_rowcount.next(v_table);
9 end loop;
10 end;
11 /
CHILD_OF_ONE 5 rows
ONE 1 rows