Question

I'd like to generate a report that displays all the tables in the database. For each table, the report would document the tables it is referencing, and the tables it is referenced by. Also included, is the foreign key used for referencing.

I've tried using Red Gate SQL Dependency Tracker previously, and it gave me an easy-to-read, usable report--what was lacking was just the foreign key for each table association.

If anyone could be a great alternative software that could do what I want to do, or provide some form of an alternative method, I'd be very grateful! My main purpose of this is actually to provide the customer with some form of a readable schema, so that the customer can understand the interconnections within the DB... I've tried using database diagrams but that was a no-go due to the readability.

Thanks!

Was it helpful?

Solution 5

I used Toad for SQL Server at the end of the day. While database documentation might not actually be ace standard, it is more customizable than Red Gate's. Using the report generator, I was able to come up with dependencies for each foreign key, as well as the columns they referenced...

Many thanks to all your answers, they provided valuable insight for me.

OTHER TIPS

I use this script querying some of the system catalog view

SELECT 
    fk.name 'FK Name',
    tpar.name 'Parent Table',
    colpar.name 'Parent Column',
    tref.name 'Referenced Table',
    colref.name 'Referenced Column',
    fk.delete_referential_action_desc 'Delete Action',
    fk.update_referential_action_desc 'Update Action'
FROM
    sys.foreign_keys fk
INNER JOIN 
    sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id
INNER JOIN 
    sys.tables tpar ON fk.parent_object_id = tpar.object_id
INNER JOIN 
    sys.columns colpar ON fkc.parent_object_id = colpar.object_id AND fkc.parent_column_id = colpar.column_id
INNER JOIN 
    sys.tables tref ON fk.referenced_object_id = tref.object_id
INNER JOIN 
    sys.columns colref ON fkc.referenced_object_id = colref.object_id AND fkc.referenced_column_id = colref.column_id

and it produces an output something like:

FK Name   Parent Table   Parent Column   Referenced Table   Referenced Column   Delete Action   Update Action

which works quite nicely for me. Feel free to adapt to your own needs.

Dependency Tracker does not show the foreign keys by default, but it does if you click the "show constraints as objects" button.

sp_help <table_name> outputs foreign keys that the table references as well as tables that reference its columns as foreign keys. Querying sys.tables and using sp_help should give you all the information you need.

Have you tried ApexSQL Clean? It has options to display all references visually, dig into all relationships and such...

If your goal is to only create document that shows all the data you should be looking at some documenting tools and there are a lot of these out there. I'm using ApexSQL Doc but I guess you can't go wrong with any other popular tool from some other big vendor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top