In Part I and Part II of the series, I discussed documenting and discovering Primary Keys and Clustered Indexes. In this article I will show how to document table relationships from a hierarchical standpoint. The type of documentation I will demonstrate in this article will prove insightful and useful – if for nothing more than pure documentation.
I have had the pleasure of sharing this information in a User Group meeting. That was the first time I had presented at a User’s Group meeting. I gave that presentation shortly after hammering out the script to help document a database as a part of a project that is still on-going. For the project, I first set out to find a script that fulfill the requirements for me. All I could find were scripts similar to what I had already written. They would present the Tables and foreign keys for those tables as well as the child tables. The problem with this was that the list was in no particular order. I then found another script that presented the data in an hierarchy – however it was not a true hierarchy. I needed a script that could lay out the hierarchy of the foreign keys in the database when provided with one table from the database as the starting point. This is how I define the perspective. Now would be a good time to discuss the requirements I had defined for this script.
- Document Foreign Key Relationships
- Foreign Key Relationships listed in Hierarchical Format
- Create Perspectives (View of Hierarchy Tree from Specified Parent Table)
- No Cursors or While Loops
Why are these requirements necessary? This is to help document a database. I needed to be able to see the ERD at a reasonable size. By adding the perspective requirement I could take a large database and reduce the number of objects displayed very quickly. Furthermore, by using a script, I can create the documentation that displays the table relationships very quickly. This information also helps in knowing the insert and delete order of data in the respective tables as they relate to one another.
The scripts I could find on the net were quickly eliminated due to the requirements. Most used a looping mechanism, while others did not create a true hierarchy. Thus I turned to my own information store to develop a script that could traverse the system views and create the kind of report that I needed.
To meet the requirements, I started looking to a CTE. And yes a recursive CTE at that. I know – not 100% set-based due to the recursion – but, it is very efficient for this purpose. In scenarios such as this, this kind of solution is acceptable. So, starting with the base query:
[codesyntax lang=”sql”]
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
;With FkCascade as ( Select OBJECT_NAME(sfk.referenced_object_id) as ParentTable ,OBJECT_NAME(sfk.Parent_object_id) as ChildTable ,sc.name as ChildColumn ,sc2.name as ParentColumn ,0 as FKLevel From sys.foreign_key_columns sfkc Inner Join sys.foreign_keys sfk On sfkc.constraint_object_id = sfk.object_id Inner Join sys.columns sc On sfkc.parent_object_id = sc.object_id And sfkc.parent_column_id = sc.column_id Inner join sys.columns sc2 On sfkc.referenced_object_id = sc2.object_id And sfkc.referenced_column_id = sc2.column_id Union All Select OBJECT_NAME(sfk.referenced_object_id) as ParentTable ,OBJECT_NAME(sfk.Parent_object_id) as ChildTable ,sc.name as ChildColumn ,sc2.name as ParentColumn, FKLevel + 1 From FkCascade F Inner Join sys.foreign_keys sfk On f.ChildTable = OBJECT_NAME(sfk.referenced_object_id) Inner Join sys.foreign_key_columns sfkc On sfkc.constraint_object_id = sfk.object_id Inner Join sys.columns sc On sfkc.parent_object_id = sc.object_id And sfkc.parent_column_id = sc.column_id Inner join sys.columns sc2 On sfkc.referenced_object_id = sc2.object_id And sfkc.referenced_column_id = sc2.column_id ) Select * from FKCascade Order By ParentTable asc option (maxrecursion 500) |
[/codesyntax]
This query gets me pretty close to the desired outcome and only needs a few more tweaks. The above script more or less does the same sort of thing I saw other scripts doing that I found from internet searches. The tables and foreign key level are displayed, but no linkage is quickly displayed by the query. That can be resolved by adding a varbinary to the the query and then concatenating the value with each recursion.
[codesyntax lang=”sql”]
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
;With FkCascade (ParentTable,ParentColumn, ChildTable,ChildColumn, FKLevel, SortCol) as ( Select OBJECT_NAME(sfk.referenced_object_id) as ParentTable ,sc2.name as ParentColumn ,OBJECT_NAME(sfk.Parent_object_id) as ChildTable ,sc.name as ChildColumn ,0 as FKLevel,cast(row_number() over(order by sfk.referenced_object_id) as varbinary(50)) --varbinary value and sortcol help to create the hierarchy. Larger trees require a larger varbinary value From sys.foreign_key_columns sfkc Inner Join sys.foreign_keys sfk On sfkc.constraint_object_id = sfk.object_id Inner Join sys.columns sc On sfkc.parent_object_id = sc.object_id And sfkc.parent_column_id = sc.column_id Inner join sys.columns sc2 On sfkc.referenced_object_id = sc2.object_id And sfkc.referenced_column_id = sc2.column_id Union All Select OBJECT_NAME(sfk.referenced_object_id) as ParentTable ,sc2.name as ParentColumn ,OBJECT_NAME(sfk.Parent_object_id) as ChildTable ,sc.name as ChildColumn ,FKLevel + 1,cast(SortCol + cast(row_number() over(order by sfk.referenced_object_id) as varbinary(3)) as varbinary(50)) From FkCascade F Inner Join sys.foreign_keys sfk On f.ChildTable = OBJECT_NAME(sfk.referenced_object_id) Inner Join sys.foreign_key_columns sfkc On sfkc.constraint_object_id = sfk.object_id Inner Join sys.columns sc On sfkc.parent_object_id = sc.object_id And sfkc.parent_column_id = sc.column_id Inner join sys.columns sc2 On sfkc.referenced_object_id = sc2.object_id And sfkc.referenced_column_id = sc2.column_id ) Select ParentTable,ParentColumn, ChildTable,ChildColumn, FKLevel, SortCol,LEN(sortcol) From FKCascade Order By SortCol asc option (maxrecursion 500) |
[/codesyntax]
Ok, now the query is very close to what I need. From the requirements, I only need to alter the script just a bit more in order to be able to create a perspective. The perspective is achieved through variables and a slight change to the CTE.
[codesyntax lang=”sql”]
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
DECLARE @StartParentTable Varchar(256) SELECT @StartParentTable = 'MyTable' ;With FkCascade (ParentTable,ParentColumn, ChildTable,ChildColumn, FKLevel, SortCol) as ( Select OBJECT_NAME(sfk.referenced_object_id) as ParentTable ,sc2.name as ParentColumn ,OBJECT_NAME(sfk.Parent_object_id) as ChildTable ,sc.name as ChildColumn ,0 as FKLevel,cast(row_number() over(order by sfk.referenced_object_id) as varbinary(50)) From sys.foreign_key_columns sfkc Inner Join sys.foreign_keys sfk On sfkc.constraint_object_id = sfk.object_id Inner Join sys.columns sc On sfkc.parent_object_id = sc.object_id And sfkc.parent_column_id = sc.column_id Inner join sys.columns sc2 On sfkc.referenced_object_id = sc2.object_id And sfkc.referenced_column_id = sc2.column_id Where OBJECT_NAME(sfk.referenced_object_id) = @StartParentTable Union All Select OBJECT_NAME(sfk.referenced_object_id) as ParentTable ,sc2.name as ParentColumn ,OBJECT_NAME(sfk.Parent_object_id) as ChildTable ,sc.name as ChildColumn ,FKLevel + 1,cast(SortCol + cast(row_number() over(order by sfk.referenced_object_id) as varbinary(3)) as varbinary(50)) From FkCascade F Inner Join sys.foreign_keys sfk On f.ChildTable = OBJECT_NAME(sfk.referenced_object_id) Inner Join sys.foreign_key_columns sfkc On sfkc.constraint_object_id = sfk.object_id Inner Join sys.columns sc On sfkc.parent_object_id = sc.object_id And sfkc.parent_column_id = sc.column_id Inner join sys.columns sc2 On sfkc.referenced_object_id = sc2.object_id And sfkc.referenced_column_id = sc2.column_id ) Select ParentTable,ParentColumn, ChildTable,ChildColumn, FKLevel, SortCol From FKCascade Order By SortCol asc option (maxrecursion 500) Go |
[/codesyntax]
Through the above script, I am now able to achieve all of the requirements that had been pre-defined. In testing the query, however, I discovered that the query was not yet quite complete. There comes a time when there is an additional foreign key defined on the table that does not fit into the above query as a downstream perspective of the origin table. There may be an occasion where an additional “parent” table needs to be displayed in the query. Thus I must be able to traverse back upwards now in order to complete the perspective. This was achieved through the following query.
[codesyntax lang=”sql”]
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |