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.

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:

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.

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.

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.

This query can easily be modified to pull back just the unique table names that are found in the CTE. Why would that be necesary? That would provide an easy method to pull back unique tables in the event that an ERD is to be derived from the perspective. I didn’t delve too much into any of the scripts presented here. At this point in the series, the only complexity that may need to be explained is the recursive piece of the scripts. I think there are plenty of articles on that very subject. The main goal here is to show that the documentation of FKs can be made relatively simple as well as provide plenty of insight into the database. Test the queries.

I am including the scripts and the execution plan for the final script. Included in the scripts will be the presentation given on the same subject. The inclusion of that slide deck is a secondary reason for not getting into great detail in this post. You can download it here.

The final blog in this particular series will be very short. I will cover the need for indexes on FKs. This is another topic that has been recently discussed. I will explore a couple of different scripts and compare and contrast those scripts performance.

Comments

Posted by ggayle on 11 February 2010

Jason, great script. The perspective from a specified entry point is particularly helpful, since problems usually arise from a specific set of table relationships. The incremental presentation was great too; very educational.

Posted by Jason Brimhall on 11 February 2010

GGayle - Thanks

Posted by Stephen J. Oesterreicher on 16 February 2010

Please note that each one of has our version of how wide we set the screen up. This document wants a really wide screen that almost makes me not want to view your insight.

Posted by Tom.Thomson on 17 February 2010

I gave up trying to read this. Constant horizontal scrolling is a pain.

Posted by Jason Brimhall on 2 April 2010

I missed the comments on the screen width issue. I believe the issue is related to the syndication from my blog to SSC. I will check with Steve on that.

Posted by Steve Jones on 2 April 2010

I checked and there are some things WordPress puts in that are causing us issues. I'm not sure what to do about it in the short term.