通过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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











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

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.

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.

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())

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

Use the DataAccessObjects (DAO) library in C++ to connect and operate the database, including establishing database connections, executing SQL queries, inserting new records and updating existing records. The specific steps are: 1. Include necessary library statements; 2. Open the database file; 3. Create a Recordset object to execute SQL queries or manipulate data; 4. Traverse the results or update records according to specific needs.
