通过shell分析表依赖的层级关系
在平时的工作环境中,总会有一些表会存在依赖关系,比如我们有三张表customer,用户表subscriber,账户表account其中客户可以有多个
在平时的工作环境中,总会有一些表会存在依赖关系,比如我们有三张表customer,用户表subscriber,账户表account
其中客户可以有多个用户或者账户,subscriber表和account表中就存在外键customer_id指向了customer表。
这种情况下表的依赖关系就如下所示:
customer
subscriber
account
如果表中的层级关系更为复杂,如果能够得到一个很清晰的依赖关系表。在做一些重要的操作时就能运筹帷幄。避免很多不必要的麻烦。
今天开发的人员私信给我,想让我帮忙在测试环境运行一些脚本,本来这种工作都是需要按照流程的,我们聊了下,他说现在手头有10个左右的脚本,都是些dml相关的操作,每个脚本会对应修改一个表的数据,但是每次执行的时候都会报外键不存在的错误,他想让我来帮忙看看,是哪些表存在依赖关系,这个问题换我来,我也得知道脚本的依赖关系,即涉及到的表的依赖关系,我说我来帮你分析这个关系,还是按照流程来运行你的脚本吧,他半信半疑,我就运行了下面的脚本,得到了一个依赖关系列表。
使用shell脚本分析表依赖的层级关系脚本如下:
sqlplus -s $DB_CONN_STR@$SH_DB_SID 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,' 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
----- -------------------------------------------------- ......
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级开始,逐次类推。
可以看到有些表的依赖都到第7级了,这些如果人工来一个一个审核,确实是很头疼的工作。
本文永久更新链接地址:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Analysis of the basic principles of the MySQL database management system MySQL is a commonly used relational database management system that uses structured query language (SQL) for data storage and management. This article will introduce the basic principles of the MySQL database management system, including database creation, data table design, data addition, deletion, modification, and other operations, and provide specific code examples. 1. Database Creation In MySQL, you first need to create a database instance to store data. The following code can create a file named "my

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.
