首页 > 数据库 > Oracle > 通过shell分析表依赖的层级关系
sqlplus -s $DB_CONN_STR@$SH_DB_SID <<EOF
set pages 1000
set echo off
set feedback off
create table table_depency_rel as
(
select
p.table_name parent_table_name ,
p.owner p_owner,
c.table_name child_table_name ,
c.owner c_owner
from user_constraints p, user_constraints c
where p.constraint_type IN ('P','U') AND
c.constraint_type = 'R' AND
p.constraint_name = c.r_constraint_name
group by p.table_name,p.owner, c.table_name, c.owner
)
;
--alter table table_depency_rel modify(p_owner null);
alter table table_depency_rel modify(parent_table_name null);
insert into table_depency_rel
(
select null,null,parent_table_name,p_owner from table_depency_rel
where parent_table_name not in (select child_table_name from table_depency_rel group by child_table_name)group by parent_table_name,p_owner
);
set echo on
set feedback on
col table_node format a50
col level_code format a5
select decode(level,1,'<--'||level,level)||'-' level_code , lpad('-',level*3-1,'--')||'||'||t.child_table_name||'('||level||')' table_node from table_depency_rel t
connect by prior t.child_table_name =t.parent_table_name
start with t.child_table_name in(select child_table_name from table_depency_rel where parent_table_name is null group by child_table_name)
--order by parent_table_name desc;
set feedback off
set echo off
drop table table_depency_rel;
EOF
exit
自己在反复模拟一些场景之后总结了如上的脚本。
我们来通过如下的方式运行脚本,查看system下的表依赖关系。
ksh showdepency.sh
----- -------------------------------------------------- ......
<--1- --||DE_CRI_PARAMS(1)
2- -----||DE_CRI_ALLOWED_OPS(2)
2- -----||DE_CRI_ITEMS(2)
3- --------||DE_CRI_CONNECTIONS(3)
3- --------||DE_CRI_POOL(3)
4- -----------||DE_CONDITIONS(4)
5- --------------||DE_DP_COMPONENTS(5)
6- -----------------||DE_CONTRIBUTE_LIST(6)
6- -----------------||DE_EXTRA_DPC_FIELDS(6)
6- -----------------||DE_TIERS(6)
7- --------------------||DE_STEPS(7)
可以很清晰的看到显示的层级关系,有1,2,3,4 其中1是根节点,2,3,4是依赖表,4依赖3,3依赖2,依此类推。运行脚本的时候是从第1级开始,逐次类推。来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30633755/viewspace-2127782/ ,如需转载,请注明出处,否则将追究法律责任。