SQL Server 数据库中未定义的关系(外键)
我正在使用一个包含近 850 个表的 SQL Server 数据库。它有许多已定义的关系和大量未定义的关系(FK)、未定义的主键等。这是一团糟。我无权访问应用程序源代码,因此无法通过代码追踪未定义的关系。
是否有任何软件或查询可以让我查看数据并找出表之间的关系?更具体地说,每个表中的每个字段(列)都映射(连接)到所有其他表的每个列,并为我提供某种类型的报告。几乎 60% 的情况下,相关表中的列名称相似,但许多表的主键列名称相同(例如 item_id)。
我需要所有那些不确定的关系,这让我每天的生活都很痛苦! :(
I am working with a SQL Server database which contains almost 850 tables. It has many defined relationships and plenty of undefined relationships(FK), undefined primary keys etc. It is a mess. I don't have access to the application source code, so I can't track down the undefined relations through code.
Is there any software or query by which I can just look at the data and figure out the relationships between the tables? To be more specific, every fields(columns) in each tables are mapped (join) against every columns of all other tables and provide me with a report of some sort. Almost 60% of the cases the column names would be similar in related tables but many tables have same column name for primary key(for example item_id).
I need all those undefined relationships which is making my life miserable everyday!! :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为最好的选择是使用探查器捕获正在执行的语句并尝试从中推断关系。这是一个艰难的问题,据我所知,没有任何简单的解决方案。
祝你好运 !
I think your best bet would be to use the profiler to capture the statements being executed and try infer the relationships from that. This is a tough one, and there aren't any easy solutions that I'm aware of.
Good Luck !
好吧,您可以查询元数据 -
INFORMATION_SCHEMA.COLUMNS
- 过滤掉极不可能作为键连接的内容 - 例如 TEXT/NVARCHAR(MAX)。将其放入某种数据字典表中,您可以在其中开始用信息标记列。您可以使用以下内容进行查询:
查看是否存在明显的匹配项。
这可能会帮助您掌握数据库。但这需要做很多工作。
如果没有外键约束,他们甚至可能会执行诸如“多键”之类的操作,其中某一列是一个表或另一个表的外键,具体取决于某种类型选择器(这些对于外键是不可能的)约束) - 除了单独的联接之间之外,您甚至可能在探查器中看不到这一点 - 因此有时您可能会看到它联接到一个表,有时又联接到另一个表。
Well, you can query the metadata -
INFORMATION_SCHEMA.COLUMNS
- filter out things which are highly unlikely to be joined as keys - like TEXT/NVARCHAR(MAX). Put it in some kind of data dictionary table where you start to tag the columns with information.You can query with things like:
to see if there are obvious matches.
That might help you get a handle on the database. But it will take a lot of work.
Without a foreign key constraint, it's even possible that they've done things like "multi-keys" where a certain column is a foreign key to one table or another depending on some kind of type selector (these aren't possible with foreign key constraints) - it's possible you won't even see this in the profiler except between separate joins - so one time you might see it join to one table and sometimes another.