以编程方式提取 RDBMS 中表之间的关系(无需外键)?
我正在对 Oracle 数据库中中等规模的表(50+)之间的关系进行逆向工程,其中表之间没有定义外键。 我(在某种程度上)可以指望能够跨表匹配列名。 例如,列名“SomeDescriptiveName”在整个表集中可能是相同的。
我希望能够做的是找到一种更好的方法来根据那些匹配的列名称提取一些关系集,而不是手动逐一浏览表。 我可以使用 Java DatabaseMetaData 方法做一些事情,但这似乎是以前有人可能必须编写脚本的任务之一。 也许使用 Perl 或其他脚本语言提取列名称,使用列名称作为散列键并将表添加到散列键指向的数组中?
有人有任何提示或建议可以使这变得更简单或提供一个良好的起点吗? 这是一个丑陋的需求,如果已经定义了外键,那么理解关系就会容易得多。
谢谢。
I'm reverse engineering the relationships between a medium-sized number of tables (50+) in an Oracle database where there are no foreign keys defined between the tables. I can count (somewhat) on being able to match column names across tables. For example, column name "SomeDescriptiveName" is probably the same across the set of tables.
What I would like to be able to do is to find a better way of extracting some set of relationships based on those matching column names than manually going through the tables one by one. I could do something with Java DatabaseMetaData methods but it seems like this is one of those tasks that someone has probably had to script before. Maybe extract the columns names with Perl or some other scripting lang, use the column names as a hash key and add tables to an array pointed to by the hash key?
Anyone have any tips or suggestions that might make this simpler or provide a good starting point? It's an ugly need, if foreign keys had already been defined, understanding the relationships would have been much easier.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您几乎已经在问题中写下了答案。
You pretty much wrote the answer in your question.
我的策略是使用 Oracle 系统目录查找列名称和数据类型相同但表名称不同的列。 另外,哪一列是表的主键或唯一键的一部分。
这是一个可能接近执行此操作的查询,但我没有方便的 Oracle 实例来测试它:
当然,这不会得到任何相关但具有不同名称的列的情况。
My strategy would be to use the Oracle system catalog to find columns that are the same in column name and data type but different in table name. Also which one of the columns is part of a table's primary or unique key.
Here's a query that may be close to doing this, but I don't have an Oracle instance handy to test it:
Of course this won't get any case of columns that are related but have different names.
您可以结合使用三种(或四种)方法,具体取决于架构的混淆程度:
You can use a combination of three (or four) approaches, depending on how obfuscated the schema is:
这是个有趣的问题。 我采取的方法是强力搜索与小样本集的类型和值匹配的列。 您可能需要调整启发式方法才能为您的模式提供良好的结果。 我在不使用自动递增键的模式上运行了它,并且运行良好。 该代码是为 MySQL 编写的,但很容易适应 Oracle。
This is an interesting question. The approach I took was a brute force search for columns that matched types and values for a small sample set. You'll probably have to tweak the heuristics to provide good results for your schema. I ran this on a schema that didn't use auto-incremented keys and it worked well. The code is written for MySQL, but it's very easy to adapt to Oracle.