I'm inheriting a database that has 400 tables and only 150 foreign key constraints registered. Knowing what I do about the application and looking at the table columns, it's easy to say that there ought to be a lot more.
I'm afraid that the current application software will break if I started adding the missing FKs because the developers have probably come to rely on this "freedom", but step one in fixing the problem is to come up with the list of missing FKs so we can evaluate them as a team.
To make matters worse, the referencing columns don't share a naming convention.
The relationships ARE coded informally into the hundreds of ad-hoc queries and stored procedures, so my hope is to parse these files programmatically looking for JOINS between actual tables (but not table variables, etc).
Challenges I foresee in this approach are: newlines, optional aliases and table hints, alias resolution.
- Any better ideas? (Besides quitting)
- Are there any pre-built tools that can solve this?
- I don't think regex can handle this. Do you disagree?
- SQL Parsers? I tried using Microsoft.SqlServer.Management.SqlParser.Parser but all that is exposed is the lexer - can't get an AST out of it - all that stuff is internal.
I feel your pain.
The free SQL Search SSMS Addin might be helpful for you.
In general, Yes, regex can handles this, but you should be aware of the point of diminishing returns in attempting to conjure regex magic. You may be better off just reviewing and searching through the code while mapping out the relationships.
SQL Search might make this a lot easier for you.
Here's what I came up with. This query looks for foreign-key-like columns (int, bigint, guid) which are not the primary key of the table and which are not currently registered with a foreign key constraint. Sure, I get a few Sort Order and Quantity columns, but it really narrows down the list with less effort than parsing out SQL scripts.
Powerful SQL Parser can help to analyze the hundreds of ad-hoc queries and stored procedures automatically, and from the query parse tree generated by SQL Parser, you can easily to find relationship of variables table/columns.
Here is sample:
After analyzing this query, you may get something like this:
You can check this demo for detailed information.