Oracle Delete语句:级联删除删除了多少行
我在执行一个像这样的语句Oracle Delete语句:级联删除删除了多少行,oracle,oracle-sqldeveloper,cascading-deletes,Oracle,Oracle Sqldeveloper,Cascading Deletes,我在执行一个像这样的语句 DELETE FROM USER WHERE USER_ID=1; 在SQLDeveloper中 由于用户在许多表中被引用(例如,用户有一个订单、设置等),因此我们在DELETE CASCADE上激活,这样我们就不必手动删除每一行。然而,在开发过程中,我们想知道有多少行和哪些表是通过级联删除“自动”删除的 有没有办法找到答案。通过SQL语句、直接在sqldeveloper中通过日志文件或任何其他想法?虽然SQL%rowcount无法实现这一点,但如果您编写触发器代码,
DELETE FROM USER WHERE USER_ID=1;
在SQLDeveloper中
由于用户在许多表中被引用(例如,用户有一个订单、设置等),因此我们在DELETE CASCADE上激活,这样我们就不必手动删除每一行。然而,在开发过程中,我们想知道有多少行和哪些表是通过级联删除“自动”删除的
有没有办法找到答案。通过SQL语句、直接在sqldeveloper中通过日志文件或任何其他想法?虽然SQL%rowcount无法实现这一点,但如果您编写触发器代码,则可以实现这一点,但这意味着您需要在所有要监视的表上使用触发器。触发器也会使操作速度稍微减慢 e、 g: 我们需要一个包规范来保存表+计数的数组:
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\u rowcount.delete('table\u NAME')
作为触发器
现在,在每个感兴趣的表上使用after for each row触发器来设置数组:
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
我不认为这是可能的。我不知道这样的功能,我想没有人。但您可以递归地查询数据字典(查看DBA_约束),并在详细信息表中计算受影响的行。
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